diff --git a/src/form/controlledInput.tsx b/src/form/controlledInput.tsx index 787e958..cbab49a 100644 --- a/src/form/controlledInput.tsx +++ b/src/form/controlledInput.tsx @@ -1,5 +1,8 @@ import * as React from "react"; import { ChangeEvent } from 'react'; +import classNames from "classnames"; +// @ts-ignore +import Trash2 from 'react-feather/dist/icons/trash-2.js'; import { useController, useFormContext } from 'react-hook-form'; import { BasicWrapper } from "./basicWrapper"; import { option } from '../Option'; @@ -62,6 +65,7 @@ export const ControlledInput = (inputProps: Props) => { name: entry }) const { getValues, setValue, formState: { errors } } = useFormContext(); + const [isAdded, setIsAdded] = React.useState(!!field.value) //@ts-ignore const error: { [x: string]: any } = entry.split('.').reduce((acc, curr) => acc && acc[curr], errors) @@ -103,13 +107,55 @@ export const ControlledInput = (inputProps: Props) => { value: field.value, } + if (step.optional && isAdded) { + return ( +
+
+ + setValue(key, value), rawValues: getValues(), getValue: (key: string) => getValues(key), ...field }} + error={error} errorDisplayed={errorDisplayed} + informations={informations} + deactivateReactMemo={deactivateReactMemo}> + {component ? component(field, props) : React.cloneElement(children!, { ...props })} + + +
+ +
+ ) + } else if (step.optional) { + return ( + +
+ +
+
+ ) + } + return setValue(key, value), rawValues: getValues(), getValue: (key: string) => getValues(key), ...field }} - error={error} errorDisplayed={errorDisplayed} - informations={informations} + error={error} errorDisplayed={errorDisplayed} + informations={informations} deactivateReactMemo={deactivateReactMemo}> {component ? component(field, props) : React.cloneElement(children!, { ...props })} diff --git a/src/form/step.tsx b/src/form/step.tsx index 34f1278..7526cae 100644 --- a/src/form/step.tsx +++ b/src/form/step.tsx @@ -170,10 +170,6 @@ export const Step = (props: { } } - if (step.optional) { - return - } - if (step.array) { return ( @@ -659,8 +655,6 @@ const OptionalStep = (props: { const { setValue } = useFormContext() - console.debug({props, isDefined: isAdded}) - if (isAdded) { return (