basic.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <script lang="ts" setup>
  2. import { Page } from '@vben/common-ui';
  3. import { NButton, NCard, useMessage } from 'naive-ui';
  4. import { useVbenForm } from '#/adapter/form';
  5. const message = useMessage();
  6. const [Form, formApi] = useVbenForm({
  7. commonConfig: {
  8. // 所有表单项
  9. componentProps: {
  10. class: 'w-full',
  11. },
  12. },
  13. layout: 'horizontal',
  14. // 大屏一行显示3个,中屏一行显示2个,小屏一行显示1个
  15. wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
  16. handleSubmit: (values) => {
  17. message.success(`表单数据:${JSON.stringify(values)}`);
  18. },
  19. schema: [
  20. {
  21. component: 'Input',
  22. fieldName: 'string',
  23. label: 'String',
  24. },
  25. {
  26. component: 'InputNumber',
  27. fieldName: 'number',
  28. label: 'Number',
  29. },
  30. {
  31. component: 'RadioGroup',
  32. fieldName: 'radio',
  33. label: 'Radio',
  34. componentProps: {
  35. options: [
  36. { value: 'A', label: 'A' },
  37. { value: 'B', label: 'B' },
  38. { value: 'C', label: 'C' },
  39. { value: 'D', label: 'D' },
  40. { value: 'E', label: 'E' },
  41. ],
  42. },
  43. },
  44. {
  45. component: 'RadioGroup',
  46. fieldName: 'radioButton',
  47. label: 'RadioButton',
  48. componentProps: {
  49. isButton: true,
  50. class: 'flex flex-wrap', // 如果选项过多,可以添加class来自动折叠
  51. options: [
  52. { value: 'A', label: '选项A' },
  53. { value: 'B', label: '选项B' },
  54. { value: 'C', label: '选项C' },
  55. { value: 'D', label: '选项D' },
  56. { value: 'E', label: '选项E' },
  57. { value: 'F', label: '选项F' },
  58. ],
  59. },
  60. },
  61. {
  62. component: 'CheckboxGroup',
  63. fieldName: 'checkbox',
  64. label: 'Checkbox',
  65. componentProps: {
  66. options: [
  67. { value: 'A', label: '选项A' },
  68. { value: 'B', label: '选项B' },
  69. { value: 'C', label: '选项C' },
  70. ],
  71. },
  72. },
  73. {
  74. component: 'DatePicker',
  75. fieldName: 'date',
  76. label: 'Date',
  77. },
  78. ],
  79. });
  80. function setFormValues() {
  81. formApi.setValues({
  82. string: 'string',
  83. number: 123,
  84. radio: 'B',
  85. radioButton: 'C',
  86. checkbox: ['A', 'C'],
  87. date: Date.now(),
  88. });
  89. }
  90. </script>
  91. <template>
  92. <Page
  93. description="表单适配器重新包装了CheckboxGroup和RadioGroup,可以通过options属性传递选项数据(选项数据将作为子组件的属性)"
  94. title="表单演示"
  95. >
  96. <NCard title="基础表单">
  97. <template #header-extra>
  98. <NButton type="primary" @click="setFormValues">设置表单值</NButton>
  99. </template>
  100. <Form />
  101. </NCard>
  102. </Page>
  103. </template>