fix: Field default should now be set properly (#445)

* fix: fields should now set default value properly

* test: add test to validate fix
This commit is contained in:
Corbin Crutchley
2023-09-07 15:55:17 -07:00
committed by GitHub
parent 6ffa574a66
commit d80bcf7a96
2 changed files with 28 additions and 18 deletions

View File

@@ -129,7 +129,7 @@ export class FieldApi<TData, TFormData> {
this.state = this.store.state
this.prevState = this.state
this.update(opts as never)
this.options = opts as never
}
mount = () => {
@@ -151,6 +151,7 @@ export class FieldApi<TData, TFormData> {
})
})
this.update(this.options as never)
this.options.onMount?.(this as never)
return () => {
@@ -163,26 +164,16 @@ export class FieldApi<TData, TFormData> {
}
update = (opts: FieldApiOptions<typeof this._tdata, TFormData>) => {
this.options = {
asyncDebounceMs: this.form.options.asyncDebounceMs ?? 0,
...opts,
} as never
// Default Value
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (this.state.value === undefined) {
if (this.options.defaultValue !== undefined) {
this.setValue(this.options.defaultValue as never)
} else if (
opts.form.options.defaultValues?.[
this.options.name as keyof TFormData
] !== undefined
) {
this.setValue(
opts.form.options.defaultValues[
this.options.name as keyof TFormData
] as never,
)
const formDefault =
opts.form.options.defaultValues?.[opts.name as keyof TFormData]
if (opts.defaultValue !== undefined) {
this.setValue(opts.defaultValue as never)
} else if (formDefault !== undefined) {
this.setValue(formDefault as never)
}
}
@@ -190,6 +181,8 @@ export class FieldApi<TData, TFormData> {
if (this._getMeta() === undefined) {
this.setMeta(this.state.meta)
}
this.options = opts as never
}
getValue = (): typeof this._tdata => {

View File

@@ -433,4 +433,21 @@ describe('field api', () => {
await vi.runAllTimersAsync()
expect(field.getMeta().error).toBe('Please enter a different value')
})
it('should handle default value on field using state.value', async () => {
interface Form {
name: string
}
const form = new FormApi<Form>()
const field = new FieldApi({
form,
name: 'name',
defaultValue: 'test',
})
field.mount()
expect(field.state.value).toBe('test')
})
})