Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
vassbence committed Nov 7, 2024
1 parent e450ef9 commit d6f3a3f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
21 changes: 0 additions & 21 deletions src/components/Form/Form.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,6 @@ export default {
},
}

export const Test = () => {
const form = useForm({
onSubmit: (v) => {
console.log(v)
},
})

console.count('test rerender')

return (
<div>
<input
value={getByPath(form.values, ['nested', 'test', 'foo'])}
onChange={(e) =>
form.setValue(['nested', 'test', 'foo'], e.target.value)
}
/>
</div>
)
}

export const Nested = () => {
return (
<Form
Expand Down
15 changes: 1 addition & 14 deletions src/components/Form/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
type FormValues = { [key: string]: string | boolean | number | null }
type FormErrors = { [key: string]: string }
type FormState = {
values: FormValues
changes: FormValues
mergedValues: FormValues
errors: FormErrors
isSubmitting: boolean
isValidating: boolean
isDirty: boolean
prevInitialValues: FormValues
failedAtLeastOneValidation: boolean
numberOfInFlightValidations: number
isDisabledFromOutside: boolean
}

export type { FormValues, FormErrors, FormState }
export type { FormValues, FormErrors }
16 changes: 14 additions & 2 deletions src/components/Form/useForm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useEffect, useRef } from 'react'
import { useRerender } from '../../hooks/useRerender.js'
import { FormErrors, FormState, FormValues } from './types.js'
import { FormErrors, FormValues } from './types.js'
import { deepEqual, deepMerge, Path, setByPath } from '@saulx/utils'

type UseFormProps = {
Expand All @@ -19,7 +19,19 @@ function useForm({
disabled,
}: UseFormProps) {
const rerender = useRerender()
const state = useRef<FormState>({
const state = useRef<{
values: FormValues
changes: FormValues
mergedValues: FormValues
errors: FormErrors
isSubmitting: boolean
isValidating: boolean
isDirty: boolean
prevInitialValues: FormValues
failedAtLeastOneValidation: boolean
numberOfInFlightValidations: number
isDisabledFromOutside: boolean
}>({
values: initialValues ?? {},
changes: {},
mergedValues: initialValues ?? {},
Expand Down

0 comments on commit d6f3a3f

Please sign in to comment.