您可能需要多种提交行为,例如返回另一页或停留在表单上。您可以通过指定 onSubmitMeta 属性来完成此操作。这些元数据将被传递到 onSubmit 函数。
注意:如果 form.handleSubmit() 在没有元数据的情况下调用,它将使用提供的默认值。
import { Component } from '@angular/core';
import { injectForm } from '@tanstack/angular-form';
type FormMeta = {
submitAction: 'continue' | 'backToMenu' | null;
};
// Metadata is not required to call form.handleSubmit().
// Specify what values to use as default if no meta is passed
const defaultMeta: FormMeta = {
submitAction: null,
};
@Component({
selector: 'app-root',
template: `
<form (submit)="handleSubmit($event)">
<button type="submit" (click)="
handleClick({ submitAction: 'continue' })
">Submit and continue</button>
<button type="submit" (click)="
handleClick({ submitAction: 'backToMenu' })
">Submit and back to menu</button>
</form>
`,
})
export class AppComponent {
form = injectForm({
defaultValues: {
data: '',
},
// Define what meta values to expect on submission
onSubmitMeta: defaultMeta,
onSubmit: async ({ value, meta }) => {
// Do something with the values passed via handleSubmit
console.log(`Selected action - ${meta.submitAction}`, value);
},
});
handleSubmit(event: SubmitEvent) {
event.preventDefault();
event.stopPropagation();
}
handleClick(meta: FormMeta) {
// Overwrites the default specified in onSubmitMeta
this.form.handleSubmit(meta);
}
}
import { Component } from '@angular/core';
import { injectForm } from '@tanstack/angular-form';
type FormMeta = {
submitAction: 'continue' | 'backToMenu' | null;
};
// Metadata is not required to call form.handleSubmit().
// Specify what values to use as default if no meta is passed
const defaultMeta: FormMeta = {
submitAction: null,
};
@Component({
selector: 'app-root',
template: `
<form (submit)="handleSubmit($event)">
<button type="submit" (click)="
handleClick({ submitAction: 'continue' })
">Submit and continue</button>
<button type="submit" (click)="
handleClick({ submitAction: 'backToMenu' })
">Submit and back to menu</button>
</form>
`,
})
export class AppComponent {
form = injectForm({
defaultValues: {
data: '',
},
// Define what meta values to expect on submission
onSubmitMeta: defaultMeta,
onSubmit: async ({ value, meta }) => {
// Do something with the values passed via handleSubmit
console.log(`Selected action - ${meta.submitAction}`, value);
},
});
handleSubmit(event: SubmitEvent) {
event.preventDefault();
event.stopPropagation();
}
handleClick(meta: FormMeta) {
// Overwrites the default specified in onSubmitMeta
this.form.handleSubmit(meta);
}
}
虽然 Tanstack Form 为验证提供了标准 Schema 支持,但它不会保留 Schema 的输出数据。
传递给 onSubmit 函数的值始终是输入数据。要接收标准 Schema 的输出数据,请在 onSubmit 函数中进行解析。
import { z } from 'zod'
// ...
const schema = z.object({
age: z.string().transform((age) => Number(age)),
})
// Tanstack Form uses the input type of Standard Schemas
const defaultValues: z.input<typeof schema> = {
age: '13',
}
// ...
@Component({
// ...
})
export class AppComponent {
form = injectForm({
defaultValues,
validators: {
onChange: schema,
},
onSubmit: ({ value }) => {
const inputAge: string = value.age
// Pass it through the schema to get the transformed value
const result = schema.parse(value)
const outputAge: number = result.age
},
})
}
import { z } from 'zod'
// ...
const schema = z.object({
age: z.string().transform((age) => Number(age)),
})
// Tanstack Form uses the input type of Standard Schemas
const defaultValues: z.input<typeof schema> = {
age: '13',
}
// ...
@Component({
// ...
})
export class AppComponent {
form = injectForm({
defaultValues,
validators: {
onChange: schema,
},
onSubmit: ({ value }) => {
const inputAge: string = value.age
// Pass it through the schema to get the transformed value
const result = schema.parse(value)
const outputAge: number = result.age
},
})
}
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。