1
0

index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * 通用组件共同的使用的基础组件,原先放在 adapter/form 内部,限制了使用范围,这里提取出来,方便其他地方使用
  3. * 可用于 vben-form、vben-modal、vben-drawer 等组件使用,
  4. */
  5. import type { Component } from 'vue';
  6. import type { BaseFormComponentType } from '@vben/common-ui';
  7. import type { Recordable } from '@vben/types';
  8. import { defineComponent, getCurrentInstance, h, ref } from 'vue';
  9. import { ApiComponent, globalShareState, IconPicker } from '@vben/common-ui';
  10. import { $t } from '@vben/locales';
  11. import {
  12. AutoComplete,
  13. Button,
  14. Checkbox,
  15. CheckboxGroup,
  16. DatePicker,
  17. Divider,
  18. Input,
  19. InputNumber,
  20. InputPassword,
  21. Mentions,
  22. notification,
  23. Radio,
  24. RadioGroup,
  25. RangePicker,
  26. Rate,
  27. Select,
  28. Space,
  29. Switch,
  30. Textarea,
  31. TimePicker,
  32. TreeSelect,
  33. Upload,
  34. } from 'ant-design-vue';
  35. const withDefaultPlaceholder = <T extends Component>(
  36. component: T,
  37. type: 'input' | 'select',
  38. componentProps: Recordable<any> = {},
  39. ) => {
  40. return defineComponent({
  41. inheritAttrs: false,
  42. name: component.name,
  43. setup: (props: any, { attrs, expose, slots }) => {
  44. const placeholder =
  45. props?.placeholder ||
  46. attrs?.placeholder ||
  47. $t(`ui.placeholder.${type}`);
  48. // 透传组件暴露的方法
  49. const innerRef = ref();
  50. const publicApi: Recordable<any> = {};
  51. expose(publicApi);
  52. const instance = getCurrentInstance();
  53. instance?.proxy?.$nextTick(() => {
  54. for (const key in innerRef.value) {
  55. if (typeof innerRef.value[key] === 'function') {
  56. publicApi[key] = innerRef.value[key];
  57. }
  58. }
  59. });
  60. return () =>
  61. h(
  62. component,
  63. { ...componentProps, placeholder, ...props, ...attrs, ref: innerRef },
  64. slots,
  65. );
  66. },
  67. });
  68. };
  69. // 这里需要自行根据业务组件库进行适配,需要用到的组件都需要在这里类型说明
  70. export type ComponentType =
  71. | 'ApiSelect'
  72. | 'ApiTreeSelect'
  73. | 'AutoComplete'
  74. | 'Checkbox'
  75. | 'CheckboxGroup'
  76. | 'DatePicker'
  77. | 'DefaultButton'
  78. | 'Divider'
  79. | 'IconPicker'
  80. | 'Input'
  81. | 'InputNumber'
  82. | 'InputPassword'
  83. | 'Mentions'
  84. | 'PrimaryButton'
  85. | 'Radio'
  86. | 'RadioGroup'
  87. | 'RangePicker'
  88. | 'Rate'
  89. | 'Select'
  90. | 'Space'
  91. | 'Switch'
  92. | 'Textarea'
  93. | 'TimePicker'
  94. | 'TreeSelect'
  95. | 'Upload'
  96. | BaseFormComponentType;
  97. async function initComponentAdapter() {
  98. const components: Partial<Record<ComponentType, Component>> = {
  99. // 如果你的组件体积比较大,可以使用异步加载
  100. // Button: () =>
  101. // import('xxx').then((res) => res.Button),
  102. ApiSelect: withDefaultPlaceholder(ApiComponent, 'select', {
  103. component: Select,
  104. loadingSlot: 'suffixIcon',
  105. visibleEvent: 'onDropdownVisibleChange',
  106. modelPropName: 'value',
  107. }),
  108. ApiTreeSelect: withDefaultPlaceholder(ApiComponent, 'select', {
  109. component: TreeSelect,
  110. fieldNames: { label: 'label', value: 'value', children: 'children' },
  111. loadingSlot: 'suffixIcon',
  112. modelPropName: 'value',
  113. optionsPropName: 'treeData',
  114. visibleEvent: 'onVisibleChange',
  115. }),
  116. AutoComplete,
  117. Checkbox,
  118. CheckboxGroup,
  119. DatePicker,
  120. // 自定义默认按钮
  121. DefaultButton: (props, { attrs, slots }) => {
  122. return h(Button, { ...props, attrs, type: 'default' }, slots);
  123. },
  124. Divider,
  125. IconPicker: withDefaultPlaceholder(IconPicker, 'select', {
  126. iconSlot: 'addonAfter',
  127. inputComponent: Input,
  128. modelValueProp: 'value',
  129. }),
  130. Input: withDefaultPlaceholder(Input, 'input'),
  131. InputNumber: withDefaultPlaceholder(InputNumber, 'input'),
  132. InputPassword: withDefaultPlaceholder(InputPassword, 'input'),
  133. Mentions: withDefaultPlaceholder(Mentions, 'input'),
  134. // 自定义主要按钮
  135. PrimaryButton: (props, { attrs, slots }) => {
  136. return h(Button, { ...props, attrs, type: 'primary' }, slots);
  137. },
  138. Radio,
  139. RadioGroup,
  140. RangePicker,
  141. Rate,
  142. Select: withDefaultPlaceholder(Select, 'select'),
  143. Space,
  144. Switch,
  145. Textarea: withDefaultPlaceholder(Textarea, 'input'),
  146. TimePicker,
  147. TreeSelect: withDefaultPlaceholder(TreeSelect, 'select'),
  148. Upload,
  149. };
  150. // 将组件注册到全局共享状态中
  151. globalShareState.setComponents(components);
  152. // 定义全局共享状态中的消息提示
  153. globalShareState.defineMessage({
  154. // 复制成功消息提示
  155. copyPreferencesSuccess: (title, content) => {
  156. notification.success({
  157. description: content,
  158. message: title,
  159. placement: 'bottomRight',
  160. });
  161. },
  162. });
  163. }
  164. export { initComponentAdapter };