Skip to content

Commit 7980336

Browse files
committed
feat: remove getInputProps to support React Native
1 parent 2a31bd8 commit 7980336

File tree

6 files changed

+54
-84
lines changed

6 files changed

+54
-84
lines changed

docs/reference/fieldApi.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ title: Field API
55

66
### Creating a new FieldApi Instance
77

8-
9-
108
Normally, you will not need to create a new `FieldApi` instance directly. Instead, you will use a framework hook/function like `useField` or `createField` to create a new instance for you that utilizes your frameworks reactivity model. However, if you need to create a new instance manually, you can do so by calling the `new FieldApi` constructor.
119

1210
```tsx
@@ -28,55 +26,58 @@ An object type representing the options for a field in a form.
2826
- ```tsx
2927
defaultMeta?: Partial<FieldMeta>
3028
```
29+
3130
- An optional object with default metadata for the field.
3231

3332
- ```tsx
3433
onMount?: (formApi: FieldApi<TData, TFormData>) => void
3534
```
35+
3636
- An optional function that takes a param of `formApi` which is a generic type of `TData` and `TFormData`
3737

3838
- ```tsx
3939
onChange?: ValidateFn<TData, TFormData>
4040
```
41+
4142
- An optional property that takes a `ValidateFn` which is a generic of `TData` and `TFormData`
4243

4344
- ```tsx
4445
onChangeAsync?: ValidateAsyncFn<TData, TFormData>
4546
```
46-
- An optional property similar to `onChange` but async validation
4747

48+
- An optional property similar to `onChange` but async validation
4849

4950
- ```tsx
5051
onChangeAsyncDebounceMs?: number
5152
```
52-
- An optional number to represent how long the `onChangeAsync` should wait before running
53+
54+
- An optional number to represent how long the `onChangeAsync` should wait before running
5355
- If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds
5456

5557
- ```tsx
5658
onBlur?: ValidateFn<TData, TFormData>
5759
```
60+
5861
- An optional function, when that run when subscribing to blur event of input
5962

6063
- ```tsx
6164
onBlurAsync?: ValidateAsyncFn<TData, TFormData>
6265
```
66+
6367
- An optional function that takes a `ValidateFn` which is a generic of `TData` and `TFormData` happens async
6468

6569
```tsx
6670
onBlurAsyncDebounceMs?: number
6771
```
68-
- An optional number to represent how long the `onBlurAsyncDebounceMs` should wait before running
69-
- If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds
72+
73+
- An optional number to represent how long the `onBlurAsyncDebounceMs` should wait before running
74+
- If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds
7075

7176
```tsx
7277
onSubmitAsync?: number
7378
```
74-
- If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds
75-
76-
77-
78-
7979

80+
- If set to a number larger than 0, will debounce the async validation event by this length of time in milliseconds
8081

8182
### `ValidationCause`
8283

@@ -205,13 +206,13 @@ A class representing the API for managing a form field.
205206
```
206207
- Validates the field value.
207208
- ```tsx
208-
getChangeProps<T extends UserChangeProps<any>>(props: T = {} as T): ChangeProps<TData> & Omit<T, keyof ChangeProps<TData>>
209+
handleBlur(): void;
209210
```
210-
- Gets the change and blur event handlers.
211+
- Handles the blur event.
211212
- ```tsx
212-
getInputProps<T extends UserInputProps>(props: T = {} as T): InputProps & Omit<T, keyof InputProps>
213+
handleChange(value: TData): void
213214
```
214-
- Gets the input event handlers.
215+
- Handles the change event.
215216
216217
### `FieldState<TData>`
217218

packages/form-core/src/FieldApi.ts

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,6 @@ export type FieldMeta = {
5757
isValidating: boolean
5858
}
5959

60-
export type UserChangeProps<TData> = {
61-
onChange?: (updater: Updater<TData>) => void
62-
onBlur?: (event: any) => void
63-
}
64-
65-
export type UserInputProps = {
66-
onChange?: (event: any) => void
67-
onBlur?: (event: any) => void
68-
}
69-
70-
export type ChangeProps<TData> = {
71-
value: TData
72-
onChange: (value: TData) => void
73-
onBlur: (event: any) => void
74-
}
75-
76-
export type InputProps<T> = {
77-
value: T
78-
onChange: (event: any) => void
79-
onBlur: (event: any) => void
80-
}
81-
8260
let uid = 0
8361

8462
export type FieldState<TData> = {
@@ -409,43 +387,17 @@ export class FieldApi<TData, TFormData> {
409387
return this.validateAsync(value, cause)
410388
}
411389

412-
getChangeProps = <T extends UserChangeProps<any>>(
413-
props: T = {} as T,
414-
): ChangeProps<typeof this._tdata> &
415-
Omit<T, keyof ChangeProps<typeof this._tdata>> => {
416-
return {
417-
...props,
418-
value: this.state.value,
419-
onChange: (value) => {
420-
this.setValue(value as never)
421-
props.onChange?.(value)
422-
},
423-
onBlur: (e) => {
424-
const prevTouched = this.state.meta.isTouched
425-
this.setMeta((prev) => ({ ...prev, isTouched: true }))
426-
if (!prevTouched) {
427-
this.validate('change')
428-
}
429-
this.validate('blur')
430-
},
431-
} as ChangeProps<typeof this._tdata> &
432-
Omit<T, keyof ChangeProps<typeof this._tdata>>
390+
handleChange = (updater: Updater<typeof this._tdata>) => {
391+
this.setValue(updater, { touch: true })
433392
}
434393

435-
getInputProps = <T extends UserInputProps>(
436-
props: T = {} as T,
437-
): InputProps<typeof this._tdata> &
438-
Omit<T, keyof InputProps<typeof this._tdata>> => {
439-
return {
440-
...props,
441-
value: this.state.value,
442-
onChange: (e) => {
443-
this.setValue(e.target.value)
444-
props.onChange?.(e.target.value)
445-
},
446-
onBlur: this.getChangeProps(props).onBlur,
447-
} as InputProps<typeof this._tdata> &
448-
Omit<T, keyof InputProps<typeof this._tdata>>
394+
handleBlur = () => {
395+
const prevTouched = this.state.meta.isTouched
396+
if (!prevTouched) {
397+
this.setMeta((prev) => ({ ...prev, isTouched: true }))
398+
this.validate('change')
399+
}
400+
this.validate('blur')
449401
}
450402
}
451403

packages/react-form/src/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export type {
2-
ChangeProps,
32
DeepKeys,
43
DeepValue,
54
FieldApiOptions,
@@ -9,12 +8,9 @@ export type {
98
FieldState,
109
FormOptions,
1110
FormState,
12-
InputProps,
1311
RequiredByKey,
1412
Updater,
1513
UpdaterFn,
16-
UserChangeProps,
17-
UserInputProps,
1814
ValidationCause,
1915
ValidationError,
2016
ValidationMeta,

packages/react-form/src/tests/createFormFactory.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference lib="dom" />
12
import { render } from '@testing-library/react'
23
import '@testing-library/jest-dom'
34
import * as React from 'react'
@@ -25,7 +26,7 @@ describe('createFormFactory', () => {
2526
<form.Field
2627
name="firstName"
2728
children={(field) => {
28-
return <p>{field.getInputProps().value}</p>
29+
return <p>{field.state.value}</p>
2930
}}
3031
/>
3132
</form.Provider>

packages/react-form/src/tests/useField.test.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference lib="dom" />
12
import * as React from 'react'
23
import { render, waitFor } from '@testing-library/react'
34
import userEvent from '@testing-library/user-event'
@@ -26,7 +27,12 @@ describe('useField', () => {
2627
defaultValue="FirstName"
2728
children={(field) => {
2829
return (
29-
<input data-testid="fieldinput" {...field.getInputProps()} />
30+
<input
31+
data-testid="fieldinput"
32+
value={field.state.value}
33+
onBlur={field.handleBlur}
34+
onChange={(e) => field.handleChange(e.target.value)}
35+
/>
3036
)
3137
}}
3238
/>
@@ -61,7 +67,9 @@ describe('useField', () => {
6167
<input
6268
data-testid="fieldinput"
6369
name={field.name}
64-
{...field.getInputProps()}
70+
value={field.state.value}
71+
onBlur={field.handleBlur}
72+
onChange={(e) => field.setValue(e.target.value)}
6573
/>
6674
<p>{field.getMeta().error}</p>
6775
</div>
@@ -100,7 +108,9 @@ describe('useField', () => {
100108
<input
101109
data-testid="fieldinput"
102110
name={field.name}
103-
{...field.getInputProps()}
111+
value={field.state.value}
112+
onBlur={field.handleBlur}
113+
onChange={(e) => field.handleChange(e.target.value)}
104114
/>
105115
<p>{field.getMeta().error}</p>
106116
</div>
@@ -143,7 +153,9 @@ describe('useField', () => {
143153
<input
144154
data-testid="fieldinput"
145155
name={field.name}
146-
{...field.getInputProps()}
156+
value={field.state.value}
157+
onBlur={field.handleBlur}
158+
onChange={(e) => field.handleChange(e.target.value)}
147159
/>
148160
<p>{field.getMeta().error}</p>
149161
</div>
@@ -189,7 +201,9 @@ describe('useField', () => {
189201
<input
190202
data-testid="fieldinput"
191203
name={field.name}
192-
{...field.getInputProps()}
204+
value={field.state.value}
205+
onBlur={field.handleBlur}
206+
onChange={(e) => field.handleChange(e.target.value)}
193207
/>
194208
<p>{field.getMeta().error}</p>
195209
</div>

packages/react-form/src/tests/useForm.test.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// <reference lib="dom" />
12
import { render } from '@testing-library/react'
23
import userEvent from '@testing-library/user-event'
34
import '@testing-library/jest-dom'
@@ -25,7 +26,12 @@ describe('useForm', () => {
2526
defaultValue={''}
2627
children={(field) => {
2728
return (
28-
<input data-testid="fieldinput" {...field.getInputProps()} />
29+
<input
30+
data-testid="fieldinput"
31+
value={field.state.value}
32+
onBlur={field.handleBlur}
33+
onChange={(e) => field.handleChange(e.target.value)}
34+
/>
2935
)
3036
}}
3137
/>
@@ -61,7 +67,7 @@ describe('useForm', () => {
6167
<form.Field
6268
name="firstName"
6369
children={(field) => {
64-
return <p>{field.getInputProps().value}</p>
70+
return <p>{field.state.value}</p>
6571
}}
6672
/>
6773
</form.Provider>

0 commit comments

Comments
 (0)