index.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import type { RuleItem } from "async-validator";
  2. export interface FormDefine {
  3. /**
  4. * Todo: page
  5. */
  6. type?: 'flat'|'page'|'group',
  7. props?: any;
  8. propNestType?: 'flat'|'nest'|'array',
  9. items: FormDefineItem[];
  10. }
  11. /**
  12. * 表单动态属性定义
  13. */
  14. export declare type IFormItemCallback<T> = {
  15. /**
  16. * 预留,暂未使用
  17. */
  18. type?: string;
  19. /**
  20. * @param model 当前表单条目的值
  21. * @param rawModel 整个 form 的值 (最常用,当两个关联组件距离较远时,可以从顶层的 rawModel 里获取)
  22. * @param parentModel 父表单元素的值 (上一级的值,只在列表场景的使用,例如列表某个元素的父级就是整个 item)
  23. * @param item 当前表单条目信息
  24. */
  25. callback: (model: any, rawModel: any, parentModel: any, params: {
  26. formGlobalParams: any,
  27. item: FormDefineItem
  28. }) => T;
  29. };
  30. export type IFormItemCallbackAdditionalProps<T> = { [P in keyof T]?: T[P]|IFormItemCallback<T[P]> }
  31. export interface FormDefineItem {
  32. /**
  33. * 表单项显示标签
  34. */
  35. label?: string|IFormItemCallback<string>;
  36. /**
  37. * 属性名称
  38. */
  39. name: string;
  40. fullName?: string;
  41. /**
  42. * 表单项组件类型
  43. */
  44. type?: string;
  45. /**
  46. * 传递给条目组件的参数。(允许动态回调)
  47. */
  48. params?: Record<string, unknown|IFormItemCallback<unknown>>|unknown;
  49. /**
  50. * 传递给FormItem组件的参数
  51. */
  52. itemParams?: any;
  53. /**
  54. * 默认值,用于默认数据生成
  55. */
  56. defaultValue?: any;
  57. /**
  58. * 当前条目的校验规则
  59. */
  60. rules?: RuleItem[],
  61. /**
  62. * 子条目,在对象中为对象子属性,在数组中为数组条目(单条目按单项控制,多条目按对象看待控制)
  63. */
  64. children?: FormDefine,
  65. //todo:联动
  66. /**
  67. * 是否显示。当为undefined时,默认显示。
  68. */
  69. show?: boolean|IFormItemCallback<boolean>|undefined,
  70. /**
  71. * 当前条目组件加载时发生事件
  72. * @param topModel 顶层数据对象
  73. * @param ref 组件实例
  74. * @returns
  75. */
  76. onMounted?: (topModel: any, ref: any) => void;
  77. /**
  78. * 当前条目组件卸载时发生事件
  79. * @param topModel 顶层数据对象
  80. * @param ref 组件实例
  81. * @returns
  82. */
  83. onBeforeUnMount?: (topModel: any, ref: any) => void;
  84. /**
  85. * 当前条目数据更改时发生事件
  86. * @param oldValue 旧值
  87. * @param newValue 新值
  88. * @param topModel 顶层数据对象
  89. * @param ref 组件实例
  90. * @returns
  91. */
  92. onChange?: (oldValue: any, newValue: any, topModel: any, ref: any) => void;
  93. }
  94. export interface FormExport {
  95. /**
  96. * 初始化表单数据对象
  97. */
  98. initFormData(data: () => any): void;
  99. /**
  100. * 加载表单数据
  101. * @param value 表单数据
  102. */
  103. loadFormData(value?: Record<string, any>): void;
  104. /**
  105. * 提交表单
  106. */
  107. submitForm<T = Record<string, any>>(): Promise<T|null>;
  108. /**
  109. * 重置整个表单数据
  110. */
  111. resetForm(): void;
  112. }