@hi-ui/form 4.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/lib/cjs/Form.js +106 -0
  4. package/lib/cjs/Form.js.map +1 -0
  5. package/lib/cjs/FormField.js +44 -0
  6. package/lib/cjs/FormField.js.map +1 -0
  7. package/lib/cjs/FormItem.js +61 -0
  8. package/lib/cjs/FormItem.js.map +1 -0
  9. package/lib/cjs/FormLabel.js +135 -0
  10. package/lib/cjs/FormLabel.js.map +1 -0
  11. package/lib/cjs/FormList.js +166 -0
  12. package/lib/cjs/FormList.js.map +1 -0
  13. package/lib/cjs/FormMessage.js +72 -0
  14. package/lib/cjs/FormMessage.js.map +1 -0
  15. package/lib/cjs/context.js +33 -0
  16. package/lib/cjs/context.js.map +1 -0
  17. package/lib/cjs/index.js +29 -0
  18. package/lib/cjs/index.js.map +1 -0
  19. package/lib/cjs/styles/index.scss.js +22 -0
  20. package/lib/cjs/styles/index.scss.js.map +1 -0
  21. package/lib/cjs/use-form-field.js +114 -0
  22. package/lib/cjs/use-form-field.js.map +1 -0
  23. package/lib/cjs/use-form.js +544 -0
  24. package/lib/cjs/use-form.js.map +1 -0
  25. package/lib/esm/Form.js +83 -0
  26. package/lib/esm/Form.js.map +1 -0
  27. package/lib/esm/FormField.js +35 -0
  28. package/lib/esm/FormField.js.map +1 -0
  29. package/lib/esm/FormItem.js +39 -0
  30. package/lib/esm/FormItem.js.map +1 -0
  31. package/lib/esm/FormLabel.js +113 -0
  32. package/lib/esm/FormLabel.js.map +1 -0
  33. package/lib/esm/FormList.js +145 -0
  34. package/lib/esm/FormList.js.map +1 -0
  35. package/lib/esm/FormMessage.js +51 -0
  36. package/lib/esm/FormMessage.js.map +1 -0
  37. package/lib/esm/context.js +25 -0
  38. package/lib/esm/context.js.map +1 -0
  39. package/lib/esm/index.js +14 -0
  40. package/lib/esm/index.js.map +1 -0
  41. package/lib/esm/styles/index.scss.js +17 -0
  42. package/lib/esm/styles/index.scss.js.map +1 -0
  43. package/lib/esm/use-form-field.js +94 -0
  44. package/lib/esm/use-form-field.js.map +1 -0
  45. package/lib/esm/use-form.js +519 -0
  46. package/lib/esm/use-form.js.map +1 -0
  47. package/lib/types/Form.d.ts +35 -0
  48. package/lib/types/FormField.d.ts +12 -0
  49. package/lib/types/FormItem.d.ts +13 -0
  50. package/lib/types/FormLabel.d.ts +28 -0
  51. package/lib/types/FormList.d.ts +15 -0
  52. package/lib/types/FormMessage.d.ts +27 -0
  53. package/lib/types/FormReset.d.ts +8 -0
  54. package/lib/types/FormSubmit.d.ts +8 -0
  55. package/lib/types/context.d.ts +55 -0
  56. package/lib/types/index.d.ts +6 -0
  57. package/lib/types/types.d.ts +113 -0
  58. package/lib/types/use-form-field.d.ts +37 -0
  59. package/lib/types/use-form.d.ts +78 -0
  60. package/lib/types/utils/index.d.ts +1 -0
  61. package/package.json +68 -0
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import { ButtonProps } from '@hi-ui/button';
3
+ /**
4
+ * TODO: What is FormReset
5
+ */
6
+ export declare const FormReset: React.ForwardRefExoticComponent<FormResetProps & React.RefAttributes<HTMLDivElement | null>>;
7
+ export interface FormResetProps extends ButtonProps {
8
+ }
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import { ButtonProps } from '@hi-ui/button';
3
+ /**
4
+ * TODO: What is FormSubmit
5
+ */
6
+ export declare const FormSubmit: React.ForwardRefExoticComponent<FormLabelProps & React.RefAttributes<HTMLDivElement | null>>;
7
+ export interface FormLabelProps extends ButtonProps {
8
+ }
@@ -0,0 +1,55 @@
1
+ /// <reference types="react" />
2
+ export declare const FormProvider: import("react").Provider<Omit<{
3
+ setFormState: (stateOrFunc: import("./types").FormSetState<unknown>) => void;
4
+ setFieldValue: (field: string, value: unknown, shouldValidate?: boolean | undefined) => void;
5
+ setFieldError: (field: import("./types").FormFieldPath, errorMessage: string | undefined) => void;
6
+ setFieldTouched: (field: import("./types").FormFieldPath, touched?: any) => void;
7
+ getFieldError: (fieldName: string) => unknown;
8
+ getFieldRules: (fieldName: string) => import("async-validator").RuleItem[];
9
+ getRootProps: () => {
10
+ onSubmit: (evt?: import("react").FormEvent<HTMLFormElement> | undefined) => void;
11
+ onReset: (evt?: import("react").FormEvent<HTMLFormElement> | undefined) => void;
12
+ };
13
+ getFieldProps: (props?: any, ref?: any) => {
14
+ [x: number]: unknown;
15
+ ref: any;
16
+ onBlur: (...args: any) => void;
17
+ invalid: unknown;
18
+ };
19
+ registerField: (key: string, value: import("./types").FormFieldCollection<unknown>) => void;
20
+ unregisterField: (key: string) => void;
21
+ submitForm: () => Promise<undefined>;
22
+ resetForm: (nextState?: Partial<import("./types").FormState<any>> | undefined) => Promise<void>;
23
+ values: unknown;
24
+ errors: import("./types").FormErrors<unknown>;
25
+ touched: import("./types").FormTouched<unknown>;
26
+ validating: boolean;
27
+ submitting?: boolean | undefined;
28
+ }, "rootProps"> | null>;
29
+ export declare const useFormContext: () => Omit<{
30
+ setFormState: (stateOrFunc: import("./types").FormSetState<unknown>) => void;
31
+ setFieldValue: (field: string, value: unknown, shouldValidate?: boolean | undefined) => void;
32
+ setFieldError: (field: import("./types").FormFieldPath, errorMessage: string | undefined) => void;
33
+ setFieldTouched: (field: import("./types").FormFieldPath, touched?: any) => void;
34
+ getFieldError: (fieldName: string) => unknown;
35
+ getFieldRules: (fieldName: string) => import("async-validator").RuleItem[];
36
+ getRootProps: () => {
37
+ onSubmit: (evt?: import("react").FormEvent<HTMLFormElement> | undefined) => void;
38
+ onReset: (evt?: import("react").FormEvent<HTMLFormElement> | undefined) => void;
39
+ };
40
+ getFieldProps: (props?: any, ref?: any) => {
41
+ [x: number]: unknown;
42
+ ref: any;
43
+ onBlur: (...args: any) => void;
44
+ invalid: unknown;
45
+ };
46
+ registerField: (key: string, value: import("./types").FormFieldCollection<unknown>) => void;
47
+ unregisterField: (key: string) => void;
48
+ submitForm: () => Promise<undefined>;
49
+ resetForm: (nextState?: Partial<import("./types").FormState<any>> | undefined) => Promise<void>;
50
+ values: unknown;
51
+ errors: import("./types").FormErrors<unknown>;
52
+ touched: import("./types").FormTouched<unknown>;
53
+ validating: boolean;
54
+ submitting?: boolean | undefined;
55
+ }, "rootProps">;
@@ -0,0 +1,6 @@
1
+ import './styles/index.scss';
2
+ export * from './Form';
3
+ export { Form as default } from './Form';
4
+ export * from './FormItem';
5
+ export * from './FormList';
6
+ export * from './types';
@@ -0,0 +1,113 @@
1
+ import { RuleItem } from 'async-validator';
2
+ import React from 'react';
3
+ export interface FormState<T> {
4
+ /**
5
+ * 字段及值的映射存储
6
+ */
7
+ values: T;
8
+ /**
9
+ * 字段及错误文案的映射存储
10
+ */
11
+ errors: FormErrors<T>;
12
+ /**
13
+ * 字段及是否触摸布尔值的映射存储
14
+ */
15
+ touched: FormTouched<T>;
16
+ /**
17
+ * 是否正在校验中
18
+ */
19
+ validating: boolean;
20
+ /**
21
+ * 是否正在提交中
22
+ */
23
+ submitting?: boolean;
24
+ }
25
+ export declare type FormTouched<T = any> = Record<string, T>;
26
+ export declare type FormErrors<T = any> = Record<string, T>;
27
+ export declare type FormSetState<T> = Partial<FormState<T>> | ((state: FormState<T>) => Partial<FormState<T>>);
28
+ export declare type FormAction<T> = {
29
+ type: 'SUBMIT_ATTEMPT';
30
+ } | {
31
+ type: 'SUBMIT_DONE';
32
+ } | {
33
+ type: 'SET_VALIDATING';
34
+ payload: boolean;
35
+ } | {
36
+ type: 'SET_SUBMITTING';
37
+ payload: boolean;
38
+ } | {
39
+ type: 'SET_VALUES';
40
+ payload: T;
41
+ } | {
42
+ type: 'SET_FIELD_VALUE';
43
+ payload: {
44
+ field: React.ReactText[];
45
+ value?: any;
46
+ };
47
+ } | {
48
+ type: 'SET_FIELD_TOUCHED';
49
+ payload: {
50
+ field: React.ReactText[];
51
+ value?: boolean;
52
+ };
53
+ } | {
54
+ type: 'SET_FIELD_ERROR';
55
+ payload: {
56
+ field: React.ReactText[];
57
+ value?: string;
58
+ };
59
+ } | {
60
+ type: 'SET_TOUCHED';
61
+ payload: FormTouched<T>;
62
+ } | {
63
+ type: 'SET_ERRORS';
64
+ payload: FormErrors<T>;
65
+ } | {
66
+ type: 'SET_STATUS';
67
+ payload: any;
68
+ } | {
69
+ type: 'SET_FORM';
70
+ payload: FormState<T>;
71
+ } | {
72
+ type: 'SET_STATE';
73
+ payload: FormSetState<T>;
74
+ };
75
+ export declare type FormValidateFunction<T = any> = (value: T) => string | Promise<string> | undefined;
76
+ export interface FormFieldCollection<T> {
77
+ validate: (value: any) => Promise<T>;
78
+ }
79
+ export declare type FormRuleModel = RuleItem;
80
+ export declare type FormRuleType = 'string' | 'number' | 'boolean' | 'method' | 'regexp' | 'integer' | 'float' | 'array' | 'object' | 'enum' | 'date' | 'url' | 'hex' | 'email' | 'any';
81
+ export interface FormHelpers<T = any> {
82
+ validate: FormValidateFunction<T>;
83
+ /**
84
+ * 对整个表单进行校验, 对应 Form.Submit中的 API
85
+ */
86
+ submit?: () => Promise<any>;
87
+ /**
88
+ * 重置整个表单的验证,对应 Form.Reset中的 API
89
+ */
90
+ reset?: (fields: [], toDefault: boolean) => Promise<any>;
91
+ /**
92
+ * 对指定表单字段进行校验
93
+ */
94
+ validateFields?: (fields?: string[] | string) => Promise<any>;
95
+ /**
96
+ * 设置表单的值,在异步获取的数据回显的时候,使用该方法
97
+ */
98
+ setFieldsValue?: (field: string, value: any) => void;
99
+ /**
100
+ * 获取一组字段名对应的 Values 返回为数组形式, 不传入 fields;默认返回全部信息, 不会触发表单校验
101
+ */
102
+ getFieldsValue?: (field: string) => any;
103
+ /**
104
+ * 获取一组字段名对应的错误信息,返回为数组形式, 不传入 fields;默认返回全部信息
105
+ */
106
+ getFieldsError?: (field: string) => any;
107
+ /**
108
+ * 移除表单项的校验结果。传入待移除的表单项的 field 属性组成的数组,如不传则移除整个表单的校验结果
109
+ */
110
+ clearValidates?: (fields: string[]) => void;
111
+ }
112
+ export declare type FormFieldPath = React.ReactText[] | React.ReactText;
113
+ export declare type FormErrorMessage = string;
@@ -0,0 +1,37 @@
1
+ import { FormFieldPath, FormRuleModel } from './types';
2
+ export declare const useFormField: <Values = any>(props: UseFormFieldProps<Values>) => {
3
+ [x: number]: unknown;
4
+ ref: any;
5
+ onBlur: (...args: any) => void;
6
+ invalid: unknown;
7
+ };
8
+ export interface UseFormFieldProps<T = Record<string, any>> {
9
+ /**
10
+ * 字段名,支持数组
11
+ */
12
+ field: FormFieldPath;
13
+ /**
14
+ * 指定控件值数据结构类型
15
+ */
16
+ valueType: 'string' | 'boolean' | 'number' | 'array' | 'object' | 'date';
17
+ /**
18
+ * 设置字段的验证规则,会覆盖 Form 设置的 rules
19
+ */
20
+ rules?: FormRuleModel[];
21
+ /**
22
+ * 自定义设置 Form 往表单控件注入值的属性,如 Switch Radio Checkbox 的是 'checked'
23
+ */
24
+ valuePropName?: string;
25
+ /**
26
+ * form 从控件个体采集数据的转换器,最终会把返回值转发给 Form
27
+ */
28
+ valueSync?: (value: any) => T;
29
+ /**
30
+ * 自定义设置 Form 从表单控件采集数据回调的属性
31
+ */
32
+ onChangePropName?: string;
33
+ /**
34
+ * 设置触发该字段校验的时机,会覆盖 Form 设置的 validateTrigger
35
+ */
36
+ validateTrigger?: string | string[];
37
+ }
@@ -0,0 +1,78 @@
1
+ import React from 'react';
2
+ import { FormState, FormFieldCollection, FormErrors, FormRuleModel, FormFieldPath, FormErrorMessage, FormSetState } from './types';
3
+ export declare const useForm: <Values = Record<string, any>>({ initialValues, initialErrors, initialTouched, lazyValidate, onValuesChange, onReset, onSubmit, rules, validateAfterTouched, validateTrigger: validateTriggerProp, ...rest }: UseFormProps<Values>) => {
4
+ setFormState: (stateOrFunc: FormSetState<Values>) => void;
5
+ setFieldValue: (field: string, value: unknown, shouldValidate?: boolean | undefined) => void;
6
+ setFieldError: (field: FormFieldPath, errorMessage: FormErrorMessage | undefined) => void;
7
+ setFieldTouched: (field: FormFieldPath, touched?: any) => void;
8
+ getFieldError: (fieldName: string) => unknown;
9
+ getFieldRules: (fieldName: string) => import("async-validator").RuleItem[];
10
+ getRootProps: () => {
11
+ onSubmit: (evt?: React.FormEvent<HTMLFormElement> | undefined) => void;
12
+ onReset: (evt?: React.FormEvent<HTMLFormElement> | undefined) => void;
13
+ };
14
+ getFieldProps: (props?: any, ref?: any) => {
15
+ [x: number]: unknown;
16
+ ref: any;
17
+ onBlur: (...args: any) => void;
18
+ invalid: unknown;
19
+ };
20
+ registerField: (key: string, value: FormFieldCollection<Values>) => void;
21
+ unregisterField: (key: string) => void;
22
+ submitForm: () => Promise<undefined>;
23
+ resetForm: (nextState?: Partial<FormState<any>> | undefined) => Promise<void>;
24
+ values: unknown;
25
+ errors: FormErrors<unknown>;
26
+ touched: import("./types").FormTouched<unknown>;
27
+ validating: boolean;
28
+ submitting?: boolean | undefined;
29
+ };
30
+ export interface UseFormProps<T = Record<string, any>> {
31
+ /**
32
+ * 初始化表单值
33
+ */
34
+ initialValues: T;
35
+ /**
36
+ * 初始化表单错误
37
+ */
38
+ initialErrors?: Record<keyof T, string>;
39
+ /**
40
+ * 初始化是否已被触碰
41
+ */
42
+ initialTouched?: Record<string, boolean>;
43
+ /**
44
+ * 校验规则,设置字段的校验逻辑
45
+ */
46
+ rules?: Record<string, FormRuleModel[]>;
47
+ /**
48
+ * 开启在 onBlur 时触发校验
49
+ */
50
+ /**
51
+ * 开启在 onChange 时触发校验
52
+ */
53
+ /**
54
+ * 设置统一的表单校验时机
55
+ */
56
+ validateTrigger?: string | string[];
57
+ /**
58
+ * 在触摸控件之后才开启校验
59
+ */
60
+ validateAfterTouched?: boolean;
61
+ /**
62
+ * 开启惰性校验,发现第一个检验失败的表单控件,就停止向下继续校验
63
+ */
64
+ lazyValidate?: boolean;
65
+ /**
66
+ * 字段值更新时触发回调事件:changedValues: 改变的表单对象,allValues: 所有表单项对象
67
+ */
68
+ onValuesChange?: (changedValue: T, allValues: T) => void;
69
+ /**
70
+ * 提交时回调
71
+ */
72
+ onSubmit?: (values: T) => void;
73
+ /**
74
+ * 重置时回调
75
+ */
76
+ onReset?: (values: T) => void | Promise<any>;
77
+ }
78
+ export declare type UseFormReturn = ReturnType<typeof useForm>;
@@ -0,0 +1 @@
1
+ export declare const setProp: (o: any, filed: any, value: any) => any;
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@hi-ui/form",
3
+ "version": "4.0.0-alpha.1",
4
+ "description": "A sub-package for @hi-ui/hiui.",
5
+ "keywords": [],
6
+ "author": "HIUI <mi-hiui@xiaomi.com>",
7
+ "homepage": "https://github.com/XiaoMi/hiui/tree/master/packages/ui/form#readme",
8
+ "license": "MIT",
9
+ "directories": {
10
+ "lib": "lib",
11
+ "test": "__tests__"
12
+ },
13
+ "files": [
14
+ "lib"
15
+ ],
16
+ "main": "lib/cjs/index.js",
17
+ "module": "lib/esm/index.js",
18
+ "types": "lib/types/index.d.ts",
19
+ "typings": "lib/types/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "require": "./lib/cjs/index.js",
23
+ "default": "./lib/esm/index.js"
24
+ }
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/XiaoMi/hiui.git"
32
+ },
33
+ "scripts": {
34
+ "test": "jest",
35
+ "clean": "rimraf lib",
36
+ "prebuild": "yarn clean",
37
+ "build:esm": "hi-build ./src/index.ts --format esm -d ./lib/esm",
38
+ "build:cjs": "hi-build ./src/index.ts --format cjs -d ./lib/cjs",
39
+ "build:types": "tsc --emitDeclarationOnly --declaration --declarationDir lib/types",
40
+ "build": "concurrently yarn:build:*"
41
+ },
42
+ "bugs": {
43
+ "url": "https://github.com/XiaoMi/hiui/issues"
44
+ },
45
+ "dependencies": {
46
+ "@flcwly/validater": "^0.1.4",
47
+ "@hi-ui/button": "^4.0.0-alpha.14",
48
+ "@hi-ui/classname": "^4.0.0-alpha.0",
49
+ "@hi-ui/core-css": "^4.0.0-alpha.9",
50
+ "@hi-ui/dom-utils": "^4.0.0-alpha.5",
51
+ "@hi-ui/env": "^4.0.0-alpha.4",
52
+ "@hi-ui/func-utils": "^4.0.0-alpha.4",
53
+ "@hi-ui/type-assertion": "^4.0.0-alpha.12",
54
+ "@hi-ui/use-latest": "^4.0.0-alpha.3",
55
+ "async-validator": "^4.0.7",
56
+ "yup": "^0.32.11"
57
+ },
58
+ "peerDependencies": {
59
+ "react": "^17.0.1",
60
+ "react-dom": "^17.0.1"
61
+ },
62
+ "devDependencies": {
63
+ "@hi-ui/hi-build": "^4.0.0-alpha.0",
64
+ "react": "^17.0.1",
65
+ "react-dom": "^17.0.1"
66
+ },
67
+ "gitHead": "37cbc875cd26474cfc9ddbd1eeb0abd797a31835"
68
+ }