Skip to content

Commit

Permalink
feat: optional default values on useResetForm
Browse files Browse the repository at this point in the history
  • Loading branch information
devcatalin committed Sep 10, 2021
1 parent 628b761 commit e6138c2
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/utils/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ export function useWindowSize(): Size {
}

/** reset antd form fields when modal is closed */
export const useResetFormOnCloseModal = ({form, visible}: {form: FormInstance; visible: boolean}) => {
export const useResetFormOnCloseModal = ({
form,
visible,
defaultValues,
}: {
form: FormInstance;
visible: boolean;
defaultValues?: any;
}) => {
const prevVisibleRef = useRef<boolean>();
useEffect(() => {
prevVisibleRef.current = visible;
Expand All @@ -48,6 +56,9 @@ export const useResetFormOnCloseModal = ({form, visible}: {form: FormInstance; v
if (!visible && prevVisible) {
form.resetFields();
}
if (visible && !prevVisible && defaultValues) {
form.setFieldsValue(defaultValues);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visible]);
};

0 comments on commit e6138c2

Please sign in to comment.