1
0

index.vue 1.6 KB

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