index.ts 2.8 KB

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