Skip to content

Commit

Permalink
Cleanup <FormAutoSubmit/> and remove internal hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
paales committed Feb 15, 2024
1 parent 646db64 commit 855ab09
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-radios-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/react-hook-form': patch
---

Cleanup `<FormAutoSubmit/>` and remove internal hook.
40 changes: 10 additions & 30 deletions packages/react-hook-form/src/useFormAutoSubmit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { useMemoObject } from '@graphcommerce/next-ui/hooks/useMemoObject'
import { debounce } from '@mui/material'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import {
Control,
DeepPartialSkipArrayKey,
FieldPath,
FieldValues,
UseFormReturn,
UseWatchProps,
useFormState,
useWatch,
} from 'react-hook-form'
Expand Down Expand Up @@ -98,21 +98,8 @@ export function useFormAutoSubmit<
return submitting
}

export type FormAutoSubmitProps<
TFieldValues extends FieldValues = FieldValues,
TFieldNames extends readonly FieldPath<TFieldValues>[] = readonly FieldPath<TFieldValues>[],
> = {
// eslint-disable-next-line react/no-unused-prop-types
control: Control<TFieldValues>
export type FormAutoSubmitProps<TFieldValues extends FieldValues = FieldValues> = {
/** Autosubmit only when these field names update */
// eslint-disable-next-line react/no-unused-prop-types
name?: readonly [...TFieldNames]

// eslint-disable-next-line react/no-unused-prop-types
disabled?: boolean

// eslint-disable-next-line react/no-unused-prop-types
exact?: boolean

/** SubmitHandler */
// eslint-disable-next-line react/no-unused-prop-types
Expand All @@ -124,12 +111,15 @@ export type FormAutoSubmitProps<
*/
// eslint-disable-next-line react/no-unused-prop-types
parallel?: boolean
} & DebounceOptions
} & DebounceOptions &
Omit<UseWatchProps<TFieldValues>, 'defaultValue'>

function useFormAutoSubmit2<
TFieldValues extends FieldValues = FieldValues,
TFieldNames extends readonly FieldPath<TFieldValues>[] = readonly FieldPath<TFieldValues>[],
>(props: FormAutoSubmitProps<TFieldValues, TFieldNames>) {
/**
* This is made a components so the useWatch that is used here doesn't retrigger the rerender of the parent component.
*/
function FormAutoSubmitBase<TFieldValues extends FieldValues = FieldValues>(
props: FormAutoSubmitProps<TFieldValues>,
) {
const { wait, initialWait, maxWait, submit, parallel, ...watchOptions } = props

// We create a stable object from the values, so that we can compare them later
Expand Down Expand Up @@ -157,17 +147,7 @@ function useFormAutoSubmit2<
// eslint-disable-next-line @typescript-eslint/no-floating-promises
submitDebounced()
}
}

/**
* We're wrapping this in a component so that the parent component doesn't rerender on every
* submission.
*/
function FormAutoSubmitBase<
TFieldValues extends FieldValues = FieldValues,
TFieldNames extends readonly FieldPath<TFieldValues>[] = readonly FieldPath<TFieldValues>[],
>(props: FormAutoSubmitProps<TFieldValues, TFieldNames>) {
useFormAutoSubmit2(props)
return null
}

Expand Down

0 comments on commit 855ab09

Please sign in to comment.