component.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {
  2. App,
  3. defineComponent,
  4. ComponentPropsOptions,
  5. ExtractPropTypes,
  6. SetupContext,
  7. RenderFunction,
  8. Component
  9. } from 'vue';
  10. import locale from '../../locale';
  11. import { getPropByPath, isFunction } from '../util';
  12. export function createComponent(name: string) {
  13. const languages = locale.languages();
  14. const componentName = 'nut-' + name;
  15. return {
  16. componentName,
  17. translate(keyPath: string, ...args: unknown[]) {
  18. const text = getPropByPath(languages, `${name.replace('-', '')}.${keyPath}`) || getPropByPath(languages, keyPath);
  19. return isFunction(text) ? text(...args) : text;
  20. },
  21. create: function <
  22. PropsOptions extends Readonly<ComponentPropsOptions>,
  23. Props extends Readonly<ExtractPropTypes<PropsOptions>>
  24. >(_component: {
  25. name?: string;
  26. baseName?: string;
  27. install?: (vue: App) => void;
  28. props?: PropsOptions;
  29. components?: Record<string, Component>;
  30. setup?: (props: Props, setupContext: SetupContext) => RenderFunction | Record<string, any> | any;
  31. emits?: string[];
  32. [optionKey: string]: any;
  33. }) {
  34. _component.baseName = name;
  35. _component.name = componentName;
  36. _component.install = (vue: App) => {
  37. vue.component(_component.name as string, _component as any);
  38. };
  39. return defineComponent(_component as any);
  40. } as typeof defineComponent,
  41. createDemo: function (_component: any) {
  42. _component.baseName = name;
  43. _component.name = 'demo-' + name;
  44. return defineComponent(_component);
  45. }
  46. };
  47. }