Skip to content

Commit

Permalink
Merge pull request #382 from shesha-io/fix/containers-in-subforms
Browse files Browse the repository at this point in the history
fix(ComponentsContainerSubForm): wrapped the components with div
  • Loading branch information
Lukeybooi committed Jul 25, 2023
2 parents 9fcedff + 25eab41 commit 6f104d5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 117 deletions.
Original file line number Diff line number Diff line change
@@ -1,54 +1,61 @@
import { IComponentsContainerBaseProps } from "interfaces";
import { useGlobalState, useSubForm } from "providers";
import React, { Fragment } from "react";
import { FC, useCallback } from "react";
import { executeScriptSync } from "utils/publicUtils";
import DynamicComponent from "../dynamicView/dynamicComponent";

export const ComponentsContainerSubForm: FC<IComponentsContainerBaseProps> = props => {
const { containerId, readOnly } = props;
const { getChildComponents } = useSubForm();
const components = getChildComponents(containerId);

//alias added for readability and avoiding names clashes
const { value: subFormData } = useSubForm();
const { globalState } = useGlobalState();

const executeExpression = useCallback(
(expression: string) => {
if (!expression) return true;
const evaluated = executeScriptSync(expression, { data: subFormData, globalState });
return typeof evaluated === 'boolean' ? evaluated : true;
},
[subFormData, globalState]
);

const getReadOnlyState = (isReadOnly: boolean) => (typeof readOnly === 'boolean' ? readOnly : isReadOnly);

return (
<Fragment>
{components
?.filter(({ customVisibility }) => {
return executeExpression(customVisibility);
})
.map(({ customEnabled, disabled: notabled, ...model }) => {
const disabled = !executeExpression(customEnabled) || notabled;

return (
<DynamicComponent
model={{
...model,
isDynamic: true,
readOnly: getReadOnlyState(model?.readOnly),
disabled,
customEnabled: '',
}}
key={model?.id}
/>
);
})}
</Fragment>
);
import { IComponentsContainerBaseProps } from 'interfaces';
import { useGlobalState, useSubForm } from 'providers';
import React from 'react';
import { FC, useCallback } from 'react';
import { executeScriptSync } from 'utils/publicUtils';
import DynamicComponent from '../dynamicView/dynamicComponent';
import { getAlignmentStyle } from 'components/formDesigner/containers/componentsContainerForm';
import { ICommonContainerProps } from 'designer-components/container/interfaces';
import { removeUndefinedProperties } from 'utils/array';

interface IComponentsContainerSubFormProps extends IComponentsContainerBaseProps, ICommonContainerProps {}

export const ComponentsContainerSubForm: FC<IComponentsContainerSubFormProps> = (props) => {
const { containerId, readOnly } = props;
const { getChildComponents } = useSubForm();
const components = getChildComponents(containerId);

const style = getAlignmentStyle(props);

//alias added for readability and avoiding names clashes
const { value: subFormData } = useSubForm();
const { globalState } = useGlobalState();

const executeExpression = useCallback(
(expression: string) => {
if (!expression) return true;
const evaluated = executeScriptSync(expression, { data: subFormData, globalState });
return typeof evaluated === 'boolean' ? evaluated : true;
},
[subFormData, globalState]
);

const getReadOnlyState = (isReadOnly: boolean) => (typeof readOnly === 'boolean' ? readOnly : isReadOnly);

return (
<div style={removeUndefinedProperties(style)}>
{components
?.filter(({ customVisibility }) => {
return executeExpression(customVisibility);
})
.map(({ customEnabled, disabled: notabled, ...model }) => {
const disabled = !executeExpression(customEnabled) || notabled;

return (
<DynamicComponent
model={{
...model,
isDynamic: true,
readOnly: getReadOnlyState(model?.readOnly),
disabled,
customEnabled: '',
}}
key={model?.id}
/>
);
})}
</div>
);
};

ComponentsContainerSubForm.displayName = "ComponentsContainer(SubForm)";
ComponentsContainerSubForm.displayName = 'ComponentsContainer(SubForm)';
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import React, { CSSProperties, FC, PropsWithChildren, useCallback, useMemo } from 'react';
import ConfigurableFormComponent from '../configurableFormComponent';
import { useForm } from 'providers/form';
import {
TOOLBOX_COMPONENT_DROPPABLE_KEY,
TOOLBOX_DATA_ITEM_DROPPABLE_KEY,
} from 'providers/form/models';
import { TOOLBOX_COMPONENT_DROPPABLE_KEY, TOOLBOX_DATA_ITEM_DROPPABLE_KEY } from 'providers/form/models';
import { ItemInterface, ReactSortable } from 'react-sortablejs';
import { joinStringValues } from 'utils';
import DynamicComponent from '../components/dynamicView/dynamicComponent';
Expand All @@ -14,7 +11,7 @@ import { useFormData, useGlobalState } from 'providers';
import { executeScriptSync } from 'utils/publicUtils';
import { IComponentsContainerProps } from './componentsContainer';

export const ComponentsContainerForm: FC<IComponentsContainerProps> = props => {
export const ComponentsContainerForm: FC<IComponentsContainerProps> = (props) => {
const { formMode } = useForm();
const { data: formData } = useFormData();
const designer = useFormDesigner(false);
Expand Down Expand Up @@ -51,7 +48,7 @@ export const ComponentsContainerForm: FC<IComponentsContainerProps> = props => {
return useDesigner ? <ComponentsContainerDesigner {...props} /> : <ComponentsContainerLive {...props} />;
};

ComponentsContainerForm.displayName = "ComponentsContainer(Form)";
ComponentsContainerForm.displayName = 'ComponentsContainer(Form)';

type AlignmentProps = Pick<
IComponentsContainerProps,
Expand All @@ -69,7 +66,7 @@ type AlignmentProps = Pick<
| 'flexWrap'
>;

const getAlignmentStyle = ({
export const getAlignmentStyle = ({
direction = 'vertical',
justifyContent,
alignItems,
Expand All @@ -87,9 +84,7 @@ const getAlignmentStyle = ({
display,
};

const gridTemplateColumns = Array(gridColumnsCount)
.fill('auto')
?.join(' ');
const gridTemplateColumns = Array(gridColumnsCount).fill('auto')?.join(' ');

if (direction === 'horizontal' || display !== 'block') {
style['justifyContent'] = justifyContent;
Expand Down Expand Up @@ -118,7 +113,7 @@ const getAlignmentStyle = ({
return style;
};

const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProps>> = props => {
const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProps>> = (props) => {
const {
containerId,
children,
Expand All @@ -132,21 +127,14 @@ const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProp
} = props;

const { getChildComponentIds } = useForm();
const {
updateChildComponents,
addComponent,
addDataProperty,
startDragging,
endDragging,
readOnly,
hasDragged,
} = useFormDesigner();
const { updateChildComponents, addComponent, addDataProperty, startDragging, endDragging, readOnly, hasDragged } =
useFormDesigner();

const childIds = getChildComponentIds(containerId);

const componentsMapped = useMemo<ItemInterface[]>(() => {
return childIds.map<ItemInterface>(id => ({
id: id
return childIds.map<ItemInterface>((id) => ({
id: id,
}));
}, [childIds]);

Expand All @@ -157,15 +145,14 @@ const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProp
return;
}

const chosen = newState.some(item => item.chosen === true);
if (chosen)
return;
const chosen = newState.some((item) => item.chosen === true);
if (chosen) return;

// temporary commented out, the behavoiur of the sortablejs differs sometimes
const listChanged = true; //!newState.some(item => item.chosen !== null && item.chosen !== undefined);

if (listChanged) {
const newDataItemIndex = newState.findIndex(item => item['type'] === TOOLBOX_DATA_ITEM_DROPPABLE_KEY);
const newDataItemIndex = newState.findIndex((item) => item['type'] === TOOLBOX_DATA_ITEM_DROPPABLE_KEY);
if (newDataItemIndex > -1) {
// dropped data item
const draggedItem = newState[newDataItemIndex];
Expand All @@ -176,7 +163,7 @@ const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProp
index: newDataItemIndex,
});
} else {
const newComponentIndex = newState.findIndex(item => item['type'] === TOOLBOX_COMPONENT_DROPPABLE_KEY);
const newComponentIndex = newState.findIndex((item) => item['type'] === TOOLBOX_COMPONENT_DROPPABLE_KEY);
if (newComponentIndex > -1) {
// add new component
const toolboxComponent = newState[newComponentIndex];
Expand All @@ -200,7 +187,7 @@ const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProp
}

if (isModified) {
const newIds = newState.map<string>(item => item.id.toString());
const newIds = newState.map<string>((item) => item.id.toString());
updateChildComponents({ containerId, componentIds: newIds });
}
}
Expand All @@ -213,7 +200,7 @@ const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProp
startDragging();
};

const onDragEnd = _evt => {
const onDragEnd = (_evt) => {
endDragging();
};

Expand All @@ -230,7 +217,7 @@ const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProp
return (
<ConditionalWrap
condition={!noDefaultStyling}
wrap={content => (
wrap={(content) => (
<div className={joinStringValues(['sha-components-container', direction, className])} style={wrapperStyle}>
{content}
</div>
Expand Down Expand Up @@ -270,7 +257,7 @@ const ComponentsContainerDesigner: FC<PropsWithChildren<IComponentsContainerProp
);
};

const ComponentsContainerLive: FC<PropsWithChildren<IComponentsContainerProps>> = props => {
const ComponentsContainerLive: FC<PropsWithChildren<IComponentsContainerProps>> = (props) => {
const {
containerId,
children,
Expand Down Expand Up @@ -305,4 +292,4 @@ const ComponentsContainerLive: FC<PropsWithChildren<IComponentsContainerProps>>
{children}
</div>
);
};
};
Loading

0 comments on commit 6f104d5

Please sign in to comment.