Skip to content

Commit

Permalink
Fix local form data when form isn't mounted
Browse files Browse the repository at this point in the history
If the form isn't on the page (e.g. if we're read-only), then
useFormData will return no values. In these cases, we can simply fall
back to the initialState values, as they'll either be: the default
values on a new form, or: the current values on an active create/edit
form.

Updates the manual type of useFormData to reflect this "maybe" fact.
  • Loading branch information
rylnd committed Sep 3, 2020
1 parent bf8e5c5 commit 36ba03f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
schema,
});
const { getFields, reset, submit } = form;
const [{ index, ruleType }] = (useFormData<DefineStepRule>({
const [{ index: formIndex, ruleType: formRuleType }] = (useFormData({
form,
watch: ['index', 'ruleType'],
}) as unknown) as [DefineStepRule];
}) as unknown) as [Partial<DefineStepRule>];
const index = formIndex || initialState.index;
const ruleType = formRuleType || initialState.ruleType;
const [
{ browserFields, indexPatterns: indexPatternQueryBar, isLoading: indexPatternLoadingQueryBar },
] = useFetchIndexPatterns(index, RuleStep.defineRule);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ const StepRuleActionsComponent: FC<StepRuleActionsProps> = ({
schema,
});
const { getFields, submit } = form;
const [{ throttle }] = (useFormData<ActionsStepRule>({
const [{ throttle: formThrottle }] = (useFormData({
form,
watch: ['throttle'],
}) as unknown) as [ActionsStepRule];
}) as unknown) as [Partial<ActionsStepRule>];
const throttle = formThrottle || initialState.throttle;

const handleSubmit = useCallback(
(enabled: boolean) => {
Expand Down

0 comments on commit 36ba03f

Please sign in to comment.