Skip to content

Commit abfd045

Browse files
authored
Merge pull request #39 from DouglasNeuroInformatics/record-array-initial-values-issue
2 parents b3199a1 + e1e9201 commit abfd045

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/components/Form/Form.stories.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,34 @@ export const ReadOnly: StoryObj<typeof Form<ExampleFormSchemaType>> = {
389389
};
390390

391391
export const WithInitialValues: StoryObj<typeof Form> = {
392+
args: {
393+
content: ungroupedContent,
394+
initialValues: {
395+
booleanCheck: true,
396+
booleanRadio: true,
397+
date: new Date(),
398+
numberInput: 44,
399+
numberSlider: 45,
400+
numberRadio: 3,
401+
numberSelect: 4,
402+
stringSelect: 'a',
403+
setListbox: new Set(['a', 'b']),
404+
setSelect: new Set(['c', 'd']),
405+
showDynamicField: true,
406+
dynamicField: 'Foo',
407+
stringTextArea: 'Lorem ipsum',
408+
stringPassword: 'Password',
409+
stringInput: 'Input',
410+
stringRadio: 'b'
411+
},
412+
onSubmit: (data) => {
413+
alert(JSON.stringify(data, null, 2));
414+
},
415+
validationSchema: $ExampleFormData
416+
}
417+
};
418+
419+
export const WithDynamicInitialValues: StoryObj<typeof Form> = {
392420
decorators: [
393421
(Story) => {
394422
const [initialValues, setInitialValues] = useState<FormTypes.PartialNullableData<ExampleFormData>>({});

src/components/Form/Form.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const Form = <TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<TS
6565
const [rootErrors, setRootErrors] = useState<string[]>([]);
6666
const [errors, setErrors] = useState<FormErrors<TData>>({});
6767
const [values, setValues] = useState<PartialFormDataType<TData>>({});
68+
const [isInitialSetValuesComplete, setIsInitialSetValuesComplete] = useState(false);
6869

6970
const handleError = (error: z.ZodError<TData>) => {
7071
const fieldErrors: FormErrors<TData> = {};
@@ -130,8 +131,13 @@ const Form = <TSchema extends z.ZodType<FormDataType>, TData extends z.TypeOf<TS
130131
if (initialValues) {
131132
setValues(getInitialValues(initialValues));
132133
}
134+
setIsInitialSetValuesComplete(true);
133135
}, [initialValues]);
134136

137+
if (!isInitialSetValuesComplete) {
138+
return null;
139+
}
140+
135141
return (
136142
<form
137143
autoComplete="off"

0 commit comments

Comments
 (0)