Skip to content

Commit

Permalink
refactor!: move submission function to take object instead of two arg…
Browse files Browse the repository at this point in the history
…uments
  • Loading branch information
crutchcorn committed Dec 5, 2023
1 parent ccb1891 commit 1dc351e
Show file tree
Hide file tree
Showing 18 changed files with 48 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docs/framework/react/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function App() {
defaultValues: {
fullName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ values }) => {
// Do something with form data
console.log(values)
},
Expand Down
4 changes: 2 additions & 2 deletions examples/react/simple/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export default function App() {
firstName: "",
lastName: "",
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values);
console.log(value);
},
});

Expand Down
4 changes: 2 additions & 2 deletions examples/react/valibot/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default function App() {
firstName: "",
lastName: "",
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values);
console.log(value);
},
// Add a validator to support Valibot usage in Form and Field
validatorAdapter: valibotValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/react/yup/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default function App() {
firstName: "",
lastName: "",
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values);
console.log(value);
},
// Add a validator to support Yup usage in Form and Field
validatorAdapter: yupValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/react/zod/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default function App() {
firstName: "",
lastName: "",
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values);
console.log(value);
},
// Add a validator to support Zod usage in Form and Field
validatorAdapter: zodValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/solid/simple/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ function App() {
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values)
console.log(value)
},
}))

Expand Down
4 changes: 2 additions & 2 deletions examples/solid/valibot/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function App() {
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values)
console.log(value)
},
// Add a validator to support Valibot usage in Form and Field
validatorAdapter: valibotValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/solid/yup/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function App() {
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values)
console.log(value)
},
// Add a validator to support Yup usage in Form and Field
validatorAdapter: yupValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/solid/zod/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function App() {
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
console.log(values)
console.log(value)
},
// Add a validator to support Zod usage in Form and Field
validatorAdapter: zodValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/vue/simple/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const form = useForm({
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
alert(JSON.stringify(values))
alert(JSON.stringify(value))
},
})
Expand Down
4 changes: 2 additions & 2 deletions examples/vue/valibot/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const form = useForm({
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
alert(JSON.stringify(values))
alert(JSON.stringify(value))
},
// Add a validator to support Valibot usage in Form and Field
validatorAdapter: valibotValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/vue/yup/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const form = useForm({
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
alert(JSON.stringify(values))
alert(JSON.stringify(value))
},
// Add a validator to support Yup usage in Form and Field
validatorAdapter: yupValidator,
Expand Down
4 changes: 2 additions & 2 deletions examples/vue/zod/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const form = useForm({
firstName: '',
lastName: '',
},
onSubmit: async (values) => {
onSubmit: async ({ value }) => {
// Do something with form data
alert(JSON.stringify(values))
alert(JSON.stringify(value))
},
// Add a validator to support Zod usage in Form and Field
validatorAdapter: zodValidator,
Expand Down
1 change: 0 additions & 1 deletion packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ export class FieldApi<
}

handleChange = (updater: Updater<TData>) => {
debugger
this.setValue(updater, { touch: true })
}

Expand Down
28 changes: 17 additions & 11 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ export type FormOptions<TData, ValidatorType> = {
asyncDebounceMs?: number
validatorAdapter?: ValidatorType
validators?: FormValidators<TData, ValidatorType>
onSubmit?: (
values: TData,
formApi: FormApi<TData, ValidatorType>,
) => any | Promise<any>
onSubmitInvalid?: (
values: TData,
formApi: FormApi<TData, ValidatorType>,
) => void
onSubmit?: (props: {
value: TData
formApi: FormApi<TData, ValidatorType>
}) => any | Promise<any>
onSubmitInvalid?: (props: {
value: TData
formApi: FormApi<TData, ValidatorType>
}) => void
}

export type ValidationMeta = {
Expand Down Expand Up @@ -525,7 +525,10 @@ export class FormApi<TFormData, ValidatorType> {
// Fields are invalid, do not submit
if (!this.state.isFieldsValid) {
done()
this.options.onSubmitInvalid?.(this.state.values, this)
this.options.onSubmitInvalid?.({
value: this.state.values,
formApi: this,
})
return
}

Expand All @@ -534,13 +537,16 @@ export class FormApi<TFormData, ValidatorType> {

if (!this.state.isValid) {
done()
this.options.onSubmitInvalid?.(this.state.values, this)
this.options.onSubmitInvalid?.({
value: this.state.values,
formApi: this,
})
return
}

try {
// Run the submit code
await this.options.onSubmit?.(this.state.values, this)
await this.options.onSubmit?.({ value: this.state.values, formApi: this })

this.store.batch(() => {
this.store.setState((prev) => ({ ...prev, isSubmitted: true }))
Expand Down
4 changes: 2 additions & 2 deletions packages/react-form/src/tests/useForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ describe('useForm', () => {
defaultValues: {
firstName: 'FirstName',
},
onSubmit: (data) => {
setSubmittedData(data)
onSubmit: ({ value }) => {
setSubmittedData(value)
},
})

Expand Down
4 changes: 2 additions & 2 deletions packages/solid-form/src/tests/createForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ describe('createForm', () => {
defaultValues: {
firstName: 'FirstName',
},
onSubmit: (data) => {
submittedData = data
onSubmit: ({ value }) => {
submittedData = value
},
}))

Expand Down
4 changes: 2 additions & 2 deletions packages/vue-form/src/tests/useForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ describe('useForm', () => {
defaultValues: {
firstName: 'FirstName',
},
onSubmit: (data) => {
submittedData.value = data
onSubmit: ({ value }) => {
submittedData.value = value
},
})
form.provideFormContext()
Expand Down

0 comments on commit 1dc351e

Please sign in to comment.