interface.ts 4.9 KB

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