index.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * 通用组件共同的使用的基础组件,原先放在 adapter/form 内部,限制了使用范围,这里提取出来,方便其他地方使用
  3. * 可用于 vben-form、vben-modal、vben-drawer 等组件使用,
  4. */
  5. import type { BaseFormComponentType } from '@vben/common-ui';
  6. import type { Component, SetupContext } from 'vue';
  7. import { h } from 'vue';
  8. import { ApiSelect, globalShareState, IconPicker } 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. | 'ApiSelect'
  46. | 'ApiTreeSelect'
  47. | 'AutoComplete'
  48. | 'Checkbox'
  49. | 'CheckboxGroup'
  50. | 'DatePicker'
  51. | 'DefaultButton'
  52. | 'Divider'
  53. | 'IconPicker'
  54. | 'Input'
  55. | 'InputNumber'
  56. | 'InputPassword'
  57. | 'Mentions'
  58. | 'PrimaryButton'
  59. | 'Radio'
  60. | 'RadioGroup'
  61. | 'RangePicker'
  62. | 'Rate'
  63. | 'Select'
  64. | 'Space'
  65. | 'Switch'
  66. | 'Textarea'
  67. | 'TimePicker'
  68. | 'TreeSelect'
  69. | 'Upload'
  70. | BaseFormComponentType;
  71. async function initComponentAdapter() {
  72. const components: Partial<Record<ComponentType, Component>> = {
  73. // 如果你的组件体积比较大,可以使用异步加载
  74. // Button: () =>
  75. // import('xxx').then((res) => res.Button),
  76. ApiSelect: (props, { attrs, slots }) => {
  77. return h(
  78. ApiSelect,
  79. {
  80. placeholder: $t('ui.placeholder.select'),
  81. ...props,
  82. ...attrs,
  83. component: Select,
  84. loadingSlot: 'suffixIcon',
  85. modelPropName: 'value',
  86. visibleEvent: 'onVisibleChange',
  87. },
  88. slots,
  89. );
  90. },
  91. ApiTreeSelect: (props, { attrs, slots }) => {
  92. return h(
  93. ApiSelect,
  94. {
  95. placeholder: $t('ui.placeholder.select'),
  96. ...props,
  97. ...attrs,
  98. component: TreeSelect,
  99. fieldNames: { label: 'label', value: 'value', children: 'children' },
  100. loadingSlot: 'suffixIcon',
  101. modelPropName: 'value',
  102. optionsPropName: 'treeData',
  103. visibleEvent: 'onVisibleChange',
  104. },
  105. slots,
  106. );
  107. },
  108. AutoComplete,
  109. Checkbox,
  110. CheckboxGroup,
  111. DatePicker,
  112. // 自定义默认按钮
  113. DefaultButton: (props, { attrs, slots }) => {
  114. return h(Button, { ...props, attrs, type: 'default' }, slots);
  115. },
  116. Divider,
  117. IconPicker: (props, { attrs, slots }) => {
  118. return h(
  119. IconPicker,
  120. { iconSlot: 'addonAfter', inputComponent: Input, ...props, ...attrs },
  121. slots,
  122. );
  123. },
  124. Input: withDefaultPlaceholder(Input, 'input'),
  125. InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
  126. InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
  127. Mentions: withDefaultPlaceholder(Mentions, 'input'),
  128. // 自定义主要按钮
  129. PrimaryButton: (props, { attrs, slots }) => {
  130. return h(Button, { ...props, attrs, type: 'primary' }, slots);
  131. },
  132. Radio,
  133. RadioGroup,
  134. RangePicker,
  135. Rate,
  136. Select: withDefaultPlaceholder(Select, 'select'),
  137. Space,
  138. Switch,
  139. Textarea: withDefaultPlaceholder(Textarea, 'input'),
  140. TimePicker,
  141. TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'),
  142. Upload,
  143. };
  144. // 将组件注册到全局共享状态中
  145. globalShareState.setComponents(components);
  146. // 定义全局共享状态中的消息提示
  147. globalShareState.defineMessage({
  148. // 复制成功消息提示
  149. copyPreferencesSuccess: (title, content) => {
  150. notification.success({
  151. description: content,
  152. message: title,
  153. placement: 'bottomRight',
  154. });
  155. },
  156. });
  157. }
  158. export { initComponentAdapter };