From 5fbb64890d6c0caa30b9466b50ce6468b857b364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81bastien=20Loix?= Date: Mon, 13 Jul 2020 12:58:22 +0200 Subject: [PATCH] [form_lib] Small refactor for formDefaultValue --- .../forms/hook_form_lib/hooks/use_form.ts | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/plugins/es_ui_shared/static/forms/hook_form_lib/hooks/use_form.ts b/src/plugins/es_ui_shared/static/forms/hook_form_lib/hooks/use_form.ts index e9858d49145bf..66ace3ac3098d 100644 --- a/src/plugins/es_ui_shared/static/forms/hook_form_lib/hooks/use_form.ts +++ b/src/plugins/es_ui_shared/static/forms/hook_form_lib/hooks/use_form.ts @@ -36,22 +36,18 @@ interface UseFormReturn { export function useForm( formConfig?: FormConfig ): UseFormReturn { - 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(