interface.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* eslint-disable @typescript-eslint/ban-types */
  2. import { DefaultAdapter } from '../base/foundation';
  3. import { Options as ScrollIntoViewOptions } from 'scroll-into-view-if-needed';
  4. export type BasicTriggerType = 'blur' | 'change' | 'custom' | 'mount';
  5. export type FieldValidateTriggerType = BasicTriggerType | Array<BasicTriggerType>;
  6. export type CommonFieldError = boolean | string | Array<any> | undefined | unknown;
  7. export type BasicFieldError = Array<any>;
  8. export interface BaseFormAdapter<P = Record<string, any>, S = Record<string, any>, Values extends object = any> extends DefaultAdapter<P, S> {
  9. cloneDeep: (val: any, ...rest: any[]) => any;
  10. notifySubmit: (values: any, e?: any) => void;
  11. notifySubmitFail: (errors: Record<keyof Values, BasicFieldError>, values: Partial<Values>, e?: any) => void;
  12. forceUpdate: (callback?: () => void) => void;
  13. notifyChange: (formState: FormState) => void;
  14. notifyValueChange: (values: any, changedValues: any) => void;
  15. notifyReset: () => void;
  16. getInitValues: () => Partial<Values>;
  17. getFormProps: (keys: undefined | string | Array<string>) => any;
  18. getAllErrorDOM: () => NodeList;
  19. getFieldDOM: (field: string) => Node;
  20. getFieldErrorDOM: (field: string) => Node;
  21. initFormId: () => void
  22. }
  23. export type AllErrors<T> = T extends Record<string, any> ? { [K in keyof T]?: string } : Record<string, any>;
  24. export interface FormState<T extends Record<string, any> = any> {
  25. values?: T extends Record<string, any> ? T : Record<string, any>;
  26. errors?: AllErrors<T>;
  27. touched?: T extends Record<string, any> ? { [K in keyof T]?: boolean } : Record<string, any>
  28. }
  29. export interface setValuesConfig {
  30. isOverride: boolean
  31. }
  32. // FieldPath 类型定义,用于生成对象字段的路径字符串
  33. export type FieldPath<T> = T extends object ? {
  34. // 遍历对象的每个键 K
  35. [K in keyof T]: T[K] extends object
  36. // 如果键 K 对应的值是对象,则生成嵌套路径(递归调用 FieldPath)
  37. ? `${string & K}.${FieldPath<T[K]>}` | `${string & K}`
  38. // 否则,仅生成当前键的路径
  39. : `${string & K}`;
  40. }[keyof T]
  41. : never;
  42. // FieldPathValue 类型定义,用于从路径字符串中推导出实际的类型
  43. export type FieldPathValue<T, P extends FieldPath<T>> =
  44. // 如果路径字符串 P 包含嵌套路径(使用模板字符串类型进行匹配)
  45. P extends `${infer K}.${infer Rest}`
  46. ? K extends keyof T
  47. // 递归解析嵌套路径,逐层深入对象结构
  48. ? Rest extends FieldPath<T[K]>
  49. ? FieldPathValue<T[K], Rest>
  50. : never
  51. : never
  52. // 如果路径字符串 P 是顶层键
  53. : P extends keyof T
  54. ? T[P]
  55. : never;
  56. export type ScrollToErrorOptions<K> = {
  57. field?: K;
  58. index?: number;
  59. scrollConfig?: ScrollIntoViewOptions
  60. }
  61. // use object replace Record<string, any>, fix issue 933
  62. export interface BaseFormApi<T extends object = any> {
  63. /** get value of field */
  64. getValue: <P extends FieldPath<T>>(field?: P) => FieldPathValue<T, P>;
  65. /** set value of field */
  66. setValue: <K extends FieldPath<T>>(field: K, newFieldValue: any) => void;
  67. /** get error of field */
  68. getError: <K extends keyof T>(field: K) => any;
  69. /** set error of field */
  70. setError: <K extends keyof T>(field: K, fieldError: any) => void;
  71. /** get touched of field */
  72. getTouched: <K extends keyof T>(field: K) => boolean;
  73. /** set touch of field */
  74. setTouched: <K extends keyof T>(field: K, fieldTouch: boolean) => void;
  75. /** judge field exist */
  76. getFieldExist: <K extends keyof T>(field: K) => boolean;
  77. /** get formState of form */
  78. getFormState: () => FormState<T extends object ? T : object>;
  79. /** submit form manual */
  80. submitForm: () => void;
  81. /** reset form manual */
  82. reset: (fields?: Array<string>) => void;
  83. /** trigger validate manual */
  84. validate: <K extends keyof T, Params extends Array<K>, V extends Params[number]>(fields?: Params) => Promise<{ [R in V]: T[R] }>;
  85. getInitValue: <K extends keyof T>(field: K) => any;
  86. getInitValues: () => any;
  87. getValues: () => T;
  88. /** set value of multiple fields */
  89. setValues: (fieldsValue: Partial<T>, config?: setValuesConfig) => void;
  90. scrollToField: <K extends keyof T>(field: K, scrollConfig?: ScrollIntoViewOptions) => void;
  91. scrollToError: <K extends keyof T>(config?: ScrollToErrorOptions<K>) => void
  92. }
  93. export interface CallOpts {
  94. [x: string]: any;
  95. notNotify?: boolean;
  96. notUpdate?: boolean;
  97. needClone?: boolean
  98. }
  99. export interface ComponentProps {
  100. [x: string]: any
  101. }
  102. export interface FieldState {
  103. value?: any;
  104. touched?: any;
  105. error?: any;
  106. status?: 'error' | 'success'
  107. }
  108. export interface WithFieldOption {
  109. valueKey?: string;
  110. onKeyChangeFnName?: string;
  111. valuePath?: string;
  112. maintainCursor?: boolean;
  113. shouldMemo?: boolean;
  114. shouldInject?: boolean
  115. }
  116. export interface InternalFieldApi {
  117. setValue: (val: any, opts: CallOpts) => void;
  118. setTouched: (isTouched: boolean, opts: CallOpts) => void;
  119. setError: (errors: any, opts: CallOpts) => void;
  120. reset: () => void;
  121. validate: (val: any, opts: CallOpts) => Promise<unknown>
  122. }
  123. export interface FieldStaff {
  124. field: string;
  125. fieldApi: InternalFieldApi;
  126. keepState: boolean;
  127. allowEmpty: boolean
  128. }
  129. export interface ArrayFieldStaff {
  130. field: string;
  131. updateKey?: string;
  132. initValue?: any
  133. }
  134. export interface FormUpdaterContextType {
  135. register: (field: string, fieldState: FieldState, fieldStuff: FieldStaff) => void;
  136. unRegister: (field: string) => void;
  137. updateStateValue: (field: string, value: any, opts?: CallOpts) => void;
  138. updateStateError: (field: string, error: any, opts?: CallOpts) => void;
  139. updateStateTouched: (field: string, isTouched: boolean, opts?: CallOpts) => void;
  140. getValue: (field?: string | undefined, opts?: CallOpts) => any;
  141. getError: (field?: string) => any;
  142. getTouched: (field?: string) => boolean | Record<string, any> | undefined;
  143. getInitValues: () => any;
  144. getInitValue: (field?: string) => any;
  145. getFormProps: (keys?: Array<string>) => ComponentProps;
  146. getField: (field: string) => FieldStaff | undefined;
  147. registerArrayField: (arrayFieldPath: string, val: any) => void;
  148. unRegisterArrayField: (arrayField: string) => void;
  149. getArrayField: (arrayField: string) => ArrayFieldStaff;
  150. updateArrayField: (arrayField: string, updateValue: any) => void
  151. }