From 9ed3fc94b55ac53dcc27f1f75284e368942c8911 Mon Sep 17 00:00:00 2001 From: Toma Rosa Date: Thu, 8 Aug 2024 16:13:45 +0200 Subject: [PATCH 1/4] feat: add field api transformOnSubmit option --- packages/form-core/src/FieldApi.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/form-core/src/FieldApi.ts b/packages/form-core/src/FieldApi.ts index 337b38d8a..943c75919 100644 --- a/packages/form-core/src/FieldApi.ts +++ b/packages/form-core/src/FieldApi.ts @@ -302,6 +302,8 @@ export interface FieldOptions< * An optional object with default metadata for the field. */ defaultMeta?: Partial + + transformOnSubmit?: (value: TData) => unknown; } /** From 93ca0a8a9bab01bf9826efcb0cd75d9ff28671a6 Mon Sep 17 00:00:00 2001 From: Toma Rosa Date: Thu, 8 Aug 2024 16:17:04 +0200 Subject: [PATCH 2/4] feat: add form api applyFieldsTransformation function and call it just before onSubmit execution --- packages/form-core/src/FormApi.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 017735b07..b4a23f9cf 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -516,6 +516,26 @@ export class FormApi< ) } + applyFieldsTransformation = () => { + this.store.batch(() => { + void ( + Object.values(this.fieldInfo) as FieldInfo[] + ).forEach((field) => { + if (!field.instance) return + const fieldInstance = field.instance + + // Apply transformation + const transformOnSubmit = fieldInstance.options.transformOnSubmit + if (!transformOnSubmit) return + + const transformedValue = transformOnSubmit(field.instance.getValue()) + if (!transformedValue) return + + fieldInstance.setValue(transformedValue) + }) + }) + } + /** * Validates all fields in the form using the correct handlers for a given validation type. */ @@ -810,6 +830,9 @@ export class FormApi< } try { + // Apply transformOnSubmit function + this.applyFieldsTransformation(); + // Run the submit code await this.options.onSubmit?.({ value: this.state.values, formApi: this }) From 19f50a2b68fbff3934d1f6d59197a74f5a4d2663 Mon Sep 17 00:00:00 2001 From: Toma Rosa Date: Thu, 8 Aug 2024 16:17:45 +0200 Subject: [PATCH 3/4] test: field value transformation on submit --- packages/form-core/tests/FieldApi.spec.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/form-core/tests/FieldApi.spec.ts b/packages/form-core/tests/FieldApi.spec.ts index 56a415b19..fa332e178 100644 --- a/packages/form-core/tests/FieldApi.spec.ts +++ b/packages/form-core/tests/FieldApi.spec.ts @@ -1329,4 +1329,24 @@ describe('field api', () => { 'other validation error', ) }) + + it('should apply field transformation on submit', async () => { + const form = new FormApi({ + defaultValues: { + id: '1', + }, + }) + + const field = new FieldApi({ + form, + name: 'id', + transformOnSubmit: (v) => parseInt(v) + }) + + field.mount(); + + expect(field.getValue()).toBe("1") + await form.handleSubmit(); + expect(field.getValue()).toBe(1) + }) }) From 3e9987fff7ad04a8d9f7540df2e31c0dd61dbd98 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 8 Aug 2024 14:27:58 +0000 Subject: [PATCH 4/4] ci: apply automated fixes --- packages/form-core/src/FieldApi.ts | 2 +- packages/form-core/src/FormApi.ts | 2 +- packages/form-core/tests/FieldApi.spec.ts | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/form-core/src/FieldApi.ts b/packages/form-core/src/FieldApi.ts index 943c75919..677dc51e1 100644 --- a/packages/form-core/src/FieldApi.ts +++ b/packages/form-core/src/FieldApi.ts @@ -303,7 +303,7 @@ export interface FieldOptions< */ defaultMeta?: Partial - transformOnSubmit?: (value: TData) => unknown; + transformOnSubmit?: (value: TData) => unknown } /** diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index b4a23f9cf..dfd55dca2 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -831,7 +831,7 @@ export class FormApi< try { // Apply transformOnSubmit function - this.applyFieldsTransformation(); + this.applyFieldsTransformation() // Run the submit code await this.options.onSubmit?.({ value: this.state.values, formApi: this }) diff --git a/packages/form-core/tests/FieldApi.spec.ts b/packages/form-core/tests/FieldApi.spec.ts index fa332e178..d30a4bfbc 100644 --- a/packages/form-core/tests/FieldApi.spec.ts +++ b/packages/form-core/tests/FieldApi.spec.ts @@ -1340,13 +1340,13 @@ describe('field api', () => { const field = new FieldApi({ form, name: 'id', - transformOnSubmit: (v) => parseInt(v) + transformOnSubmit: (v) => parseInt(v), }) - field.mount(); + field.mount() - expect(field.getValue()).toBe("1") - await form.handleSubmit(); + expect(field.getValue()).toBe('1') + await form.handleSubmit() expect(field.getValue()).toBe(1) }) })