component.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * 通用组件共同的使用的基础组件,原先放在 adapter/form 内部,限制了使用范围,这里提取出来,方便其他地方使用
  3. * 可用于 vben-form、vben-modal、vben-drawer 等组件使用,
  4. */
  5. import type { Component, SetupContext } from 'vue';
  6. import type { BaseFormComponentType } from '@vben/common-ui';
  7. import { h } from 'vue';
  8. import { globalShareState } from '@vben/common-ui';
  9. import { $t } from '@vben/locales';
  10. import {
  11. AutoComplete,
  12. Button,
  13. Checkbox,
  14. CheckboxGroup,
  15. DatePicker,
  16. Divider,
  17. Input,
  18. InputNumber,
  19. InputPassword,
  20. Mentions,
  21. notification,
  22. Radio,
  23. RadioGroup,
  24. RangePicker,
  25. Rate,
  26. Select,
  27. Space,
  28. Switch,
  29. Textarea,
  30. TimePicker,
  31. TreeSelect,
  32. Upload,
  33. } from 'ant-design-vue';
  34. const withDefaultPlaceholder = <T extends Component>(
  35. component: T,
  36. type: 'input' | 'select',
  37. ) => {
  38. return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => {
  39. const placeholder = props?.placeholder || $t(`ui.placeholder.${type}`);
  40. return h(component, { ...props, ...attrs, placeholder }, slots);
  41. };
  42. };
  43. // 这里需要自行根据业务组件库进行适配,需要用到的组件都需要在这里类型说明
  44. export type ComponentType =
  45. | 'AutoComplete'
  46. | 'Checkbox'
  47. | 'CheckboxGroup'
  48. | 'DatePicker'
  49. | 'DefaultButton'
  50. | 'Divider'
  51. | 'Input'
  52. | 'InputNumber'
  53. | 'InputPassword'
  54. | 'Mentions'
  55. | 'PrimaryButton'
  56. | 'Radio'
  57. | 'RadioGroup'
  58. | 'RangePicker'
  59. | 'Rate'
  60. | 'Select'
  61. | 'Space'
  62. | 'Switch'
  63. | 'Textarea'
  64. | 'TimePicker'
  65. | 'TreeSelect'
  66. | 'Upload'
  67. | BaseFormComponentType;
  68. async function initComponentAdapter() {
  69. const components: Partial<Record<ComponentType, Component>> = {
  70. // 如果你的组件体积比较大,可以使用异步加载
  71. // Button: () =>
  72. // import('xxx').then((res) => res.Button),
  73. AutoComplete,
  74. Checkbox,
  75. CheckboxGroup,
  76. DatePicker,
  77. // 自定义默认按钮
  78. DefaultButton: (props, { attrs, slots }) => {
  79. return h(Button, { ...props, attrs, type: 'default' }, slots);
  80. },
  81. Divider,
  82. Input: withDefaultPlaceholder(Input, 'input'),
  83. InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
  84. InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
  85. Mentions: withDefaultPlaceholder(Mentions, 'input'),
  86. // 自定义主要按钮
  87. PrimaryButton: (props, { attrs, slots }) => {
  88. return h(Button, { ...props, attrs, type: 'primary' }, slots);
  89. },
  90. Radio,
  91. RadioGroup,
  92. RangePicker,
  93. Rate,
  94. Select: withDefaultPlaceholder(Select, 'select'),
  95. Space,
  96. Switch,
  97. Textarea: withDefaultPlaceholder(Textarea, 'input'),
  98. TimePicker,
  99. TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'),
  100. Upload,
  101. };
  102. // 将组件注册到全局共享状态中
  103. globalShareState.setComponents(components);
  104. // 定义全局共享状态中的消息提示
  105. globalShareState.defineMessage({
  106. // 复制成功消息提示
  107. copyPreferencesSuccess: (title, content) => {
  108. notification.success({
  109. description: content,
  110. message: title,
  111. placement: 'bottomRight',
  112. });
  113. },
  114. });
  115. }
  116. export { initComponentAdapter };