data-form.vue 6.0 KB

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