Skip to content

Commit

Permalink
More form cleanup
Browse files Browse the repository at this point in the history
* 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 elastic#75796 is
    merged these will no longer be necessary.
  • Loading branch information
rylnd committed Aug 27, 2020
1 parent 788135b commit 1cb55bf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,16 @@ const StepAboutRuleComponent: FC<StepAboutRuleProps> = ({
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,12 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({

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) {
Expand Down Expand Up @@ -268,11 +266,9 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
fields={{
thresholdField: {
path: 'threshold.field',
defaultValue: initialState.threshold.field,
},
thresholdValue: {
path: 'threshold.value',
defaultValue: initialState.threshold.value,
},
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const StepRuleActionsComponent: FC<StepRuleActionsProps> = ({
options: { stripEmptyFields: false },
schema,
});
const { getFields, reset, submit } = form;
const { getFields, submit } = form;

const kibanaAbsoluteUrl = useMemo(
() =>
Expand All @@ -93,19 +93,20 @@ const StepRuleActionsComponent: FC<StepRuleActionsProps> = ({
[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(() => {
Expand Down Expand Up @@ -160,25 +161,16 @@ const StepRuleActionsComponent: FC<StepRuleActionsProps> = ({
<EuiSpacer />
<UseField
path="actions"
// defaultValue={initialState.actions}
component={RuleActionsField}
componentProps={{
messageVariables: actionMessageParams,
}}
/>
</>
) : (
<UseField
path="actions"
// defaultValue={initialState.actions}
component={GhostFormField}
/>
<UseField path="actions" component={GhostFormField} />
)}
<UseField
path="kibanaSiemAppUrl"
defaultValue={kibanaAbsoluteUrl}
component={GhostFormField}
/>
<UseField path="kibanaSiemAppUrl" component={GhostFormField} />
<UseField path="enabled" component={GhostFormField} />
</EuiForm>
</Form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,23 @@ const StepScheduleRuleComponent: FC<StepScheduleRuleProps> = ({
setForm,
}) => {
const initialState = defaultValues ?? stepScheduleDefaultValue;
// const [myStepData, setMyStepData] = useState<ScheduleStepRule>(initialState);

const { form } = useForm<ScheduleStepRule>({
defaultValue: initialState,
options: { stripEmptyFields: false },
schema,
});
const { reset, submit } = form;
const { submit } = form;

const onSubmit = useCallback(async () => {
if (setStepData) {
// setStepData(RuleStep.scheduleRule, null, false);
const { isValid, data } = await submit();
if (isValid) {
setStepData(RuleStep.scheduleRule, data, isValid);
reset({ defaultValue: data });
}
}
}, [reset, setStepData, submit]);
}, [setStepData, submit]);

useEffect(() => {
if (setForm) {
Expand Down

0 comments on commit 1cb55bf

Please sign in to comment.