data-form.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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' | 'timePick' | 'TimePick' | 'textarea'
  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. <span v-if="props?.required" style="color: #ff2e2e">*</span>
  119. {{ label || prop }}
  120. </label>
  121. <wd-input
  122. v-if="type === 'TextField'"
  123. v-bind="{
  124. ...(direction === 'vertical'
  125. ? verticalDefaultProps[type]
  126. : horizontalDefaultProps[type]),
  127. ...props,
  128. }"
  129. v-model="modelValue[prop]"
  130. ></wd-input>
  131. <wd-datetime-picker
  132. v-model="modelValue[prop]"
  133. v-if="type === 'timePick'"
  134. v-bind="{
  135. ...(direction === 'vertical'
  136. ? verticalDefaultProps[type]
  137. : horizontalDefaultProps[type]),
  138. cell: false,
  139. ...props,
  140. }"
  141. />
  142. <wd-datetime-picker
  143. v-model="modelValue[prop]"
  144. v-if="type === 'TimePick'"
  145. v-bind="{
  146. ...(direction === 'vertical'
  147. ? verticalDefaultProps[type]
  148. : horizontalDefaultProps[type]),
  149. cell: false,
  150. ...props,
  151. }"
  152. />
  153. <wd-textarea
  154. v-if="type === 'textarea'"
  155. v-model="modelValue[prop]"
  156. v-bind="{
  157. ...(direction === 'vertical'
  158. ? verticalDefaultProps[type]
  159. : horizontalDefaultProps[type]),
  160. cell: false,
  161. ...props,
  162. }"
  163. />
  164. <wd-textarea
  165. v-if="type === 'Textarea'"
  166. v-model="modelValue[prop]"
  167. v-bind="{
  168. ...(direction === 'vertical'
  169. ? verticalDefaultProps[type]
  170. : horizontalDefaultProps[type]),
  171. cell: false,
  172. ...props,
  173. }"
  174. />
  175. <wd-picker
  176. v-if="type === 'Select'"
  177. v-bind="{
  178. ...(direction === 'vertical'
  179. ? verticalDefaultProps[type]
  180. : horizontalDefaultProps[type]),
  181. cell: false,
  182. ...props,
  183. }"
  184. v-model="modelValue[prop]"
  185. ></wd-picker>
  186. <wd-radio-group
  187. v-if="type === 'Radio'"
  188. v-bind="{
  189. ...(direction === 'vertical'
  190. ? verticalDefaultProps[type]
  191. : horizontalDefaultProps[type]),
  192. ...props,
  193. cell: true,
  194. shape: 'button',
  195. }"
  196. v-model="modelValue[prop]"
  197. >
  198. <template v-for="{ label, value } of props.columns" :key="value">
  199. <wd-radio :value="value">{{ label }}</wd-radio>
  200. </template>
  201. </wd-radio-group>
  202. <wd-button
  203. v-if="type === 'Submit'"
  204. v-bind="{
  205. ...(direction === 'vertical' ? verticalDefaultProps[type] : {}),
  206. ...props,
  207. formType: 'submit',
  208. }"
  209. @click="submit"
  210. >
  211. <span v-if="type === 'Submit'">提交</span>
  212. </wd-button>
  213. </div>
  214. </template>
  215. </wd-form>
  216. </wd-config-provider>
  217. </template>
  218. <style lang="less" scoped>
  219. :deep(.wd-input) {
  220. background-color: #f5f7f9;
  221. border-radius: 8px;
  222. padding: 5px;
  223. margin-left: 5px;
  224. }
  225. .wd-input::after {
  226. height: 0;
  227. }
  228. :deep(.wd-picker) {
  229. background-color: #f5f7f9;
  230. border-radius: 8px;
  231. padding: 5px;
  232. margin-left: 5px;
  233. }
  234. :deep(.wd-textarea) {
  235. background-color: #f5f7f9;
  236. border-radius: 8px;
  237. }
  238. :deep(.wd-picker__cell) {
  239. background: transparent;
  240. }
  241. :deep(.wd-textarea__value) {
  242. background: transparent;
  243. }
  244. </style>