interface.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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) => void;
  11. notifySubmitFail: (errors: Record<keyof Values, BasicFieldError>, values: Partial<Values>) => 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. initFormId: () => void
  21. }
  22. export type AllErrors<T> = T extends Record<string, any> ? { [K in keyof T]?: string } : Record<string, any>;
  23. export interface FormState<T extends Record<string, any> = any> {
  24. values?: T extends Record<string, any> ? T : Record<string, any>;
  25. errors?: AllErrors<T>;
  26. touched?: T extends Record<string, any> ? { [K in keyof T]?: boolean } : Record<string, any>
  27. }
  28. export interface setValuesConfig {
  29. isOverride: boolean
  30. }
  31. // use object replace Record<string, any>, fix issue 933
  32. export interface BaseFormApi<T extends object = any> {
  33. /** get value of field */
  34. getValue: <K extends keyof T>(field?: K) => T[K];
  35. /** set value of field */
  36. setValue: <K extends keyof T>(field: K, newFieldValue: T[K]) => void;
  37. /** get error of field */
  38. getError: <K extends keyof T>(field: K) => any;
  39. /** set error of field */
  40. setError: <K extends keyof T>(field: K, fieldError: any) => void;
  41. /** get touched of field */
  42. getTouched: <K extends keyof T>(field: K) => boolean;
  43. /** set touch of field */
  44. setTouched: <K extends keyof T>(field: K, fieldTouch: boolean) => void;
  45. /** judge field exist */
  46. getFieldExist: <K extends keyof T>(field: K) => boolean;
  47. /** get formState of form */
  48. getFormState: () => FormState<T extends object ? T : object>;
  49. /** submit form manual */
  50. submitForm: () => void;
  51. /** reset form manual */
  52. reset: (fields?: Array<string>) => void;
  53. /** trigger validate manual */
  54. validate: <K extends keyof T, Params extends Array<K>, V extends Params[number]>(fields?: Params) => Promise<{ [R in V]: T[R] }>;
  55. getInitValue: <K extends keyof T>(field: K) => any;
  56. getInitValues: () => any;
  57. getValues: () => T;
  58. /** set value of multiple fields */
  59. setValues: (fieldsValue: Partial<T>, config?: setValuesConfig) => void;
  60. scrollToField: <K extends keyof T>(field: K, scrollConfig?: scrollIntoViewOptions) => void
  61. }
  62. export interface CallOpts {
  63. [x: string]: any;
  64. notNotify?: boolean;
  65. notUpdate?: boolean;
  66. needClone?: boolean
  67. }
  68. export interface ComponentProps {
  69. [x: string]: any
  70. }
  71. export interface FieldState {
  72. value?: any;
  73. touched?: any;
  74. error?: any;
  75. status?: 'error' | 'success'
  76. }
  77. export interface WithFieldOption {
  78. valueKey?: string;
  79. onKeyChangeFnName?: string;
  80. valuePath?: string;
  81. maintainCursor?: boolean;
  82. shouldMemo?: boolean;
  83. shouldInject?: boolean
  84. }
  85. export interface InternalFieldApi {
  86. setValue: (val: any, opts: CallOpts) => void;
  87. setTouched: (isTouched: boolean, opts: CallOpts) => void;
  88. setError: (errors: any, opts: CallOpts) => void;
  89. reset: () => void;
  90. validate: (val: any, opts: CallOpts) => Promise<unknown>
  91. }
  92. export interface FieldStaff {
  93. field: string;
  94. fieldApi: InternalFieldApi;
  95. keepState: boolean;
  96. allowEmpty: boolean
  97. }
  98. export interface ArrayFieldStaff {
  99. field: string;
  100. updateKey?: string;
  101. initValue?: any
  102. }
  103. export interface FormUpdaterContextType {
  104. register: (field: string, fieldState: FieldState, fieldStuff: FieldStaff) => void;
  105. unRegister: (field: string) => void;
  106. updateStateValue: (field: string, value: any, opts?: CallOpts) => void;
  107. updateStateError: (field: string, error: any, opts?: CallOpts) => void;
  108. updateStateTouched: (field: string, isTouched: boolean, opts?: CallOpts) => void;
  109. getValue: (field?: string | undefined, opts?: CallOpts) => any;
  110. getError: (field?: string) => any;
  111. getTouched: (field?: string) => boolean | Record<string, any> | undefined;
  112. getInitValues: () => any;
  113. getInitValue: (field?: string) => any;
  114. getFormProps: (keys?: Array<string>) => ComponentProps;
  115. getField: (field: string) => FieldStaff | undefined;
  116. registerArrayField: (arrayFieldPath: string, val: any) => void;
  117. unRegisterArrayField: (arrayField: string) => void;
  118. getArrayField: (arrayField: string) => ArrayFieldStaff;
  119. updateArrayField: (arrayField: string, updateValue: any) => void
  120. }