interface.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* eslint-disable @typescript-eslint/ban-types */
  2. /* eslint-disable max-len */
  3. import { DefaultAdapter } from '../base/foundation';
  4. import { Options as scrollIntoViewOptions } from 'scroll-into-view-if-needed';
  5. export type BasicTriggerType = 'blur' | 'change' | 'custom' | 'mount';
  6. export type FieldValidateTriggerType = BasicTriggerType | Array<BasicTriggerType>;
  7. export type CommonFieldError = boolean | string | Array<any> | undefined | unknown;
  8. export type BasicFieldError = Array<any>;
  9. export interface BaseFormAdapter<P = Record<string, any>, S = Record<string, any>, Values extends object = any> extends DefaultAdapter<P, S> {
  10. cloneDeep: (val: any, ...rest: any[]) => any;
  11. notifySubmit: (values: any) => void;
  12. notifySubmitFail: (errors: Record<keyof Values, BasicFieldError>, values: Partial<Values>) => void;
  13. forceUpdate: (callback?: () => void) => void;
  14. notifyChange: (formState: FormState) => void;
  15. notifyValueChange: (values: any, changedValues: any) => void;
  16. notifyReset: () => void;
  17. getInitValues: () => Partial<Values>;
  18. getFormProps: (keys: undefined | string | Array<string>) => any;
  19. getAllErrorDOM: () => NodeList;
  20. getFieldDOM: (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. // use object replace Record<string, any>, fix issue 933
  33. export interface BaseFormApi<T extends object = any> {
  34. /** get value of field */
  35. getValue: <K extends keyof T>(field?: K) => T[K];
  36. /** set value of field */
  37. setValue: <K extends keyof T>(field: K, newFieldValue: T[K]) => void;
  38. /** get error of field */
  39. getError: <K extends keyof T>(field: K) => any;
  40. /** set error of field */
  41. setError: <K extends keyof T>(field: K, fieldError: any) => void;
  42. /** get touched of field */
  43. getTouched: <K extends keyof T>(field: K) => boolean;
  44. /** set touch of field */
  45. setTouched: <K extends keyof T>(field: K, fieldTouch: boolean) => void;
  46. /** judge field exist */
  47. getFieldExist: <K extends keyof T>(field: K) => boolean;
  48. /** get formState of form */
  49. getFormState: () => FormState<T extends object ? T : object>;
  50. /** submit form manual */
  51. submitForm: () => void;
  52. /** reset form manual */
  53. reset: (fields?: Array<string>) => void;
  54. /** trigger validate manual */
  55. validate: <K extends keyof T, Params extends Array<K>, V extends Params[number]>(fields?: Params) => Promise<{ [R in V]: T[R] }>;
  56. getInitValue: <K extends keyof T>(field: K) => any;
  57. getInitValues: () => any;
  58. getValues: () => T;
  59. /** set value of multiple fields */
  60. setValues: (fieldsValue: Partial<T>, config?: setValuesConfig) => void;
  61. scrollToField: <K extends keyof T>(field: K, scrollConfig?: scrollIntoViewOptions) => void
  62. }
  63. export interface CallOpts {
  64. [x: string]: any;
  65. notNotify?: boolean;
  66. notUpdate?: boolean;
  67. needClone?: boolean
  68. }
  69. export interface ComponentProps {
  70. [x: string]: any
  71. }
  72. export interface FieldState {
  73. value?: any;
  74. touched?: any;
  75. error?: any;
  76. status?: 'error' | 'success'
  77. }
  78. export interface WithFieldOption {
  79. valueKey?: string;
  80. onKeyChangeFnName?: string;
  81. valuePath?: string;
  82. maintainCursor?: boolean;
  83. shouldMemo?: boolean;
  84. shouldInject?: boolean
  85. }
  86. export interface InternalFieldApi {
  87. setValue: (val: any, opts: CallOpts) => void;
  88. setTouched: (isTouched: boolean, opts: CallOpts) => void;
  89. setError: (errors: any, opts: CallOpts) => void;
  90. reset: () => void;
  91. validate: (val: any, opts: CallOpts) => Promise<unknown>
  92. }
  93. export interface FieldStaff {
  94. field: string;
  95. fieldApi: InternalFieldApi;
  96. keepState: boolean;
  97. allowEmpty: boolean
  98. }
  99. export interface ArrayFieldStaff {
  100. field: string;
  101. updateKey?: string;
  102. initValue?: any
  103. }
  104. export interface FormUpdaterContextType {
  105. register: (field: string, fieldState: FieldState, fieldStuff: FieldStaff) => void;
  106. unRegister: (field: string) => void;
  107. updateStateValue: (field: string, value: any, opts?: CallOpts) => void;
  108. updateStateError: (field: string, error: any, opts?: CallOpts) => void;
  109. updateStateTouched: (field: string, isTouched: boolean, opts?: CallOpts) => void;
  110. getValue: (field?: string | undefined, opts?: CallOpts) => any;
  111. getError: (field?: string) => any;
  112. getTouched: (field?: string) => boolean | Record<string, any> | undefined;
  113. getInitValues: () => any;
  114. getInitValue: (field?: string) => any;
  115. getFormProps: (keys?: Array<string>) => ComponentProps;
  116. getField: (field: string) => FieldStaff | undefined;
  117. registerArrayField: (arrayFieldPath: string, val: any) => void;
  118. unRegisterArrayField: (arrayField: string) => void;
  119. getArrayField: (arrayField: string) => ArrayFieldStaff;
  120. updateArrayField: (arrayField: string, updateValue: any) => void
  121. }