useDictSelectRule.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { generateUUID } from '@/utils'
  2. import * as DictDataApi from '@/api/system/dict/dict.type'
  3. import { localeProps, makeRequiredRule } from '@/components/FormCreate/src/utils'
  4. import selectRule from '@/components/FormCreate/src/config/selectRule'
  5. export const useDictSelectRule = () => {
  6. const label = '字典选择器'
  7. const name = 'DictSelect'
  8. const dictOptions = ref<{ label: string; value: string }[]>([]) // 字典类型下拉数据
  9. onMounted(async () => {
  10. const data = await DictDataApi.getSimpleDictTypeList()
  11. if (!data || data.length === 0) {
  12. return
  13. }
  14. dictOptions.value =
  15. data?.map((item: DictDataApi.DictTypeVO) => ({
  16. label: item.name,
  17. value: item.type
  18. })) ?? []
  19. })
  20. return {
  21. icon: 'icon-select',
  22. label,
  23. name,
  24. rule() {
  25. return {
  26. type: name,
  27. field: generateUUID(),
  28. title: label,
  29. info: '',
  30. $required: false
  31. }
  32. },
  33. props(_, { t }) {
  34. return localeProps(t, name + '.props', [
  35. makeRequiredRule(),
  36. {
  37. type: 'select',
  38. field: 'dictType',
  39. title: '字典类型',
  40. value: '',
  41. options: dictOptions.value
  42. },
  43. {
  44. type: 'select',
  45. field: 'valueType',
  46. title: '字典值类型',
  47. value: 'str',
  48. options: [
  49. { label: '数字', value: 'int' },
  50. { label: '字符串', value: 'str' },
  51. { label: '布尔值', value: 'bool' }
  52. ]
  53. },
  54. ...selectRule
  55. ])
  56. }
  57. }
  58. }