| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <script lang="ts" setup>
- import { message } from 'ant-design-vue';
- import { useVbenForm, z } from '#/adapter/form';
- const [Form] = useVbenForm({
- // 所有表单项共用,可单独在表单内覆盖
- commonConfig: {
- // 所有表单项
- componentProps: {
- class: 'w-full',
- },
- },
- // 提交函数
- handleSubmit: onSubmit,
- // 垂直布局,label和input在不同行,值为vertical
- // 水平布局,label和input在同一行
- layout: 'horizontal',
- schema: [
- {
- // 组件需要在 #/adapter.ts内注册,并加上类型
- component: 'Input',
- // 对应组件的参数
- componentProps: {
- placeholder: '请输入',
- },
- // 字段名
- fieldName: 'field1',
- // 界面显示的label
- label: '字段1',
- rules: 'required',
- },
- {
- component: 'Input',
- componentProps: {
- placeholder: '请输入',
- },
- defaultValue: '默认值',
- fieldName: 'field2',
- label: '默认值(必填)',
- rules: 'required',
- },
- {
- component: 'Input',
- componentProps: {
- placeholder: '请输入',
- },
- fieldName: 'field3',
- label: '默认值(非必填)',
- rules: z.string().default('默认值').optional(),
- },
- {
- component: 'Input',
- componentProps: {
- placeholder: '请输入',
- },
- fieldName: 'field31',
- label: '自定义信息',
- rules: z.string().min(1, { message: '最少输入1个字符' }),
- },
- {
- component: 'Input',
- // 对应组件的参数
- componentProps: {
- placeholder: '请输入',
- },
- // 字段名
- fieldName: 'field4',
- // 界面显示的label
- label: '邮箱',
- rules: z.string().email('请输入正确的邮箱'),
- },
- {
- component: 'InputNumber',
- componentProps: {
- placeholder: '请输入',
- },
- fieldName: 'number',
- label: '数字',
- rules: 'required',
- },
- {
- component: 'Select',
- componentProps: {
- allowClear: true,
- filterOption: true,
- options: [
- {
- label: '选项1',
- value: '1',
- },
- {
- label: '选项2',
- value: '2',
- },
- ],
- placeholder: '请选择',
- showSearch: true,
- },
- defaultValue: undefined,
- fieldName: 'options',
- label: '下拉选',
- rules: 'selectRequired',
- },
- {
- component: 'RadioGroup',
- componentProps: {
- options: [
- {
- label: '选项1',
- value: '1',
- },
- {
- label: '选项2',
- value: '2',
- },
- ],
- },
- fieldName: 'radioGroup',
- label: '单选组',
- rules: 'selectRequired',
- },
- {
- component: 'CheckboxGroup',
- componentProps: {
- name: 'cname',
- options: [
- {
- label: '选项1',
- value: '1',
- },
- {
- label: '选项2',
- value: '2',
- },
- ],
- },
- fieldName: 'checkboxGroup',
- label: '多选组',
- rules: 'selectRequired',
- },
- {
- component: 'Checkbox',
- fieldName: 'checkbox',
- label: '',
- renderComponentContent: () => {
- return {
- default: () => ['我已阅读并同意'],
- };
- },
- rules: 'selectRequired',
- },
- {
- component: 'DatePicker',
- defaultValue: undefined,
- fieldName: 'datePicker',
- label: '日期选择框',
- rules: 'selectRequired',
- },
- {
- component: 'RangePicker',
- defaultValue: undefined,
- fieldName: 'rangePicker',
- label: '区间选择框',
- rules: 'selectRequired',
- },
- {
- component: 'InputPassword',
- componentProps: {
- placeholder: '请输入',
- },
- fieldName: 'password',
- label: '密码',
- rules: 'required',
- },
- ],
- wrapperClass: 'grid-cols-1',
- });
- function onSubmit(values: Record<string, any>) {
- message.success({
- content: `form values: ${JSON.stringify(values)}`,
- });
- }
- </script>
- <template>
- <Form />
- </template>
|