Skip to content

Commit

Permalink
fix: form ref getter
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 3, 2024
1 parent c79777f commit 4257ba1
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/components/ui/form/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/form/FormInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const FormInput: FC<
useAddField({
rules: rules || [],
transform,
$ref: inputRef.current,
getEl: () => inputRef.current,
name,
})
const resetFieldStatus = useResetFieldStatus(name)
Expand Down
11 changes: 7 additions & 4 deletions src/components/ui/form/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface Field {
*/
transform?: <X, T = string>(value: T) => X

$ref: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null
getEl: () => HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | null
}

export interface FormFieldBaseProps<T> extends Pick<Field, 'transform'> {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const Input = forwardRef<
<input
ref={ref}
className={clsxm(
'min-w-0 flex-auto appearance-none rounded-lg border ring-accent/20 duration-200 sm:text-sm',
'min-w-0 flex-auto appearance-none rounded-lg border ring-accent/20 duration-200 sm:text-sm lg:text-base',
'bg-base-100 px-3 py-[calc(theme(spacing.2)-1px)] placeholder:text-zinc-400 focus:outline-none focus:ring-2 dark:bg-zinc-700/[0.15]',
'border-border',
'focus:border-accent/80 dark:text-zinc-200 dark:placeholder:text-zinc-500',
Expand Down
1 change: 1 addition & 0 deletions src/components/ui/input/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const TextArea = forwardRef<
'overflow-auto px-3 py-4',
'!outline-none',
'text-neutral-900/80 dark:text-slate-100/80',
roundedMap[rounded],
className,
)}
{...rest}
Expand Down

0 comments on commit 4257ba1

Please sign in to comment.