From 3dfb4fc27aa0a9eb7f3302fb15883cfd9588e1dc Mon Sep 17 00:00:00 2001 From: anoop narang Date: Mon, 20 Jul 2020 16:22:43 +0530 Subject: [PATCH] [INFRA-421] | Anoop | Hide sibling folds when a FoldingGroup is opened --- src/components/manifest/SchemaForm.tsx | 34 ++++++++++++++------------ src/renderer/FoldableGroup.tsx | 22 +++++++++++++++-- src/renderer/FoldableGroupContext.tsx | 21 ++++++++++++++++ 3 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/renderer/FoldableGroupContext.tsx diff --git a/src/components/manifest/SchemaForm.tsx b/src/components/manifest/SchemaForm.tsx index fe21183..c8abb77 100644 --- a/src/components/manifest/SchemaForm.tsx +++ b/src/components/manifest/SchemaForm.tsx @@ -12,6 +12,7 @@ import { materialCells, materialRenderers } from '@jsonforms/material-renderers' import customAjv from '../../ajv/CustomAjv' import _ from 'lodash' import { FoldableGroupTester, FoldableGroupRenderer } from '../../renderer/FoldableGroup' +import { FoldableGroupContextProvider } from '../../renderer/FoldableGroupContext' interface IProps { csrfToken?: any name: string @@ -66,6 +67,7 @@ export default function SchemaForm(props: IProps) { // eslint-disable-next-line react-hooks/exhaustive-deps }, []) + return ( ( !isLoading ? @@ -82,21 +84,23 @@ export default function SchemaForm(props: IProps) { { (!props.updateCompleteSchemaHandler && isProperJsonSchema(completeSchema)) ? - { - if(props.onDataChange) { - props.onDataChange(data) - } - }} /> + + { + if (props.onDataChange) { + props.onDataChange(data) + } + }} /> + : null } diff --git a/src/renderer/FoldableGroup.tsx b/src/renderer/FoldableGroup.tsx index 9d0d712..22db709 100644 --- a/src/renderer/FoldableGroup.tsx +++ b/src/renderer/FoldableGroup.tsx @@ -10,9 +10,11 @@ import { } from "@material-ui/core"; import ExpandMoreIcon from "@material-ui/icons/ExpandMore"; import React from "react"; +import { FoldableGroupContext } from './FoldableGroupContext'; const FoldableGroup = props => { const { uischema, schema, path, visible, renderers} = props; + const { foldableGroupState, foldableGroupDispatch } = React.useContext(FoldableGroupContext) const layoutProps : MaterialLayoutRendererProps = { elements: uischema.elements, @@ -20,13 +22,29 @@ const FoldableGroup = props => { path: path, direction: 'column', visible: visible, - uischema: uischema, renderers: renderers }; + let expansionPanelProps = {} + + if(uischema.parentId) { + const currentExpandedPanel = foldableGroupState[uischema.parentId] + expansionPanelProps = { + expanded: currentExpandedPanel === uischema.id, + onChange: (event, isExpanded) => { + if(isExpanded) { + foldableGroupDispatch({ type: 'EXPAND', key: uischema.parentId, value: uischema.id }) + } + else { + foldableGroupDispatch({ type: 'SHRINK', key: uischema.parentId }) + } + } + } + } + return ( - + }> {uischema.label} diff --git a/src/renderer/FoldableGroupContext.tsx b/src/renderer/FoldableGroupContext.tsx new file mode 100644 index 0000000..e7ff9bf --- /dev/null +++ b/src/renderer/FoldableGroupContext.tsx @@ -0,0 +1,21 @@ +import React, { createContext, useReducer } from "react"; + +const FoldableGroupContext = createContext({}) + +const reducer = (state, action) => { + switch(action.type) { + case 'EXPAND': + return { ...state, [action.key]: action.value } + case 'SHRINK': + delete state[action.key] + return {...state} + } +}; + +const FoldableGroupContextProvider = (props) => { + const [state, dispatch] = useReducer(reducer, []); + + return {props.children} ; +}; + +export { FoldableGroupContext, FoldableGroupContextProvider };