index.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. NButton,
  12. NCheckbox,
  13. NCheckboxGroup,
  14. NDatePicker,
  15. NDivider,
  16. NInput,
  17. NInputNumber,
  18. NRadioGroup,
  19. NSelect,
  20. NSpace,
  21. NSwitch,
  22. NTimePicker,
  23. NTreeSelect,
  24. NUpload,
  25. } from 'naive-ui';
  26. import { message } from '#/adapter/naive';
  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(`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: NCheckbox,
  58. CheckboxGroup: NCheckboxGroup,
  59. DatePicker: NDatePicker,
  60. // 自定义默认按钮
  61. DefaultButton: (props, { attrs, slots }) => {
  62. return h(NButton, { ...props, attrs, type: 'info' }, slots);
  63. },
  64. // 自定义主要按钮
  65. PrimaryButton: (props, { attrs, slots }) => {
  66. return h(NButton, { ...props, attrs, type: 'primary' }, slots);
  67. },
  68. Divider: NDivider,
  69. Input: withDefaultPlaceholder(NInput, 'input'),
  70. InputNumber: withDefaultPlaceholder(NInputNumber, 'input'),
  71. RadioGroup: NRadioGroup,
  72. Select: withDefaultPlaceholder(NSelect, 'select'),
  73. Space: NSpace,
  74. Switch: NSwitch,
  75. TimePicker: NTimePicker,
  76. TreeSelect: withDefaultPlaceholder(NTreeSelect, 'select'),
  77. Upload: NUpload,
  78. };
  79. // 将组件注册到全局共享状态中
  80. globalShareState.setComponents(components);
  81. // 定义全局共享状态中的消息提示
  82. globalShareState.defineMessage({
  83. // 复制成功消息提示
  84. copyPreferencesSuccess: (title, content) => {
  85. message.success(content || title, {
  86. duration: 0,
  87. });
  88. },
  89. });
  90. }
  91. export { initComponentAdapter };