From 1cb55bf19bb3f06da916d5ec8c948088c90ed754 Mon Sep 17 00:00:00 2001 From: Ryland Herrick Date: Wed, 26 Aug 2020 16:08:27 -0500 Subject: [PATCH] More form cleanup * Removes redundant uses of field's defaultValue * Most are redundant with the form's defaultValue; added calls to field.setValue in cases where the default is generated internally * Removes calls to reset() after submitting * These were needed due to a bug in the form lib; once #75796 is merged these will no longer be necessary. --- .../rules/step_about_rule/index.tsx | 5 ++-- .../rules/step_define_rule/index.tsx | 6 +---- .../rules/step_rule_actions/index.tsx | 24 +++++++------------ .../rules/step_schedule_rule/index.tsx | 6 ++--- 4 files changed, 13 insertions(+), 28 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/index.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/index.tsx index 1f7120f8c8922..96c909ebc6542 100644 --- a/x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/index.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/rules/step_about_rule/index.tsx @@ -86,17 +86,16 @@ const StepAboutRuleComponent: FC = ({ options: { stripEmptyFields: false }, schema, }); - const { getFields, reset, submit } = form; + const { getFields, submit } = form; const onSubmit = useCallback(async () => { if (setStepData) { const { isValid, data } = await submit(); if (isValid) { setStepData(RuleStep.aboutRule, data, isValid); - reset({ defaultValue: data }); } } - }, [reset, setStepData, submit]); + }, [setStepData, submit]); useEffect(() => { if (setForm) { diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/step_define_rule/index.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/step_define_rule/index.tsx index aa2b96ddb4e70..c08e33932c730 100644 --- a/x-pack/plugins/security_solution/public/detections/components/rules/step_define_rule/index.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/rules/step_define_rule/index.tsx @@ -126,14 +126,12 @@ const StepDefineRuleComponent: FC = ({ const onSubmit = useCallback(async () => { if (setStepData) { - // setStepData(RuleStep.defineRule, null, false); // TODO why do we set to null? const { isValid, data } = await submit(); if (isValid && setStepData) { setStepData(RuleStep.defineRule, data, isValid); - reset({ defaultValue: data }); // TODO we have to reset our form to its values post-submission } } - }, [reset, setStepData, submit]); + }, [setStepData, submit]); useEffect(() => { if (setForm) { @@ -268,11 +266,9 @@ const StepDefineRuleComponent: FC = ({ fields={{ thresholdField: { path: 'threshold.field', - defaultValue: initialState.threshold.field, }, thresholdValue: { path: 'threshold.value', - defaultValue: initialState.threshold.value, }, }} > diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/step_rule_actions/index.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/step_rule_actions/index.tsx index a8b5c3565dd9f..39807fcb72b1e 100644 --- a/x-pack/plugins/security_solution/public/detections/components/rules/step_rule_actions/index.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/rules/step_rule_actions/index.tsx @@ -83,7 +83,7 @@ const StepRuleActionsComponent: FC = ({ options: { stripEmptyFields: false }, schema, }); - const { getFields, reset, submit } = form; + const { getFields, submit } = form; const kibanaAbsoluteUrl = useMemo( () => @@ -93,19 +93,20 @@ const StepRuleActionsComponent: FC = ({ [application] ); + useEffect(() => { + getFields().kibanaSiemAppUrl.setValue(kibanaAbsoluteUrl); + }, [getFields, kibanaAbsoluteUrl]); + const onSubmit = useCallback( async (enabled: boolean) => { if (setStepData) { - // setStepData(RuleStep.ruleActions, null, false); const { isValid, data } = await submit(); if (isValid) { setStepData(RuleStep.ruleActions, { ...data, enabled }, isValid); - reset({ defaultValue: { ...data, enabled } }); - // setMyStepData(data as ActionsStepRule); } } }, - [reset, setStepData, submit] + [setStepData, submit] ); useEffect(() => { @@ -160,7 +161,6 @@ const StepRuleActionsComponent: FC = ({ = ({ /> ) : ( - + )} - + diff --git a/x-pack/plugins/security_solution/public/detections/components/rules/step_schedule_rule/index.tsx b/x-pack/plugins/security_solution/public/detections/components/rules/step_schedule_rule/index.tsx index 99dc887bef00c..fbd876d2eba05 100644 --- a/x-pack/plugins/security_solution/public/detections/components/rules/step_schedule_rule/index.tsx +++ b/x-pack/plugins/security_solution/public/detections/components/rules/step_schedule_rule/index.tsx @@ -38,14 +38,13 @@ const StepScheduleRuleComponent: FC = ({ setForm, }) => { const initialState = defaultValues ?? stepScheduleDefaultValue; - // const [myStepData, setMyStepData] = useState(initialState); const { form } = useForm({ defaultValue: initialState, options: { stripEmptyFields: false }, schema, }); - const { reset, submit } = form; + const { submit } = form; const onSubmit = useCallback(async () => { if (setStepData) { @@ -53,10 +52,9 @@ const StepScheduleRuleComponent: FC = ({ const { isValid, data } = await submit(); if (isValid) { setStepData(RuleStep.scheduleRule, data, isValid); - reset({ defaultValue: data }); } } - }, [reset, setStepData, submit]); + }, [setStepData, submit]); useEffect(() => { if (setForm) {