interface.ts 4.9 KB

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