interface.d.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import type { ReactElement } from 'react';
  2. import type { DeepNamePath } from './namePathType';
  3. import type { ReducerAction } from './useForm';
  4. export type InternalNamePath = (string | number)[];
  5. export type NamePath<T = any> = DeepNamePath<T>;
  6. export type StoreValue = any;
  7. export type Store = Record<string, StoreValue>;
  8. export interface Meta {
  9. touched: boolean;
  10. validating: boolean;
  11. errors: string[];
  12. warnings: string[];
  13. name: InternalNamePath;
  14. validated: boolean;
  15. }
  16. export interface InternalFieldData extends Meta {
  17. value: StoreValue;
  18. }
  19. /**
  20. * Used by `setFields` config
  21. */
  22. export interface FieldData<Values = any> extends Partial<Omit<InternalFieldData, 'name'>> {
  23. name: NamePath<Values>;
  24. }
  25. export type RuleType = 'string' | 'number' | 'boolean' | 'method' | 'regexp' | 'integer' | 'float' | 'object' | 'enum' | 'date' | 'url' | 'hex' | 'email';
  26. type Validator = (rule: RuleObject, value: StoreValue, callback: (error?: string) => void) => Promise<void | any> | void;
  27. export type RuleRender = (form: FormInstance) => RuleObject;
  28. export interface ValidatorRule {
  29. warningOnly?: boolean;
  30. message?: string | ReactElement;
  31. validator: Validator;
  32. }
  33. interface BaseRule {
  34. warningOnly?: boolean;
  35. enum?: StoreValue[];
  36. len?: number;
  37. max?: number;
  38. message?: string | ReactElement;
  39. min?: number;
  40. pattern?: RegExp;
  41. required?: boolean;
  42. transform?: (value: StoreValue) => StoreValue;
  43. type?: RuleType;
  44. whitespace?: boolean;
  45. /** Customize rule level `validateTrigger`. Must be subset of Field `validateTrigger` */
  46. validateTrigger?: string | string[];
  47. }
  48. type AggregationRule = BaseRule & Partial<ValidatorRule>;
  49. interface ArrayRule extends Omit<AggregationRule, 'type'> {
  50. type: 'array';
  51. defaultField?: RuleObject;
  52. }
  53. export type RuleObject = AggregationRule | ArrayRule;
  54. export type Rule = RuleObject | RuleRender;
  55. export interface ValidateErrorEntity<Values = any> {
  56. values: Values;
  57. errorFields: {
  58. name: InternalNamePath;
  59. errors: string[];
  60. }[];
  61. outOfDate: boolean;
  62. }
  63. export interface FieldEntity {
  64. onStoreChange: (store: Store, namePathList: InternalNamePath[] | null, info: ValuedNotifyInfo) => void;
  65. isFieldTouched: () => boolean;
  66. isFieldDirty: () => boolean;
  67. isFieldValidating: () => boolean;
  68. isListField: () => boolean;
  69. isList: () => boolean;
  70. isPreserve: () => boolean;
  71. validateRules: (options?: InternalValidateOptions) => Promise<RuleError[]>;
  72. getMeta: () => Meta;
  73. getNamePath: () => InternalNamePath;
  74. getErrors: () => string[];
  75. getWarnings: () => string[];
  76. props: {
  77. name?: NamePath;
  78. rules?: Rule[];
  79. dependencies?: NamePath[];
  80. initialValue?: any;
  81. };
  82. }
  83. export interface FieldError {
  84. name: InternalNamePath;
  85. errors: string[];
  86. warnings: string[];
  87. }
  88. export interface RuleError {
  89. errors: string[];
  90. rule: RuleObject;
  91. }
  92. export interface ValidateOptions {
  93. /**
  94. * Validate only and not trigger UI and Field status update
  95. */
  96. validateOnly?: boolean;
  97. /**
  98. * Recursive validate. It will validate all the name path that contains the provided one.
  99. * e.g. [['a']] will validate ['a'] , ['a', 'b'] and ['a', 1].
  100. */
  101. recursive?: boolean;
  102. /** Validate when a field is dirty (validated or touched) */
  103. dirty?: boolean;
  104. }
  105. export type ValidateFields<Values = any> = {
  106. (opt?: ValidateOptions): Promise<Values>;
  107. (nameList?: NamePath[], opt?: ValidateOptions): Promise<Values>;
  108. };
  109. export interface InternalValidateOptions extends ValidateOptions {
  110. triggerName?: string;
  111. validateMessages?: ValidateMessages;
  112. }
  113. export type InternalValidateFields<Values = any> = {
  114. (options?: InternalValidateOptions): Promise<Values>;
  115. (nameList?: NamePath[], options?: InternalValidateOptions): Promise<Values>;
  116. };
  117. interface ValueUpdateInfo {
  118. type: 'valueUpdate';
  119. source: 'internal' | 'external';
  120. }
  121. interface ValidateFinishInfo {
  122. type: 'validateFinish';
  123. }
  124. interface ResetInfo {
  125. type: 'reset';
  126. }
  127. interface RemoveInfo {
  128. type: 'remove';
  129. }
  130. interface SetFieldInfo {
  131. type: 'setField';
  132. data: FieldData;
  133. }
  134. interface DependenciesUpdateInfo {
  135. type: 'dependenciesUpdate';
  136. /**
  137. * Contains all the related `InternalNamePath[]`.
  138. * a <- b <- c : change `a`
  139. * relatedFields=[a, b, c]
  140. */
  141. relatedFields: InternalNamePath[];
  142. }
  143. export type NotifyInfo = ValueUpdateInfo | ValidateFinishInfo | ResetInfo | RemoveInfo | SetFieldInfo | DependenciesUpdateInfo;
  144. export type ValuedNotifyInfo = NotifyInfo & {
  145. store: Store;
  146. };
  147. export interface Callbacks<Values = any> {
  148. onValuesChange?: (changedValues: any, values: Values) => void;
  149. onFieldsChange?: (changedFields: FieldData[], allFields: FieldData[]) => void;
  150. onFinish?: (values: Values) => void;
  151. onFinishFailed?: (errorInfo: ValidateErrorEntity<Values>) => void;
  152. }
  153. export type WatchCallBack = (values: Store, allValues: Store, namePathList: InternalNamePath[]) => void;
  154. export interface WatchOptions<Form extends FormInstance = FormInstance> {
  155. form?: Form;
  156. preserve?: boolean;
  157. }
  158. export interface InternalHooks {
  159. dispatch: (action: ReducerAction) => void;
  160. initEntityValue: (entity: FieldEntity) => void;
  161. registerField: (entity: FieldEntity) => () => void;
  162. useSubscribe: (subscribable: boolean) => void;
  163. setInitialValues: (values: Store, init: boolean) => void;
  164. destroyForm: (clearOnDestroy?: boolean) => void;
  165. setCallbacks: (callbacks: Callbacks) => void;
  166. registerWatch: (callback: WatchCallBack) => () => void;
  167. getFields: (namePathList?: InternalNamePath[]) => FieldData[];
  168. setValidateMessages: (validateMessages: ValidateMessages) => void;
  169. setPreserve: (preserve?: boolean) => void;
  170. getInitialValue: (namePath: InternalNamePath) => StoreValue;
  171. }
  172. /** Only return partial when type is not any */
  173. type RecursivePartial<T> = NonNullable<T> extends object ? {
  174. [P in keyof T]?: NonNullable<T[P]> extends (infer U)[] ? RecursivePartial<U>[] : NonNullable<T[P]> extends object ? RecursivePartial<T[P]> : T[P];
  175. } : T;
  176. export type FilterFunc = (meta: Meta) => boolean;
  177. export type GetFieldsValueConfig = {
  178. strict?: boolean;
  179. filter?: FilterFunc;
  180. };
  181. export interface FormInstance<Values = any> {
  182. getFieldValue: (name: NamePath<Values>) => StoreValue;
  183. getFieldsValue: (() => Values) & ((nameList: NamePath<Values>[] | true, filterFunc?: FilterFunc) => any) & ((config: GetFieldsValueConfig) => any);
  184. getFieldError: (name: NamePath<Values>) => string[];
  185. getFieldsError: (nameList?: NamePath<Values>[]) => FieldError[];
  186. getFieldWarning: (name: NamePath<Values>) => string[];
  187. isFieldsTouched: ((nameList?: NamePath<Values>[], allFieldsTouched?: boolean) => boolean) & ((allFieldsTouched?: boolean) => boolean);
  188. isFieldTouched: (name: NamePath<Values>) => boolean;
  189. isFieldValidating: (name: NamePath<Values>) => boolean;
  190. isFieldsValidating: (nameList?: NamePath<Values>[]) => boolean;
  191. resetFields: (fields?: NamePath<Values>[]) => void;
  192. setFields: (fields: FieldData<Values>[]) => void;
  193. setFieldValue: (name: NamePath<Values>, value: any) => void;
  194. setFieldsValue: (values: RecursivePartial<Values>) => void;
  195. validateFields: ValidateFields<Values>;
  196. submit: () => void;
  197. }
  198. export type FormRef<Values = any> = FormInstance<Values> & {
  199. nativeElement?: HTMLElement;
  200. };
  201. export type InternalFormInstance = Omit<FormInstance, 'validateFields'> & {
  202. validateFields: InternalValidateFields;
  203. /**
  204. * Passed by field context props
  205. */
  206. prefixName?: InternalNamePath;
  207. validateTrigger?: string | string[] | false;
  208. /**
  209. * Form component should register some content into store.
  210. * We pass the `HOOK_MARK` as key to avoid user call the function.
  211. */
  212. getInternalHooks: (secret: string) => InternalHooks | null;
  213. /** @private Internal usage. Do not use it in your production */
  214. _init?: boolean;
  215. };
  216. export type EventArgs = any[];
  217. type ValidateMessage = string | (() => string);
  218. export interface ValidateMessages {
  219. default?: ValidateMessage;
  220. required?: ValidateMessage;
  221. enum?: ValidateMessage;
  222. whitespace?: ValidateMessage;
  223. date?: {
  224. format?: ValidateMessage;
  225. parse?: ValidateMessage;
  226. invalid?: ValidateMessage;
  227. };
  228. types?: {
  229. string?: ValidateMessage;
  230. method?: ValidateMessage;
  231. array?: ValidateMessage;
  232. object?: ValidateMessage;
  233. number?: ValidateMessage;
  234. date?: ValidateMessage;
  235. boolean?: ValidateMessage;
  236. integer?: ValidateMessage;
  237. float?: ValidateMessage;
  238. regexp?: ValidateMessage;
  239. email?: ValidateMessage;
  240. url?: ValidateMessage;
  241. hex?: ValidateMessage;
  242. };
  243. string?: {
  244. len?: ValidateMessage;
  245. min?: ValidateMessage;
  246. max?: ValidateMessage;
  247. range?: ValidateMessage;
  248. };
  249. number?: {
  250. len?: ValidateMessage;
  251. min?: ValidateMessage;
  252. max?: ValidateMessage;
  253. range?: ValidateMessage;
  254. };
  255. array?: {
  256. len?: ValidateMessage;
  257. min?: ValidateMessage;
  258. max?: ValidateMessage;
  259. range?: ValidateMessage;
  260. };
  261. pattern?: {
  262. mismatch?: ValidateMessage;
  263. };
  264. }
  265. export {};