diff --git a/src/components/ui/form/Form.tsx b/src/components/ui/form/Form.tsx index f72074a354..2e8342e2d6 100644 --- a/src/components/ui/form/Form.tsx +++ b/src/components/ui/form/Form.tsx @@ -33,7 +33,7 @@ export const Form = forwardRef< getCurrentValues: () => { return Object.fromEntries( Object.entries(jotaiStore.get(fieldsAtom)).map(([key, value]) => { - const nextValue = (value as any as Field).$ref?.value + const nextValue = (value as any as Field).getEl()?.value return [ key, @@ -90,7 +90,7 @@ const FormInternal = ( const fields = jotaiStore.get(fieldsAtom) for await (const [key, field] of Object.entries(fields)) { - const $ref = field.$ref + const $ref = field.getEl() if (!$ref) continue const value = $ref.value const rules = field.rules diff --git a/src/components/ui/form/FormInput.tsx b/src/components/ui/form/FormInput.tsx index fca81d0896..988f35c801 100644 --- a/src/components/ui/form/FormInput.tsx +++ b/src/components/ui/form/FormInput.tsx @@ -29,7 +29,7 @@ export const FormInput: FC< useAddField({ rules: rules || [], transform, - $ref: inputRef.current, + getEl: () => inputRef.current, name, }) const resetFieldStatus = useResetFieldStatus(name) diff --git a/src/components/ui/form/hooks.tsx b/src/components/ui/form/hooks.tsx index bfe4397b87..c2beb60972 100644 --- a/src/components/ui/form/hooks.tsx +++ b/src/components/ui/form/hooks.tsx @@ -4,6 +4,8 @@ import { useAtomValue, useStore } from 'jotai' import { selectAtom } from 'jotai/utils' import type { Field } from './types' +import { useEventCallback } from '~/hooks/common/use-event-callback' + import { useForm } from './FormContext' const useAssetFormContext = () => { @@ -34,26 +36,27 @@ export const useFormErrorMessage = (name: string) => { export const useAddField = ({ rules, transform, - $ref, + getEl: getRef, name, }: Field & { name: string }) => { const FormCtx = useAssetFormContext() const { addField, removeField } = FormCtx + const stableGetEl = useEventCallback(getRef) useEffect(() => { if (!rules) return if (!name) return addField(name, { rules, - $ref, + getEl: stableGetEl, transform, }) return () => { removeField(name) } - }, [$ref, addField, name, removeField, rules, transform]) + }, [addField, stableGetEl, name, removeField, rules, transform]) } export const useResetFieldStatus = (name: string) => { @@ -80,7 +83,7 @@ export const useCheckFieldStatus = (name: string) => { jotaiStore.set(fields, (p) => { return produce(p, (draft) => { if (!name) return - const value = draft[name].$ref?.value + const value = draft[name].getEl()?.value if (!value) return draft[name].rules.some((rule) => { const result = rule.validator(value) diff --git a/src/components/ui/form/types.ts b/src/components/ui/form/types.ts index 479d80e9b1..1cc5f4a089 100644 --- a/src/components/ui/form/types.ts +++ b/src/components/ui/form/types.ts @@ -14,7 +14,7 @@ export interface Field { */ transform?: (value: T) => X - $ref: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null + getEl: () => HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null } export interface FormFieldBaseProps extends Pick { diff --git a/src/components/ui/input/Input.tsx b/src/components/ui/input/Input.tsx index 44015929a7..d49fb51dbb 100644 --- a/src/components/ui/input/Input.tsx +++ b/src/components/ui/input/Input.tsx @@ -18,7 +18,7 @@ export const Input = forwardRef<