Skip to content

Commit

Permalink
[form_lib] Small refactor for formDefaultValue
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Jul 13, 2020
1 parent 52ee926 commit 5fbb648
Showing 1 changed file with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,18 @@ interface UseFormReturn<T extends FormData> {
export function useForm<T extends FormData = FormData>(
formConfig?: FormConfig<T>
): UseFormReturn<T> {
const { onSubmit, schema, serializer, deserializer, options, id = 'default' } = formConfig ?? {};
const { onSubmit, schema, serializer, deserializer, options, id = 'default', defaultValue } =
formConfig ?? {};

const formDefaultValue = useMemo(() => {
if (formConfig === undefined) {
if (defaultValue === undefined || Object.keys(defaultValue).length === 0) {
return {};
}

const hasDefaultValue =
formConfig.defaultValue !== undefined && Object.keys(formConfig.defaultValue).length > 0;

return hasDefaultValue
? Object.entries(formConfig!.defaultValue as object)
.filter(({ 1: value }) => value !== undefined)
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {})
: {};
}, [formConfig]);
return Object.entries(defaultValue as object)
.filter(({ 1: value }) => value !== undefined)
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
}, [defaultValue]);

const { errorDisplayDelay, stripEmptyFields: doStripEmptyFields } = options ?? {};
const formOptions = useMemo(
Expand Down

0 comments on commit 5fbb648

Please sign in to comment.