custom.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <script lang="ts" setup>
  2. import { h } from 'vue';
  3. import { Page } from '@vben/common-ui';
  4. import { Card, Input, message } from 'ant-design-vue';
  5. import { useVbenForm } from '#/adapter';
  6. const [BaseForm] = useVbenForm({
  7. // 所有表单项共用,可单独在表单内覆盖
  8. commonConfig: {
  9. // 所有表单项
  10. componentProps: {
  11. class: 'w-full',
  12. },
  13. labelClass: 'w-2/6',
  14. },
  15. // 提交函数
  16. handleSubmit: onSubmit,
  17. // 垂直布局,label和input在不同行,值为vertical
  18. // 水平布局,label和input在同一行
  19. layout: 'horizontal',
  20. schema: [
  21. {
  22. // 组件需要在 #/adapter.ts内注册,并加上类型
  23. component: 'Input',
  24. fieldName: 'field',
  25. label: '自定义后缀',
  26. suffix: () => h('span', { class: 'text-red-600' }, '元'),
  27. },
  28. {
  29. component: 'Input',
  30. fieldName: 'field1',
  31. label: '自定义组件slot',
  32. renderComponentContent: () => ({
  33. prefix: () => 'prefix',
  34. suffix: () => 'suffix',
  35. }),
  36. },
  37. {
  38. component: h(Input, { placeholder: '请输入' }),
  39. fieldName: 'field2',
  40. label: '自定义组件',
  41. rules: 'required',
  42. },
  43. {
  44. component: 'Input',
  45. fieldName: 'field3',
  46. label: '自定义组件(slot)',
  47. rules: 'required',
  48. },
  49. ],
  50. // 中屏一行显示2个,小屏一行显示1个
  51. wrapperClass: 'grid-cols-1 md:grid-cols-2',
  52. });
  53. function onSubmit(values: Record<string, any>) {
  54. message.success({
  55. content: `form values: ${JSON.stringify(values)}`,
  56. });
  57. }
  58. </script>
  59. <template>
  60. <Page description="表单组件自定义示例" title="表单组件">
  61. <Card title="基础示例">
  62. <BaseForm>
  63. <template #field3="slotProps">
  64. <Input placeholder="请输入" v-bind="slotProps" />
  65. </template>
  66. </BaseForm>
  67. </Card>
  68. </Page>
  69. </template>