form-schema.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import type {
  2. AutoCompleteProps,
  3. ButtonProps,
  4. CascaderProps,
  5. CheckboxGroupProps,
  6. CheckboxProps,
  7. DatePickerProps,
  8. DividerProps,
  9. InputNumberProps,
  10. InputProps,
  11. MentionsProps,
  12. RadioGroupProps,
  13. RadioProps,
  14. RangePickerProps,
  15. RateProps,
  16. SelectProps,
  17. SpaceProps,
  18. SwitchProps,
  19. TextAreaProps,
  20. TimePickerProps,
  21. TreeSelectProps,
  22. UploadProps,
  23. } from 'antdv-next';
  24. import type { Component } from 'vue';
  25. import type {
  26. ApiComponentSharedProps,
  27. VbenFormSchema as CoreFormSchema,
  28. FormActions,
  29. IconPickerProps,
  30. } from '@vben/common-ui';
  31. import type { ComponentType } from './component';
  32. type ComponentProps<P> =
  33. | ((
  34. value: Partial<Record<string, any>>,
  35. actions: FormActions,
  36. ) => P & Record<string, any>)
  37. | (P & Record<string, any>);
  38. /**
  39. * 与 {@link ComponentType} 中注册的组件名一一对应,便于 Schema 上 `component` + `componentProps` 联动提示
  40. */
  41. interface ComponentPropsMap {
  42. ApiCascader: ApiComponentSharedProps & CascaderProps;
  43. ApiSelect: ApiComponentSharedProps & SelectProps;
  44. ApiTreeSelect: ApiComponentSharedProps & TreeSelectProps;
  45. AutoComplete: AutoCompleteProps;
  46. Cascader: CascaderProps;
  47. Checkbox: CheckboxProps;
  48. CheckboxGroup: CheckboxGroupProps;
  49. DatePicker: DatePickerProps;
  50. DefaultButton: ButtonProps;
  51. Divider: DividerProps;
  52. IconPicker: IconPickerProps;
  53. Input: InputProps;
  54. InputNumber: InputNumberProps;
  55. InputPassword: InputProps;
  56. Mentions: MentionsProps;
  57. PrimaryButton: ButtonProps;
  58. Radio: RadioProps;
  59. RadioGroup: RadioGroupProps;
  60. RangePicker: RangePickerProps;
  61. Rate: RateProps;
  62. Select: SelectProps;
  63. Space: SpaceProps;
  64. Switch: SwitchProps;
  65. Textarea: TextAreaProps;
  66. TimePicker: TimePickerProps;
  67. TreeSelect: TreeSelectProps;
  68. Upload: UploadProps;
  69. }
  70. type BaseSchema = Omit<
  71. CoreFormSchema<ComponentType>,
  72. 'component' | 'componentProps'
  73. >;
  74. type RegisteredName = keyof ComponentPropsMap;
  75. type DiscriminatedFormSchema = {
  76. [K in RegisteredName]: BaseSchema & {
  77. component: K;
  78. componentProps?: ComponentProps<ComponentPropsMap[K]>;
  79. };
  80. }[RegisteredName];
  81. type FallbackFormSchema = BaseSchema & {
  82. component: Component | Exclude<ComponentType, RegisteredName>;
  83. componentProps?: CoreFormSchema<ComponentType>['componentProps'];
  84. };
  85. export type VbenFormSchema = DiscriminatedFormSchema | FallbackFormSchema;