interface.ts 5.0 KB

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