Skip to content

Commit

Permalink
FIX label issue with optional input
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Jul 10, 2024
1 parent 23ff37a commit c805815
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
52 changes: 49 additions & 3 deletions src/form/controlledInput.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -62,6 +65,7 @@ export const ControlledInput = (inputProps: Props) => {
name: entry
})
const { getValues, setValue, formState: { errors } } = useFormContext();
const [isAdded, setIsAdded] = React.useState<boolean>(!!field.value)

//@ts-ignore
const error: { [x: string]: any } = entry.split('.').reduce((acc, curr) => acc && acc[curr], errors)
Expand Down Expand Up @@ -103,13 +107,55 @@ export const ControlledInput = (inputProps: Props) => {
value: field.value,
}

if (step.optional && isAdded) {
return (
<div className='mrf-ai_center mrf-mt_5' style={{ position: 'relative' }}>
<div style={{ width: '95%' }}>
<BasicWrapper key={`collapse-${entry}`} entry={entry} realEntry={realEntry} functionalProperty={functionalProperty} step={step} render={inputWrapper} informations={informations}>
<CustomizableInput
render={step.render}
step={step}
field={{ setValue: (key: string, value: any) => 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 })}
</CustomizableInput>
</BasicWrapper>
</div>
<button type="button"
style={{ position: 'absolute', top: '2px', right: 0 }}
className='mrf-btn mrf-btn_red mrf-btn_sm mrf-ml_5'
onClick={() => {
setIsAdded(false)
setValue(entry, null)
}}>
<Trash2 size={16} />
</button>
</div>
)
} else if (step.optional) {
return (
<BasicWrapper key={`collapse-${entry}`} entry={entry} realEntry={realEntry} functionalProperty={functionalProperty} step={step} render={inputWrapper} informations={informations}>
<div className='mrf-flex mrf-jc_flex_end'>
<button type="button"
className={classNames('mrf-btn', 'mrf-btn_blue', 'mrf-btn_sm', 'mrf-mt_5')}
onClick={() => {
setIsAdded(true)
//todo: setup la valeur par defaut ??
}}>Add</button>
</div>
</BasicWrapper>
)
}

return <BasicWrapper key={`collapse-${entry}`} entry={entry} realEntry={realEntry} functionalProperty={functionalProperty} step={step} render={inputWrapper} informations={informations}>
<CustomizableInput
render={step.render}
render={step.render}
step={step}
field={{ setValue: (key: string, value: any) => 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 })}
</CustomizableInput>
Expand Down
6 changes: 0 additions & 6 deletions src/form/step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ export const Step = (props: {
}
}

if (step.optional) {
return <OptionalStep {...props} />
}

if (step.array) {
return (
<ControlledInput step={step} entry={entry} realEntry={realEntry} errorDisplayed={errorDisplayed} informations={informations} deactivateReactMemo={deactivateReactMemo} inputWrapper={inputWrapper} defaultFormValue={defaultFormValue} >
Expand Down Expand Up @@ -659,8 +655,6 @@ const OptionalStep = (props: {
const { setValue } = useFormContext()


console.debug({props, isDefined: isAdded})

if (isAdded) {
return (
<div className='mrf-ai_center mrf-mt_5' style={{ position: 'relative' }}>
Expand Down

0 comments on commit c805815

Please sign in to comment.