Skip to content

Commit

Permalink
FIX #107
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Apr 24, 2024
1 parent a6a9fd8 commit 50e60d2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/form/controlledInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface BaseProps {
informations: Informations,
deactivateReactMemo: boolean,
inputWrapper?: (props: object) => JSX.Element,
defaultFormValue: any
}

interface ComponentProps extends BaseProps {
Expand All @@ -55,7 +56,7 @@ interface ChildrenProps extends BaseProps {
type Props = ComponentProps | ChildrenProps

export const ControlledInput = (inputProps: Props) => {
const { step, entry, realEntry, children, component, errorDisplayed = false, informations, deactivateReactMemo, inputWrapper } = inputProps;
const { step, entry, realEntry, children, component, errorDisplayed = false, informations, deactivateReactMemo, inputWrapper, defaultFormValue } = inputProps;
const { field } = useController({
defaultValue: isDefined(step.defaultValue) ? step.defaultValue : null,
name: entry
Expand All @@ -68,7 +69,7 @@ export const ControlledInput = (inputProps: Props) => {

const functionalProperty = (entry: string, prop: any) => {
if (typeof prop === 'function') {
return prop({ rawValues: getValues(), value: getValues(entry), informations, error, getValue: (key: string) => getValues(key) });
return prop({ rawValues: getValues(), value: getValues(entry), defaultFormValue, informations, error, getValue: (key: string) => getValues(key) });
} else {
return prop;
}
Expand Down
11 changes: 6 additions & 5 deletions src/form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type FormProps<DataType> = {
export interface FormRef {
handleSubmit: () => void,
trigger: () => void,
methods: UseFormReturn & { data: () => any}
methods: UseFormReturn & { data: () => any }
}

const FormComponent = <T extends TBaseObject>(props: FormProps<T>, ref: React.Ref<FormRef>) => {
Expand Down Expand Up @@ -95,7 +95,7 @@ const FormComponent = <T extends TBaseObject>(props: FormProps<T>, ref: React.Re
const functionalProperty = <T,>(entry: string, prop: T | ((param: { rawValues: { [x: string]: any }, value: any, informations?: Informations, getValue: (key: string) => any }) => T), informations?: Informations, error?: { [x: string]: any }): T => {
if (typeof prop === 'function') {

return (prop as Function)({ rawValues: getValues(), value: getValues(entry), informations, getValue: (key: string) => getValues(key), error });
return (prop as Function)({ rawValues: getValues(), value: getValues(entry), defaultFormValue: value, informations, getValue: (key: string) => getValues(key), error });
} else {
return prop;
}
Expand Down Expand Up @@ -137,7 +137,8 @@ const FormComponent = <T extends TBaseObject>(props: FormProps<T>, ref: React.Re
inputWrapper={inputWrapper}
httpClient={maybeCustomHttpClient}
functionalProperty={functionalProperty}
stepsOptions={{addLabel: props.options?.actions?.add?.label}}
stepsOptions={{ addLabel: props.options?.actions?.add?.label }}
defaultFormValue={value}
/>
)
}
Expand All @@ -151,10 +152,10 @@ const FormComponent = <T extends TBaseObject>(props: FormProps<T>, ref: React.Re

const informations = { path: entry, key: entry }
return (
<Step key={idx} entry={entry} step={step}
<Step defaultFormValue={value} key={idx} entry={entry} step={step}
schema={schema} inputWrapper={inputWrapper}
httpClient={maybeCustomHttpClient} functionalProperty={functionalProperty}
informations={informations} options={{addLabel: props.options?.actions?.add?.label}}/>
informations={informations} options={{ addLabel: props.options?.actions?.add?.label }} />
)
})}
<Footer render={footer} reset={() => reset(defaultValues)} valid={handleSubmit(data => onSubmit(cleanOutputArray(data, schema)), onError)} actions={options.actions} />
Expand Down
Loading

0 comments on commit 50e60d2

Please sign in to comment.