Skip to content

Commit f449dec

Browse files
committed
clean array output for all rawValues usage
1 parent b73fa49 commit f449dec

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

src/form/arrayStep.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import classNames from 'classnames';
44
import Trash2 from 'react-feather/dist/icons/trash-2.js';
55
import { useFormContext, useFieldArray } from "react-hook-form";
66

7-
import { SchemaEntry } from "./types";
7+
import { Schema, SchemaEntry } from "./types";
88
import { option } from '../Option';
99
import { type } from '../type';
10-
import { defaultVal, getDefaultValues } from './formUtils';
10+
import { cleanOutputArray, defaultVal, getDefaultValues } from './formUtils';
1111

12-
export const ArrayStep = ({ entry, step, component, disabled, addLabel }:
12+
export const ArrayStep = ({ entry, step, component, disabled, addLabel, schema }:
1313
{
1414
entry: string,
1515
step: SchemaEntry,
1616
component: ({ key, defaultValue, value }: { key: string, defaultValue: any, value?: any }, ids: number) => JSX.Element,
1717
disabled: boolean,
18-
addLabel?: string
18+
schema: Schema,
19+
addLabel?: string,
1920
}) => {
2021
const { getValues, setValue, control, trigger, formState } = useFormContext();
2122

@@ -45,7 +46,7 @@ export const ArrayStep = ({ entry, step, component, disabled, addLabel }:
4546
onClick={() => {
4647
remove(idx)
4748
option(step.onChange)
48-
.map(onChange => onChange({ rawValues: getValues(), value: getValues(entry), setValue }))
49+
.map(onChange => onChange({ rawValues: cleanOutputArray(getValues(), schema), value: getValues(entry), setValue }))
4950
trigger(entry);
5051
}}>
5152
<Trash2 size={16} />
@@ -62,7 +63,7 @@ export const ArrayStep = ({ entry, step, component, disabled, addLabel }:
6263
append({ value: step.addableDefaultValue || defaultValues })
6364
// trigger(entry);
6465
option(step.onChange)
65-
.map(onChange => onChange({ rawValues: getValues(), value: getValues(entry), setValue }))
66+
.map(onChange => onChange({ rawValues: cleanOutputArray(getValues(), schema), value: getValues(entry), setValue }))
6667
}} disabled={disabled}>{addLabel ? addLabel : 'Add'}</button>
6768
</div>
6869
</>

src/form/controlledInput.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { BasicWrapper } from "./basicWrapper";
88
import { option } from '../Option';
99
import { type } from '../type';
1010
import { cleanHash, isDefined } from '../utils';
11-
import { ConditionnalSchema, Informations, SchemaEntry, SchemaRenderType } from "./types";
11+
import { ConditionnalSchema, Informations, Schema, SchemaEntry, SchemaRenderType } from "./types";
12+
import { cleanOutputArray } from "./formUtils";
1213

1314
const CustomizableInput = React.memo(
1415
(props: {
@@ -46,6 +47,7 @@ interface BaseProps {
4647
deactivateReactMemo: boolean,
4748
inputWrapper?: (props: object) => JSX.Element,
4849
defaultFormValue: any
50+
schema: Schema
4951
}
5052

5153
interface ComponentProps extends BaseProps {
@@ -73,7 +75,7 @@ export const ControlledInput = (inputProps: Props) => {
7375

7476
const functionalProperty = (entry: string, prop: any) => {
7577
if (typeof prop === 'function') {
76-
return prop({ rawValues: getValues(), value: getValues(entry), defaultFormValue, informations, error, getValue: (key: string) => getValues(key) });
78+
return prop({ rawValues: cleanOutputArray(getValues(), inputProps.schema), value: getValues(entry), defaultFormValue, informations, error, getValue: (key: string) => getValues(key) });
7779
} else {
7880
return prop;
7981
}
@@ -102,7 +104,7 @@ export const ControlledInput = (inputProps: Props) => {
102104
})()
103105
field.onChange(value)
104106
option(step.onChange)
105-
.map(onChange => onChange({ rawValues: getValues(), value, setValue }))
107+
.map(onChange => onChange({ rawValues: cleanOutputArray(getValues(), inputProps.schema), value, setValue }))
106108
},
107109
value: field.value,
108110
}
@@ -153,7 +155,7 @@ export const ControlledInput = (inputProps: Props) => {
153155
<CustomizableInput
154156
render={step.render}
155157
step={step}
156-
field={{ setValue: (key: string, value: any) => setValue(key, value), rawValues: getValues(), getValue: (key: string) => getValues(key), ...field }}
158+
field={{ setValue: (key: string, value: any) => setValue(key, value), rawValues: cleanOutputArray(getValues(), inputProps.schema), getValue: (key: string) => getValues(key), ...field }}
157159
error={error} errorDisplayed={errorDisplayed}
158160
informations={informations}
159161
deactivateReactMemo={deactivateReactMemo}>

src/form/form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const FormComponent = <T extends TBaseObject>(props: FormProps<T>, ref: React.Re
9595
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 => {
9696
if (typeof prop === 'function') {
9797

98-
return (prop as Function)({ rawValues: getValues(), value: getValues(entry), defaultFormValue: value, informations, getValue: (key: string) => getValues(key), error });
98+
return (prop as Function)({ rawValues: cleanOutputArray(getValues(), schema), value: getValues(entry), defaultFormValue: value, informations, getValue: (key: string) => getValues(key), error });
9999
} else {
100100
return prop;
101101
}

0 commit comments

Comments
 (0)