data-form.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <script setup lang="ts">
  2. import WdButton from 'wot-design-uni/components/wd-button/wd-button.vue'
  3. import WdInput from 'wot-design-uni/components/wd-input/wd-input.vue'
  4. import WdPicker from 'wot-design-uni/components/wd-picker/wd-picker.vue'
  5. import { ConfigProviderThemeVars } from 'wot-design-uni'
  6. const modelValue = defineModel({
  7. type: Object,
  8. default: () => ({}),
  9. })
  10. // defineProps({
  11. // schema: {
  12. // type: Object as PropType<{
  13. // [key: string | symbol]: { type: 'TextField' | 'Submit' | string; label?: string; props?: any }
  14. // }>,
  15. // required: true,
  16. // default: () => ({}),
  17. // },
  18. // direction: {
  19. // type: String as PropType<'horizontal' | 'vertical'>,
  20. // default: 'vertical',
  21. // },
  22. // })
  23. withDefaults(
  24. defineProps<{
  25. schema: {
  26. [key: symbol]: {
  27. type: 'TextField' | 'Select' | 'Radio' | 'Submit'
  28. label?: string
  29. existing?: boolean
  30. props?: any
  31. }
  32. }
  33. direction?: 'horizontal' | 'vertical'
  34. }>(),
  35. { direction: 'vertical' },
  36. )
  37. const emits = defineEmits(['submit'])
  38. const form = ref()
  39. const types = {
  40. TextField: WdInput,
  41. Submit: WdButton,
  42. Select: WdPicker,
  43. // Radio: WdRadioGroup,
  44. }
  45. const defaultProps = {
  46. TextField: {
  47. noBorder: true,
  48. style: {},
  49. customClass: 'rounded border border-[#e1e1e1] border-solid p-1',
  50. placeholder: ' ',
  51. },
  52. Submit: {
  53. customClass: 'w-full! rounded-lg! my-4!',
  54. block: true,
  55. },
  56. }
  57. const verticalDefaultProps = {
  58. TextField: {
  59. noBorder: true,
  60. style: {},
  61. customClass: 'rounded border border-[#e1e1e1] border-solid p-1',
  62. placeholder: ' ',
  63. },
  64. Submit: {
  65. customClass: 'w-full! rounded-lg! my-4!',
  66. block: true,
  67. },
  68. }
  69. const horizontalDefaultProps = {
  70. TextField: {
  71. customClass: 'text-red!',
  72. placeholderClass: 'text-black/30',
  73. },
  74. Select: {
  75. customClass: 'text-black/30! border-b-1 border-b-[#e1e1e1] border-b-solid',
  76. },
  77. Radio: {
  78. customClass: 'my--4!',
  79. },
  80. }
  81. const themeVars: ConfigProviderThemeVars = {
  82. cellPadding: '0',
  83. cellWrapperPadding: '10rpx',
  84. radioButtonRadius: '8rpx',
  85. radioButtonBg: 'transparent',
  86. }
  87. const submit = () => {
  88. emits('submit', modelValue)
  89. }
  90. const validate = (): Promise<{ valid: boolean; errors: any[] }> => form.value?.validate()
  91. defineExpose({
  92. validate,
  93. })
  94. </script>
  95. <template>
  96. <wd-config-provider :theme-vars="themeVars">
  97. <wd-form ref="form" :model="modelValue" >
  98. <template
  99. v-for="([prop, { type, label, existing, props }], index) in Object.entries(schema)"
  100. :key="index"
  101. >
  102. <div
  103. v-if="existing ?? true"
  104. class="grid mb-4"
  105. :class="[direction === 'horizontal' ? 'items-center' : '']"
  106. :style="
  107. direction === 'horizontal'
  108. ? { 'grid-template-columns': `${props?.labelWidth} auto` }
  109. : {}
  110. "
  111. >
  112. <label
  113. v-if="type !== 'Submit'"
  114. class="text-sm font-normal leading-relaxed"
  115. :class="[direction === 'horizontal' ? 'text-black/60' : 'mb-1 text-black/40']"
  116. :for="prop"
  117. >
  118. {{ label || prop }}
  119. </label>
  120. <wd-input
  121. v-if="type === 'TextField'"
  122. v-bind="{
  123. ...(direction === 'vertical'
  124. ? verticalDefaultProps[type]
  125. : horizontalDefaultProps[type]),
  126. ...props,
  127. }"
  128. v-model="modelValue[prop]"
  129. ></wd-input>
  130. <wd-picker
  131. v-if="type === 'Select'"
  132. v-bind="{
  133. ...(direction === 'vertical'
  134. ? verticalDefaultProps[type]
  135. : horizontalDefaultProps[type]),
  136. cell: false,
  137. ...props,
  138. }"
  139. v-model="modelValue[prop]"
  140. ></wd-picker>
  141. <wd-radio-group
  142. v-if="type === 'Radio'"
  143. v-bind="{
  144. ...(direction === 'vertical'
  145. ? verticalDefaultProps[type]
  146. : horizontalDefaultProps[type]),
  147. ...props,
  148. cell: true,
  149. shape: 'button',
  150. }"
  151. v-model="modelValue[prop]"
  152. >
  153. <template v-for="{ label, value } of props.columns" :key="value">
  154. <wd-radio :value="value">{{ label }}</wd-radio>
  155. </template>
  156. </wd-radio-group>
  157. <wd-button
  158. v-if="type === 'Submit'"
  159. v-bind="{
  160. ...(direction === 'vertical' ? verticalDefaultProps[type] : {}),
  161. ...props,
  162. formType: 'submit',
  163. }"
  164. @click="submit"
  165. >
  166. <span v-if="type === 'Submit'">提交</span>
  167. </wd-button>
  168. </div>
  169. </template>
  170. </wd-form>
  171. </wd-config-provider>
  172. </template>