diff --git a/src/form.js b/src/form.js index 445a3ee..fc01f1f 100644 --- a/src/form.js +++ b/src/form.js @@ -32,7 +32,7 @@ const usePrevious = (value) => { return ref.current; } -const BasicWrapper = ({ entry, className, label, help, children, render, collapsed }) => { +const BasicWrapper = ({ entry, className, label, help, children, render }) => { if (typeof entry === 'object') { return children } @@ -47,16 +47,9 @@ const BasicWrapper = ({ entry, className, label, help, children, render, collaps const errorDisplayed = formState.isSubmitted || isDirty || isTouched if (render) { - return render({ entry, label, error, help, children, collapsed }) + return render({ entry, label, error, help, children }) } - const childrenWithProps = React.Children.map(children, child => { - if (React.isValidElement(child)) { - return React.cloneElement(child, { collapsed }); - } - return child; - }); - return (
{label && } - {childrenWithProps} + {children} {error &&
{error.message}
}
) @@ -223,6 +216,8 @@ export const Form = React.forwardRef(({ schema, flow, value, inputWrapper, onSub }) } + const [expandedAll, setExpandedAll] = useState(false) + const defaultValues = getDefaultValues(formFlow, schema, value); //FIXME: get real schema through the switch @@ -293,6 +288,19 @@ export const Form = React.forwardRef(({ schema, flow, value, inputWrapper, onSub const clean = cleanOutputArray(data, schema) onSubmit(clean) }, onError)}> +
+ +
{formFlow.map((entry, idx) => { const step = schema[entry] @@ -327,7 +335,8 @@ export const Form = React.forwardRef(({ schema, flow, value, inputWrapper, onSub + httpClient={maybeCustomHttpClient} functionalProperty={functionalProperty} + expandedAll={expandedAll} /> ) })} @@ -355,14 +364,14 @@ const Footer = (props) => { ) } -const Step = ({ entry, realEntry, step, schema, inputWrapper, httpClient, defaultValue, index, functionalProperty, parent, onAfterChange, collapsed }) => { +const Step = ({ entry, realEntry, step, schema, inputWrapper, httpClient, defaultValue, index, functionalProperty, parent, onAfterChange, expandedAll }) => { const classes = useCustomStyle(); const { formState: { errors, dirtyFields, touchedFields, isSubmitted }, control, trigger, getValues, setValue, watch, register } = useFormContext(); if (entry && typeof entry === 'object') { const errored = entry.flow.some(step => !!errors[step] && (dirtyFields[step] || touchedFields[step])) return ( - + {entry.flow.map((en, entryIdx) => { const stp = schema[en] const err = typeof en === 'object' ? undefined : en.split('.').reduce((object, key) => { @@ -394,9 +403,9 @@ const Step = ({ entry, realEntry, step, schema, inputWrapper, httpClient, defaul } return ( - + @@ -458,7 +467,7 @@ const Step = ({ entry, realEntry, step, schema, inputWrapper, httpClient, defaul value={props.value} index={idx} functionalProperty={functionalProperty} - collapsed={collapsed} /> + expandedAll={expandedAll} /> ) })} /> @@ -579,9 +588,9 @@ const Step = ({ entry, realEntry, step, schema, inputWrapper, httpClient, defaul return ( setValue(key, value), rawValues: getValues(), value: getValues(entry), onChange: v => setValue(entry, v, { shouldValidate: true }) }}> + index={index} functionalProperty={functionalProperty} errorDisplayed={errorDisplayed} expandedAll={expandedAll} /> ) @@ -755,16 +764,15 @@ const ArrayStep = ({ entry, step, component, disabled }) => { ) } -const NestedForm = ({ schema, flow, parent, inputWrapper, maybeCustomHttpClient, errorDisplayed, value, step, functionalProperty, index, ...props }) => { +const NestedForm = ({ schema, flow, parent, inputWrapper, maybeCustomHttpClient, errorDisplayed, value, step, functionalProperty, index, expandedAll }) => { const { getValues, setValue, watch, trigger, formState } = useFormContext(); - const [collapsed, setCollapsed] = useState(!!step.collapsed || props.collapsed); - - useEffect(() => { - setCollapsed(props.collapsed) - }, [props.collapsed]) - + const [collapsed, setCollapsed] = useState(!!step.collapsed); const classes = useCustomStyle(); + useEffect(() => { + if (!expandedAll || (expandedAll && parent.split('.').length >= 2)) + setCollapsed(expandedAll) + }, [expandedAll]) const schemaAndFlow = option(step.conditionalSchema) .map(condiSchema => { @@ -842,7 +850,8 @@ const NestedForm = ({ schema, flow, parent, inputWrapper, maybeCustomHttpClient, inputWrapper={inputWrapper} httpClient={maybeCustomHttpClient} defaultValue={value && value[entry]} - functionalProperty={functionalProperty} /> + functionalProperty={functionalProperty} + expandedAll={expandedAll} /> ) })} diff --git a/src/inputs/Collapse.js b/src/inputs/Collapse.js index 484be72..6dade25 100644 --- a/src/inputs/Collapse.js +++ b/src/inputs/Collapse.js @@ -5,7 +5,6 @@ import { useCustomStyle } from '../styleContext' export const Collapse = (props) => { const [collapsed, setCollapsed] = useState(props.initCollapsed || props.collapsed) - const [expandedAll, setExpandedAll] = useState(false) const classes = useCustomStyle() const toggle = (e) => { @@ -13,29 +12,11 @@ export const Collapse = (props) => { setCollapsed(!collapsed) }; - const childrenWithProps = React.Children.map(props.children, child => { - if (React.isValidElement(child)) { - return React.cloneElement(child, { collapsed: expandedAll }); - } - return child; - }); - return (

{props.label} -
- -
- {childrenWithProps} + {props.children}
{props.lineEnd &&
}