BasicFormRander.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!-- 自定义渲染 -->
  2. <template>
  3. <!-- 自定义表单 -->
  4. <BasicForm @register="registerForm" style="margin-top: 20px">
  5. <!-- #phone对应的是formSchemas对应slot(phone)插槽 -->
  6. <template #phone="{ model, field }">
  7. <!-- 如果是组件需要进行双向绑定,model当前表单对象,field当前字段名称 -->
  8. <a-input v-model:value="model[field]" placeholder="请输入手机号" />
  9. <span class="font-color">请输入您的手机号,方便我们联系您</span>
  10. </template>
  11. <template #feedback="{ model, field }">
  12. <JEditor v-model:value="model[field]" placeholder="请输入问题反馈" />
  13. <span class="font-color">请您图文并茂,方便我们了解问题并及时反馈</span>
  14. </template>
  15. </BasicForm>
  16. </template>
  17. <script lang="ts" setup>
  18. //引入依赖
  19. import { useForm, BasicForm, FormSchema } from '/@/components/Form';
  20. import JEditor from '/@/components/Form/src/jeecg/components/JEditor.vue';
  21. import { h } from 'vue';
  22. import { Input } from 'ant-design-vue';
  23. //自定义表单字段
  24. const formSchemas: FormSchema[] = [
  25. {
  26. field: 'productName',
  27. label: '商品名称',
  28. component: 'Input',
  29. },
  30. {
  31. field: 'price',
  32. label: '价格',
  33. component: 'InputNumber',
  34. },
  35. {
  36. field: 'buyNums',
  37. label: '购买个数',
  38. component: 'InputNumber',
  39. //model 单签表单对象,field 当前字段
  40. render: ({ model, field }) => {
  41. //渲染自定义组件,以Input为例
  42. return h(Input, {
  43. placeholder: '请输入购买个数',
  44. value: model[field],
  45. style: { width: '100%' },
  46. type: 'number',
  47. onChange: (e: ChangeEvent) => {
  48. model[field] = e.target.value;
  49. },
  50. });
  51. },
  52. },
  53. {
  54. field: 'describe',
  55. label: '描述',
  56. component: 'Input',
  57. componentProps: {
  58. disabled: true,
  59. },
  60. //渲染 values当前表单所有值
  61. render: ({ values }) => {
  62. let productName = values.productName?values.productName:'';
  63. let price = values.price ? values.price : 0;
  64. let buyNums = values.buyNums ? values.buyNums : 0;
  65. return '购买商品名称:' + productName + ', 总价格: ' + price * buyNums + '元';
  66. },
  67. },
  68. ];
  69. /**
  70. * BasicForm绑定注册;
  71. */
  72. const [registerForm] = useForm({
  73. //注册表单列
  74. schemas: formSchemas,
  75. showResetButton: false,
  76. labelWidth: '150px',
  77. submitButtonOptions: { text: '提交', preIcon: '' },
  78. actionColOptions: { span: 17 },
  79. });
  80. </script>
  81. <style scoped>
  82. /** 数字输入框样式 */
  83. :deep(.ant-input-number) {
  84. width: 100%;
  85. }
  86. </style>