index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. ElButton,
  12. ElCheckbox,
  13. ElCheckboxGroup,
  14. ElDatePicker,
  15. ElDivider,
  16. ElInput,
  17. ElInputNumber,
  18. ElNotification,
  19. ElRadioGroup,
  20. ElSelect,
  21. ElSelectV2,
  22. ElSpace,
  23. ElSwitch,
  24. ElTimePicker,
  25. ElTreeSelect,
  26. ElUpload,
  27. } from 'element-plus';
  28. const withDefaultPlaceholder = <T extends Component>(
  29. component: T,
  30. type: 'input' | 'select',
  31. ) => {
  32. return (props: any, { attrs, slots }: Omit<SetupContext, 'expose'>) => {
  33. const placeholder = props?.placeholder || $t(`ui.placeholder.${type}`);
  34. return h(component, { ...props, ...attrs, placeholder }, slots);
  35. };
  36. };
  37. // 这里需要自行根据业务组件库进行适配,需要用到的组件都需要在这里类型说明
  38. export type ComponentType =
  39. | 'ApiSelect'
  40. | 'Checkbox'
  41. | 'CheckboxGroup'
  42. | 'DatePicker'
  43. | 'Divider'
  44. | 'IconPicker'
  45. | 'Input'
  46. | 'InputNumber'
  47. | 'RadioGroup'
  48. | 'Select'
  49. | 'Space'
  50. | 'Switch'
  51. | 'TimePicker'
  52. | 'TreeSelect'
  53. | 'Upload'
  54. | BaseFormComponentType;
  55. async function initComponentAdapter() {
  56. const components: Partial<Record<ComponentType, Component>> = {
  57. // 如果你的组件体积比较大,可以使用异步加载
  58. // Button: () =>
  59. // import('xxx').then((res) => res.Button),
  60. ApiSelect: (props, { attrs, slots }) => {
  61. return h(
  62. ApiSelect,
  63. {
  64. ...props,
  65. ...attrs,
  66. component: ElSelectV2,
  67. loadingSlot: 'loading',
  68. visibleEvent: 'onDropdownVisibleChange',
  69. },
  70. slots,
  71. );
  72. },
  73. Checkbox: ElCheckbox,
  74. CheckboxGroup: ElCheckboxGroup,
  75. // 自定义默认按钮
  76. DefaultButton: (props, { attrs, slots }) => {
  77. return h(ElButton, { ...props, attrs, type: 'info' }, slots);
  78. },
  79. // 自定义主要按钮
  80. PrimaryButton: (props, { attrs, slots }) => {
  81. return h(ElButton, { ...props, attrs, type: 'primary' }, slots);
  82. },
  83. Divider: ElDivider,
  84. IconPicker: (props, { attrs, slots }) => {
  85. return h(
  86. IconPicker,
  87. {
  88. iconSlot: 'append',
  89. modelValueProp: 'model-value',
  90. inputComponent: ElInput,
  91. ...props,
  92. ...attrs,
  93. },
  94. slots,
  95. );
  96. },
  97. Input: withDefaultPlaceholder(ElInput, 'input'),
  98. InputNumber: withDefaultPlaceholder(ElInputNumber, 'input'),
  99. RadioGroup: ElRadioGroup,
  100. Select: withDefaultPlaceholder(ElSelect, 'select'),
  101. Space: ElSpace,
  102. Switch: ElSwitch,
  103. TimePicker: ElTimePicker,
  104. DatePicker: ElDatePicker,
  105. TreeSelect: withDefaultPlaceholder(ElTreeSelect, 'select'),
  106. Upload: ElUpload,
  107. };
  108. // 将组件注册到全局共享状态中
  109. globalShareState.setComponents(components);
  110. // 定义全局共享状态中的消息提示
  111. globalShareState.defineMessage({
  112. // 复制成功消息提示
  113. copyPreferencesSuccess: (title, content) => {
  114. ElNotification({
  115. title,
  116. message: content,
  117. position: 'bottom-right',
  118. duration: 0,
  119. type: 'success',
  120. });
  121. },
  122. });
  123. }
  124. export { initComponentAdapter };