Skip to content

Commit

Permalink
[refactor] GH-5 Remove parent
Browse files Browse the repository at this point in the history
  • Loading branch information
amivanoff committed Aug 10, 2021
1 parent d759b02 commit 1a161ed
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 112 deletions.
3 changes: 1 addition & 2 deletions src/DispatchCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { MstContext } from './MstContext';
* Dispatch renderer component for cells.
*/
export const DispatchCell: React.FC<DispatchCellProps> = React.memo(
({ data, onMeasureChange, uri, schema, viewElement, view, enabled, id, parent, CKey, rowData, ...rest }) => {
({ data, onMeasureChange, uri, schema, viewElement, view, enabled, id, CKey, rowData, ...rest }) => {
const { cells } = useContext(MstContext);
const renderer = maxBy(cells, (r) => r.tester(viewElement, schema));
if (renderer === undefined || renderer.tester(viewElement, schema) === -1) {
Expand All @@ -43,7 +43,6 @@ export const DispatchCell: React.FC<DispatchCellProps> = React.memo(
enabled={enabled}
view={view}
id={id}
parent={parent}
{...rest}
/>
</ErrorBoundary>
Expand Down
13 changes: 2 additions & 11 deletions src/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export interface FormsInitStateProps {
}
export interface FormsDispatchProps extends InitStateProps {
enabled?: boolean;
parent?: string;
form?: string;
}
export interface FormDispatchProps extends FormsDispatchProps {
Expand All @@ -87,7 +86,7 @@ export interface DispatchCellProps extends RenderProps {
}

export const FormsDispatch: React.FC<FormDispatchProps> = observer<FormDispatchProps>(
({ view, viewElement, parent, form, uri, enabled }: any) => {
({ view, viewElement, form, uri, enabled }: any) => {
const { store, renderers } = useContext(MstContext);

const shapes = viewElement.resultsScope ? viewElement.resultsScope.split('/') : [];
Expand Down Expand Up @@ -117,15 +116,7 @@ export const FormsDispatch: React.FC<FormDispatchProps> = observer<FormDispatchP
const Render: React.FC<RenderProps> = renderer.renderer;
return (
<ErrorBoundary FallbackComponent={ErrorFallback} onReset={() => {}}>
<Render
schema={schema}
viewElement={viewElement}
enabled={enabled}
view={view}
id={id}
parent={parent}
form={form}
/>
<Render schema={schema} viewElement={viewElement} enabled={enabled} view={view} id={id} form={form} />
</ErrorBoundary>
);
}
Expand Down
12 changes: 1 addition & 11 deletions src/cells/AntdCellHorizontalLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,7 @@ import { Idx } from '../util/layout';

export const AntdHorizontalLayoutRenderer: React.FC<DispatchCellProps> = ({ viewElement, view, data, schema }) => {
//const layout = viewElement as Layout;
const Render: React.FC<DispatchCellProps & Idx> = ({
idx,
schema,
viewElement,
view,
data,
enabled,
parent,
form,
}) => {
const Render: React.FC<DispatchCellProps & Idx> = ({ idx, schema, viewElement, view, data, enabled, form }) => {
const options = viewElement.options || {};
//const style: any = options.style;
const span = options.contentSize || !viewElement.elements ? undefined : Math.ceil(24 / viewElement.elements.length);
Expand All @@ -46,7 +37,6 @@ export const AntdHorizontalLayoutRenderer: React.FC<DispatchCellProps> = ({ view
rowData={data}
schema={newSchema || schema}
enabled={enabled}
parent={parent}
form={form}
/>
</Col>
Expand Down
2 changes: 0 additions & 2 deletions src/layouts/AntdFormLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export const AntdFormLayout: React.FC<any> = ({
enabled,
title,
visible,
parent,
formId,
validation,
editable,
Expand All @@ -101,7 +100,6 @@ export const AntdFormLayout: React.FC<any> = ({
viewElement={viewElement}
enabled={enabled}
view={view}
parent={parent}
form={formId}
/>
</Form>
Expand Down
14 changes: 4 additions & 10 deletions src/layouts/AntdHorizontalLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@ import { renderLayoutElements } from '../util/layout';
import { Idx } from '../util/layout';
import { LayoutComponent } from './LayoutComponent';

export const AntdHorizontalLayoutRenderer: React.FC<LayoutComponent> = ({
viewElement,
view,
enabled,
visible,
parent,
}) => {
export const AntdHorizontalLayoutRenderer: React.FC<LayoutComponent> = ({ viewElement, view, enabled, visible }) => {
//const layout = viewElement as Layout;
const Render: React.FC<FormsDispatchProps & Idx> = ({ idx, viewElement, view, enabled, parent, form }) => {
const Render: React.FC<FormsDispatchProps & Idx> = ({ idx, viewElement, view, enabled, form }) => {
const options = viewElement.options || {};
const style: any = options.style;
const span = options.contentSize || !viewElement.elements ? undefined : Math.ceil(24 / viewElement.elements.length);
return (
<Col key={idx} style={style} span={span}>
<FormsDispatch viewElement={viewElement} view={view} enabled={enabled} parent={parent} form={form} />
<FormsDispatch viewElement={viewElement} view={view} enabled={enabled} form={form} />
</Col>
);
};
Expand All @@ -41,7 +35,7 @@ export const AntdHorizontalLayoutRenderer: React.FC<LayoutComponent> = ({
if (viewElement.options && viewElement.options.width === 'all-empty-space') rowStyle.width = '100%';
return (
<Row justify={justify} style={rowStyle} align={'middle'}>
{renderLayoutElements({ viewElement, view, enabled, Render, parent })}
{renderLayoutElements({ viewElement, view, enabled, Render })}
</Row>
);
};
Expand Down
7 changes: 3 additions & 4 deletions src/layouts/AntdVerticalLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ export const AntdVerticalLayoutRenderer: React.FC<LayoutComponent> = ({
view,
enabled,
visible,
parent,
form,
}) => {
const Render: React.FC<FormsDispatchProps & Idx> = ({ idx, viewElement, view, enabled, parent }) => {
const Render: React.FC<FormsDispatchProps & Idx> = ({ idx, viewElement, view, enabled }) => {
const options = viewElement.options || {};
const style: any = options.style;
return (
Expand All @@ -36,15 +35,15 @@ export const AntdVerticalLayoutRenderer: React.FC<LayoutComponent> = ({
flex: viewElement.options && viewElement.options.height === 'all-empty-space' ? '1 1 auto' : '',
}}>
<Col style={style} span={24}>
<FormsDispatch viewElement={viewElement} view={view} enabled={enabled} parent={parent} form={form} />
<FormsDispatch viewElement={viewElement} view={view} enabled={enabled} form={form} />
</Col>
</Row>
);
};
return (
<React.Fragment>
<div style={{ display: 'flex', flexDirection: 'column', height: '100%' }}>
{renderLayoutElements({ viewElement, view, enabled, Render, parent })}
{renderLayoutElements({ viewElement, view, enabled, Render })}
</div>
</React.Fragment>
);
Expand Down
33 changes: 7 additions & 26 deletions src/layouts/SplitPaneLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,17 @@ const divStyle: React.CSSProperties = {
margin: '1px',
};

const renderSplitElements = ({ viewElement, view, enabled, Render, parent, form }: RenderLayoutProps) => {
const renderSplitElements = ({ viewElement, view, enabled, Render, form }: RenderLayoutProps) => {
const elements = viewElement.elements;
const defaultSize = viewElement.options && viewElement.options.defaultSize;
const id = viewElement['@id'];
const sort = id
? viewElement.properties && viewElement.properties[id] && viewElement.properties[id].order
: undefined;
return sort ? (
sort.reduce((res: ReactElement[], e: string, idx: number) => {
const key = elements ? elements.findIndex((el: ViewElement) => el['@id'] === e || el.scope === e) : -1;
if (key !== -1 && elements) {
const childView = elements[key];
const id = childView['@id'] || childView.scope || '';
const style = childView.options && childView.options.style;
res.push(
<Pane key={idx} style={style} initialSize={defaultSize[id]}>
<FormsDispatch viewElement={childView} view={view} enabled={enabled} parent={parent} form={form} />
</Pane>,
);
}
return res;
}, [])
) : elements ? (
return elements ? (
elements.map((el: ViewElement, idx: number) => {
const id = el['@id'] || el.resultsScope || '';
const style = el.options && el.options.style;
return (
<Pane key={idx} style={style} initialSize={defaultSize[id]}>
<div style={{ height: '100%', ...style }}>
<FormsDispatch viewElement={el} view={view} enabled={enabled} parent={parent} />
<FormsDispatch viewElement={el} view={view} enabled={enabled} />
</div>
</Pane>
);
Expand All @@ -65,19 +46,19 @@ const renderSplitElements = ({ viewElement, view, enabled, Render, parent, form
);
};

export const SplitPaneLayoutRenderer: React.FC<LayoutComponent> = ({ viewElement, view, enabled, visible, parent }) => {
export const SplitPaneLayoutRenderer: React.FC<LayoutComponent> = ({ viewElement, view, enabled, visible }) => {
//const layout = viewElement as Layout;
const Render: React.FC<FormsDispatchProps & Idx> = ({ idx, viewElement, view, enabled, parent }) => {
const Render: React.FC<FormsDispatchProps & Idx> = ({ idx, viewElement, view, enabled }) => {
return (
<div>
<FormsDispatch viewElement={viewElement} view={view} enabled={enabled} parent={parent} />
<FormsDispatch viewElement={viewElement} view={view} enabled={enabled} />
</div>
);
};
return (
<React.Fragment>
<SplitPane split='vertical' style={divStyle} minSize={300}>
{renderSplitElements({ viewElement, view, enabled, Render, parent })}
{renderSplitElements({ viewElement, view, enabled, Render })}
</SplitPane>
</React.Fragment>
);
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/TabsLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const TabsLayout: React.FC<any> = (props) => {
const title = e.options && e.options.title;
return (
<Tabs.TabPane tab={title} key={'' + index}>
<FormsDispatch viewElement={e} enabled={enabled} view={view} parent={viewElement.resultsScope} />
<FormsDispatch viewElement={e} enabled={enabled} view={view} />
</Tabs.TabPane>
);
})
Expand Down
10 changes: 1 addition & 9 deletions src/util/AntdModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,7 @@ export const AntdModal: React.FC<any> = observer<any>(
cancelText='Отмена'
width={1200}
okText='Сохранить'>
<Render
schema={schema}
viewElement={viewElement}
enabled={enabled}
view={view}
cells={cells}
id={childrenId}
parent={id}
/>
<Render schema={schema} viewElement={viewElement} enabled={enabled} view={view} cells={cells} id={childrenId} />
<SaveReqDialog
visible={visible}
onOk={() => {
Expand Down
58 changes: 24 additions & 34 deletions src/util/ContextToProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const withStoreToControlProps = (Component: React.FC<ControlComponent>):
);
});
export const withStoreToFormProps = (Component: React.FC<any>): React.FC<RenderProps> =>
observer<RenderProps>(({ viewElement, view, enabled, parent, form }) => {
observer<RenderProps>(({ viewElement, view, enabled, form }) => {
if (!view['@id']) {
return null;
}
Expand All @@ -124,7 +124,6 @@ export const withStoreToFormProps = (Component: React.FC<any>): React.FC<RenderP
view={view}
enabled={enabledLayout}
visible={visible}
parent={parent}
onSave={() => store.onSaveFormData(id)}
editing={store.editingData.get(id)}
onEdit={() => store.setEditing(id, true)}
Expand All @@ -148,7 +147,7 @@ export const withStoreToViewClassProps = (Component: any): any =>

export const withStoreToViewProps = (Component: any): any =>
observer<any>(({ ...props }: any) => {
const { view, viewElement, parent } = props;
const { view, viewElement } = props;
const { store } = useContext(MstContext);
const scope = viewElement.resultsScope;
const coll = store.getColl(scope);
Expand All @@ -160,8 +159,8 @@ export const withStoreToViewProps = (Component: any): any =>
return <Spin />;
}
data = getSnapshot(data);
//if (parent && viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(parent, viewElement.resultsScope);
//if (viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(viewElement.resultsScope);
//}
//const id = store.getSelectedDataJs(scope)['@type'];
//const selection = getSnapshot(store.selectedData);
Expand All @@ -187,10 +186,10 @@ export const withStoreToModalProps = (Component: any): any =>
});
export const withStoreToButtonProps = (Component: any): any =>
observer<any>(({ ...props }: any) => {
const { schema, viewElement, parent } = props;
const { schema, viewElement } = props;
const { store } = useContext(MstContext);
if (parent && viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
store.setSaveLogic(parent, viewElement.resultsScope);
if (viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
store.setSaveLogic(viewElement.resultsScope);
}
const options = viewElement.options || {};

Expand Down Expand Up @@ -224,10 +223,10 @@ export const withStoreToCellProps = (Component: React.FC<any>): React.FC<any> =>

export const withStoreToDataControlProps = (Component: any): any =>
observer<any>(({ ...props }: any) => {
const { viewElement, view, parent } = props;
const { viewElement, view } = props;
const { store } = useContext(MstContext);
//if (parent && viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(parent, viewElement.resultsScope);
//if (viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(viewElement.resultsScope);
//}
const custom = view[viewElement.resultsScope.split('/')[0]]
? view[view.resultsScope.split('/')[0]].customReq
Expand Down Expand Up @@ -328,10 +327,10 @@ export const withStoreToSelectControlProps = (Component: any): any =>

export const withStoreToTabProps = (Component: any): any =>
observer<any>(({ ...props }: any) => {
const { schema, viewElement, view, parent } = props;
const { schema, viewElement, view } = props;
const { store } = useContext(MstContext);
//if (parent && viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(parent, viewElement.resultsScope);
//if (viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(viewElement.resultsScope);
//}
const options = viewElement.options || {};
const custom = view[viewElement.resultsScope.split('/')[0]]
Expand Down Expand Up @@ -361,10 +360,10 @@ export const withStoreToTabProps = (Component: any): any =>

export const withStoreToMenuProps = (Component: any): any =>
observer<any>(({ ...props }: any) => {
const { schema, viewElement, view, parent } = props;
const { schema, viewElement, view } = props;
const { store } = useContext(MstContext);
//if (parent && viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(parent, viewElement.resultsScope);
//if (viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(viewElement.resultsScope);
//}
const options = viewElement.options || {};
const custom = view[viewElement.resultsScope.split('/')[0]]
Expand Down Expand Up @@ -401,10 +400,10 @@ export const withStoreToCollapseProps = (Component: any): any =>

export const withStoreToArrayProps = (Component: any): any =>
observer<any>(({ ...props }: any) => {
const { schema, viewElement, view, parent } = props;
const { schema, viewElement, view } = props;
const { store } = useContext(MstContext);
//if (parent && viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(parent, viewElement.resultsScope);
//if (viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
// store.setSaveLogic(viewElement.resultsScope);
//}
const options = viewElement.options || {};
const custom = view[viewElement.resultsScope.split('/')[0]]
Expand Down Expand Up @@ -454,31 +453,22 @@ export const withStoreToArrayProps = (Component: any): any =>
});

export const withLayoutProps = (Component: React.FC<LayoutComponent>): React.FC<RenderProps> =>
observer<RenderProps>(({ viewElement, view, enabled, form, parent }) => {
observer<RenderProps>(({ viewElement, view, enabled, form }) => {
const id = viewElement['@id'] || '';
const enabledLayout = enabled && checkProperty('editable', id, viewElement, view);
const visible = checkProperty('visible', id, viewElement, view);
const { store } = useContext(MstContext);
if (viewElement.options && viewElement.options.connections) {
viewElement.options.connections.forEach((e: any) => store.setSaveLogic(e.from, e.to));
}
return (
<Component
viewElement={viewElement}
view={view}
enabled={enabledLayout}
visible={visible}
parent={parent}
form={form}
/>
);
return <Component viewElement={viewElement} view={view} enabled={enabledLayout} visible={visible} form={form} />;
});

export const withStoreToSaveButtonProps = (Component: React.FC<ButtonComponent>): React.FC<RenderProps> =>
observer<RenderProps>(({ viewElement, view, enabled, parent }) => {
observer<RenderProps>(({ viewElement, view, enabled }) => {
const { store } = useContext(MstContext);
if (parent && viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
store.setSaveLogic(parent, viewElement.resultsScope);
if (viewElement.resultsScope && !store.saveLogicTree[viewElement.resultsScope]) {
store.setSaveLogic(viewElement.resultsScope);
}
const key = viewElement.resultsScope;
return (
Expand Down
Loading

0 comments on commit 1a161ed

Please sign in to comment.