mirror of
https://github.com/LukeHagar/form.git
synced 2025-12-10 12:27:45 +00:00
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:
@@ -129,7 +129,7 @@ export class FieldApi<TData, TFormData> {
|
|||||||
|
|
||||||
this.state = this.store.state
|
this.state = this.store.state
|
||||||
this.prevState = this.state
|
this.prevState = this.state
|
||||||
this.update(opts as never)
|
this.options = opts as never
|
||||||
}
|
}
|
||||||
|
|
||||||
mount = () => {
|
mount = () => {
|
||||||
@@ -151,6 +151,7 @@ export class FieldApi<TData, TFormData> {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this.update(this.options as never)
|
||||||
this.options.onMount?.(this as never)
|
this.options.onMount?.(this as never)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -163,26 +164,16 @@ export class FieldApi<TData, TFormData> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
update = (opts: FieldApiOptions<typeof this._tdata, TFormData>) => {
|
update = (opts: FieldApiOptions<typeof this._tdata, TFormData>) => {
|
||||||
this.options = {
|
|
||||||
asyncDebounceMs: this.form.options.asyncDebounceMs ?? 0,
|
|
||||||
...opts,
|
|
||||||
} as never
|
|
||||||
|
|
||||||
// Default Value
|
// Default Value
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
if (this.state.value === undefined) {
|
if (this.state.value === undefined) {
|
||||||
if (this.options.defaultValue !== undefined) {
|
const formDefault =
|
||||||
this.setValue(this.options.defaultValue as never)
|
opts.form.options.defaultValues?.[opts.name as keyof TFormData]
|
||||||
} else if (
|
|
||||||
opts.form.options.defaultValues?.[
|
if (opts.defaultValue !== undefined) {
|
||||||
this.options.name as keyof TFormData
|
this.setValue(opts.defaultValue as never)
|
||||||
] !== undefined
|
} else if (formDefault !== undefined) {
|
||||||
) {
|
this.setValue(formDefault as never)
|
||||||
this.setValue(
|
|
||||||
opts.form.options.defaultValues[
|
|
||||||
this.options.name as keyof TFormData
|
|
||||||
] as never,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,6 +181,8 @@ export class FieldApi<TData, TFormData> {
|
|||||||
if (this._getMeta() === undefined) {
|
if (this._getMeta() === undefined) {
|
||||||
this.setMeta(this.state.meta)
|
this.setMeta(this.state.meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.options = opts as never
|
||||||
}
|
}
|
||||||
|
|
||||||
getValue = (): typeof this._tdata => {
|
getValue = (): typeof this._tdata => {
|
||||||
|
|||||||
@@ -433,4 +433,21 @@ describe('field api', () => {
|
|||||||
await vi.runAllTimersAsync()
|
await vi.runAllTimersAsync()
|
||||||
expect(field.getMeta().error).toBe('Please enter a different value')
|
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')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user