rsuite 5.11.0 → 5.12.0
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.
- package/CHANGELOG.md +6 -0
- package/cjs/Form/Form.js +21 -7
- package/cjs/Form/FormContext.d.ts +18 -13
- package/cjs/Form/useSchemaModel.d.ts +9 -0
- package/cjs/Form/useSchemaModel.js +46 -0
- package/cjs/FormControl/FormControl.d.ts +3 -0
- package/cjs/FormControl/FormControl.js +9 -2
- package/cjs/FormControl/useRegisterModel.d.ts +4 -0
- package/cjs/FormControl/useRegisterModel.js +20 -0
- package/dist/rsuite.js +24 -2
- package/dist/rsuite.js.map +1 -1
- package/dist/rsuite.min.js +1 -1
- package/dist/rsuite.min.js.map +1 -1
- package/esm/Form/Form.js +19 -7
- package/esm/Form/FormContext.d.ts +18 -13
- package/esm/Form/useSchemaModel.d.ts +9 -0
- package/esm/Form/useSchemaModel.js +39 -0
- package/esm/FormControl/FormControl.d.ts +3 -0
- package/esm/FormControl/FormControl.js +9 -6
- package/esm/FormControl/useRegisterModel.d.ts +4 -0
- package/esm/FormControl/useRegisterModel.js +14 -0
- package/package.json +1 -1
package/esm/Form/Form.js
CHANGED
|
@@ -12,6 +12,7 @@ import FormErrorMessage from '../FormErrorMessage';
|
|
|
12
12
|
import FormGroup from '../FormGroup';
|
|
13
13
|
import FormHelpText from '../FormHelpText';
|
|
14
14
|
import { useFormClassNames } from './useFormClassNames';
|
|
15
|
+
import useSchemaModel from './useSchemaModel';
|
|
15
16
|
var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
16
17
|
var _props$checkTrigger = props.checkTrigger,
|
|
17
18
|
checkTrigger = _props$checkTrigger === void 0 ? 'change' : _props$checkTrigger,
|
|
@@ -27,7 +28,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
27
28
|
_props$layout = props.layout,
|
|
28
29
|
layout = _props$layout === void 0 ? 'vertical' : _props$layout,
|
|
29
30
|
_props$model = props.model,
|
|
30
|
-
|
|
31
|
+
formModel = _props$model === void 0 ? SchemaModel({}) : _props$model,
|
|
31
32
|
readOnly = props.readOnly,
|
|
32
33
|
plaintext = props.plaintext,
|
|
33
34
|
className = props.className,
|
|
@@ -39,6 +40,11 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
39
40
|
onChange = props.onChange,
|
|
40
41
|
rest = _objectWithoutPropertiesLoose(props, ["checkTrigger", "classPrefix", "errorFromContext", "formDefaultValue", "formValue", "formError", "fluid", "layout", "model", "readOnly", "plaintext", "className", "children", "disabled", "onSubmit", "onCheck", "onError", "onChange"]);
|
|
41
42
|
|
|
43
|
+
var _useSchemaModel = useSchemaModel(formModel),
|
|
44
|
+
getCombinedModel = _useSchemaModel.getCombinedModel,
|
|
45
|
+
pushFieldRule = _useSchemaModel.pushFieldRule,
|
|
46
|
+
removeFieldRule = _useSchemaModel.removeFieldRule;
|
|
47
|
+
|
|
42
48
|
var classes = useFormClassNames({
|
|
43
49
|
classPrefix: classPrefix,
|
|
44
50
|
className: className,
|
|
@@ -73,6 +79,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
73
79
|
var formValue = getFormValue() || {};
|
|
74
80
|
var formError = {};
|
|
75
81
|
var errorCount = 0;
|
|
82
|
+
var model = getCombinedModel();
|
|
76
83
|
Object.keys(model.spec).forEach(function (key) {
|
|
77
84
|
var checkResult = model.checkForField(key, formValue);
|
|
78
85
|
|
|
@@ -91,7 +98,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
91
98
|
}
|
|
92
99
|
|
|
93
100
|
return true;
|
|
94
|
-
}, [
|
|
101
|
+
}, [getFormValue, getCombinedModel, onCheck, onError]);
|
|
95
102
|
/**
|
|
96
103
|
* Check the data field
|
|
97
104
|
* @param fieldName
|
|
@@ -102,6 +109,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
102
109
|
var _extends2;
|
|
103
110
|
|
|
104
111
|
var formValue = getFormValue() || {};
|
|
112
|
+
var model = getCombinedModel();
|
|
105
113
|
var checkResult = model.checkForField(fieldName, formValue);
|
|
106
114
|
|
|
107
115
|
var formError = _extends({}, getFormError(), (_extends2 = {}, _extends2[fieldName] = (checkResult === null || checkResult === void 0 ? void 0 : checkResult.errorMessage) || checkResult, _extends2));
|
|
@@ -115,7 +123,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
return !checkResult.hasError;
|
|
118
|
-
}, [
|
|
126
|
+
}, [getFormValue, getCombinedModel, getFormError, onCheck, onError]);
|
|
119
127
|
/**
|
|
120
128
|
* Check form data asynchronously and return a Promise
|
|
121
129
|
*/
|
|
@@ -124,6 +132,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
124
132
|
var formValue = getFormValue() || {};
|
|
125
133
|
var promises = [];
|
|
126
134
|
var keys = [];
|
|
135
|
+
var model = getCombinedModel();
|
|
127
136
|
Object.keys(model.spec).forEach(function (key) {
|
|
128
137
|
keys.push(key);
|
|
129
138
|
promises.push(model.checkForFieldAsync(key, formValue));
|
|
@@ -151,7 +160,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
151
160
|
formError: formError
|
|
152
161
|
};
|
|
153
162
|
});
|
|
154
|
-
}, [
|
|
163
|
+
}, [getFormValue, getCombinedModel, onCheck, onError]);
|
|
155
164
|
/**
|
|
156
165
|
* Asynchronously check form fields and return Promise
|
|
157
166
|
* @param fieldName
|
|
@@ -159,6 +168,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
159
168
|
|
|
160
169
|
var checkForFieldAsync = useCallback(function (fieldName) {
|
|
161
170
|
var formValue = getFormValue() || {};
|
|
171
|
+
var model = getCombinedModel();
|
|
162
172
|
return model.checkForFieldAsync(fieldName, formValue).then(function (checkResult) {
|
|
163
173
|
var _extends3;
|
|
164
174
|
|
|
@@ -173,7 +183,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
173
183
|
|
|
174
184
|
return checkResult;
|
|
175
185
|
});
|
|
176
|
-
}, [
|
|
186
|
+
}, [getFormValue, getCombinedModel, getFormError, onCheck, onError]);
|
|
177
187
|
var cleanErrors = useCallback(function () {
|
|
178
188
|
setFormError({});
|
|
179
189
|
}, []);
|
|
@@ -244,7 +254,7 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
244
254
|
var rootRef = useRef(null);
|
|
245
255
|
var formContextValue = useMemo(function () {
|
|
246
256
|
return {
|
|
247
|
-
|
|
257
|
+
getCombinedModel: getCombinedModel,
|
|
248
258
|
checkTrigger: checkTrigger,
|
|
249
259
|
formDefaultValue: formDefaultValue,
|
|
250
260
|
errorFromContext: errorFromContext,
|
|
@@ -254,11 +264,13 @@ var Form = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
254
264
|
formError: getFormError(),
|
|
255
265
|
removeFieldValue: removeFieldValue,
|
|
256
266
|
removeFieldError: removeFieldError,
|
|
267
|
+
pushFieldRule: pushFieldRule,
|
|
268
|
+
removeFieldRule: removeFieldRule,
|
|
257
269
|
onFieldChange: handleFieldChange,
|
|
258
270
|
onFieldError: handleFieldError,
|
|
259
271
|
onFieldSuccess: handleFieldSuccess
|
|
260
272
|
};
|
|
261
|
-
}, [
|
|
273
|
+
}, [getCombinedModel, checkTrigger, formDefaultValue, errorFromContext, readOnly, plaintext, disabled, getFormError, removeFieldValue, removeFieldError, pushFieldRule, removeFieldRule, handleFieldChange, handleFieldError, handleFieldSuccess]);
|
|
262
274
|
return /*#__PURE__*/React.createElement("form", _extends({}, rest, {
|
|
263
275
|
ref: rootRef,
|
|
264
276
|
onSubmit: handleSubmit,
|
|
@@ -1,26 +1,31 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { Schema } from 'schema-typed';
|
|
3
2
|
import { TypeAttributes } from '../@types/common';
|
|
4
|
-
|
|
3
|
+
import type { Schema } from 'schema-typed';
|
|
4
|
+
import type { FieldRuleType } from './useSchemaModel';
|
|
5
|
+
interface TrulyFormContextValue<T = Record<string, any>, errorMsgType = any, E = {
|
|
5
6
|
[P in keyof T]?: errorMsgType;
|
|
6
7
|
}> {
|
|
7
|
-
|
|
8
|
+
getCombinedModel: () => Schema;
|
|
9
|
+
formError: E;
|
|
10
|
+
removeFieldValue: (name: string) => void;
|
|
11
|
+
removeFieldError: (name: string) => void;
|
|
12
|
+
pushFieldRule: (name: string, fieldRule: FieldRuleType) => void;
|
|
13
|
+
removeFieldRule: (name: string) => void;
|
|
14
|
+
onFieldChange: (name: string, value: any, event: React.SyntheticEvent) => void;
|
|
15
|
+
onFieldError: (name: string, errorMessage: string) => void;
|
|
16
|
+
onFieldSuccess: (name: string) => void;
|
|
17
|
+
}
|
|
18
|
+
declare type ExternalPropsContextValue<T> = {
|
|
8
19
|
checkTrigger?: TypeAttributes.CheckTrigger;
|
|
9
20
|
formDefaultValue?: T;
|
|
10
21
|
errorFromContext?: boolean;
|
|
11
22
|
readOnly?: boolean;
|
|
12
23
|
plaintext?: boolean;
|
|
13
24
|
disabled?: boolean;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
onFieldError?: (name: string, errorMessage: string) => void;
|
|
19
|
-
onFieldSuccess?: (name: string) => void;
|
|
20
|
-
}
|
|
21
|
-
export declare const FormContext: React.Context<FormContextValue<Record<string, any>, any, {
|
|
22
|
-
[x: string]: any;
|
|
23
|
-
}>>;
|
|
25
|
+
};
|
|
26
|
+
declare type InitialContextType = Partial<Record<keyof TrulyFormContextValue, undefined>>;
|
|
27
|
+
export declare type FormContextValue<T = Record<string, any>, errorMsgType = any> = (TrulyFormContextValue<T, errorMsgType> | InitialContextType) & ExternalPropsContextValue<T>;
|
|
28
|
+
export declare const FormContext: React.Context<FormContextValue<Record<string, any>, any>>;
|
|
24
29
|
export declare const FormValueContext: React.Context<Record<string, any> | undefined>;
|
|
25
30
|
export declare const FormPlaintextContext: React.Context<boolean>;
|
|
26
31
|
export default FormContext;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MutableRefObject } from 'react';
|
|
2
|
+
import type { CheckType, Schema } from 'schema-typed';
|
|
3
|
+
export declare type FieldRuleType = MutableRefObject<CheckType<unknown, any> | undefined>;
|
|
4
|
+
declare function useSchemaModel(formModel: Schema): {
|
|
5
|
+
getCombinedModel: () => Schema<any, string>;
|
|
6
|
+
pushFieldRule: (name: string, fieldRule: FieldRuleType) => void;
|
|
7
|
+
removeFieldRule: (name: string) => void;
|
|
8
|
+
};
|
|
9
|
+
export default useSchemaModel;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SchemaModel } from 'schema-typed';
|
|
2
|
+
import { useRef, useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
function useSchemaModel(formModel) {
|
|
5
|
+
var subRulesRef = useRef([]);
|
|
6
|
+
var pushFieldRule = useCallback(function (name, fieldRule) {
|
|
7
|
+
subRulesRef.current.push({
|
|
8
|
+
name: name,
|
|
9
|
+
fieldRule: fieldRule
|
|
10
|
+
});
|
|
11
|
+
}, []);
|
|
12
|
+
var removeFieldRule = useCallback(function (name) {
|
|
13
|
+
var index = subRulesRef.current.findIndex(function (v) {
|
|
14
|
+
return v.name === name;
|
|
15
|
+
});
|
|
16
|
+
subRulesRef.current.splice(index, 1);
|
|
17
|
+
}, []);
|
|
18
|
+
var getCombinedModel = useCallback(function () {
|
|
19
|
+
var realSubRules = subRulesRef.current.filter(function (v) {
|
|
20
|
+
return Boolean(v.fieldRule.current);
|
|
21
|
+
});
|
|
22
|
+
return SchemaModel.combine(formModel, SchemaModel(realSubRules.map(function (_ref) {
|
|
23
|
+
var _ref2;
|
|
24
|
+
|
|
25
|
+
var name = _ref.name,
|
|
26
|
+
fieldRule = _ref.fieldRule;
|
|
27
|
+
return _ref2 = {}, _ref2[name] = fieldRule.current, _ref2;
|
|
28
|
+
}).reduce(function (a, b) {
|
|
29
|
+
return Object.assign(a, b);
|
|
30
|
+
}, {})));
|
|
31
|
+
}, [formModel]);
|
|
32
|
+
return {
|
|
33
|
+
getCombinedModel: getCombinedModel,
|
|
34
|
+
pushFieldRule: pushFieldRule,
|
|
35
|
+
removeFieldRule: removeFieldRule
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default useSchemaModel;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import Input from '../Input';
|
|
3
3
|
import { TypeAttributes, FormControlBaseProps, WithAsProps } from '../@types/common';
|
|
4
|
+
import type { CheckType } from 'schema-typed';
|
|
4
5
|
/**
|
|
5
6
|
* Props that FormControl passes to its accepter
|
|
6
7
|
*/
|
|
@@ -30,6 +31,8 @@ export interface FormControlProps<P = any, ValueType = any> extends WithAsProps,
|
|
|
30
31
|
checkAsync?: boolean;
|
|
31
32
|
/** Remove field value and error message when component is unmounted */
|
|
32
33
|
shouldResetWithUnmount?: boolean;
|
|
34
|
+
/** Validation rule */
|
|
35
|
+
rule?: CheckType<unknown, any>;
|
|
33
36
|
}
|
|
34
37
|
interface FormControlComponent extends React.FC<FormControlProps> {
|
|
35
38
|
<Accepter extends React.ElementType = typeof Input>(props: FormControlProps & {
|
|
@@ -13,10 +13,7 @@ import { useClassNames } from '../utils';
|
|
|
13
13
|
import FormContext, { FormValueContext } from '../Form/FormContext';
|
|
14
14
|
import { FormGroupContext } from '../FormGroup/FormGroup';
|
|
15
15
|
import { useWillUnmount } from '../utils';
|
|
16
|
-
|
|
17
|
-
* Props that FormControl passes to its accepter
|
|
18
|
-
*/
|
|
19
|
-
|
|
16
|
+
import useRegisterModel from './useRegisterModel';
|
|
20
17
|
var FormControl = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
21
18
|
var _useContext = useContext(FormContext),
|
|
22
19
|
readOnlyContext = _useContext.readOnly,
|
|
@@ -28,10 +25,12 @@ var FormControl = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
28
25
|
formError = _useContext.formError,
|
|
29
26
|
removeFieldValue = _useContext.removeFieldValue,
|
|
30
27
|
removeFieldError = _useContext.removeFieldError,
|
|
28
|
+
pushFieldRule = _useContext.pushFieldRule,
|
|
29
|
+
removeFieldRule = _useContext.removeFieldRule,
|
|
31
30
|
onFieldChange = _useContext.onFieldChange,
|
|
32
31
|
onFieldError = _useContext.onFieldError,
|
|
33
32
|
onFieldSuccess = _useContext.onFieldSuccess,
|
|
34
|
-
|
|
33
|
+
getCombinedModel = _useContext.getCombinedModel,
|
|
35
34
|
contextCheckTrigger = _useContext.checkTrigger;
|
|
36
35
|
|
|
37
36
|
var _props$as = props.as,
|
|
@@ -59,7 +58,8 @@ var FormControl = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
59
58
|
defaultValue = props.defaultValue,
|
|
60
59
|
_props$shouldResetWit = props.shouldResetWithUnmount,
|
|
61
60
|
shouldResetWithUnmount = _props$shouldResetWit === void 0 ? false : _props$shouldResetWit,
|
|
62
|
-
|
|
61
|
+
rule = props.rule,
|
|
62
|
+
rest = _objectWithoutPropertiesLoose(props, ["as", "accepter", "classPrefix", "className", "checkAsync", "checkTrigger", "errorPlacement", "errorMessage", "name", "value", "readOnly", "plaintext", "disabled", "onChange", "onBlur", "defaultValue", "shouldResetWithUnmount", "rule"]);
|
|
63
63
|
|
|
64
64
|
var _useContext2 = useContext(FormGroupContext),
|
|
65
65
|
controlId = _useContext2.controlId;
|
|
@@ -68,6 +68,7 @@ var FormControl = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
68
68
|
throw new Error("\n <FormControl> must be inside a component decorated with <Form>.\n And need to update React to 16.6.0 +.\n ");
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
useRegisterModel(name, pushFieldRule, removeFieldRule, rule);
|
|
71
72
|
useWillUnmount(function () {
|
|
72
73
|
if (shouldResetWithUnmount) {
|
|
73
74
|
removeFieldValue === null || removeFieldValue === void 0 ? void 0 : removeFieldValue(name);
|
|
@@ -114,6 +115,8 @@ var FormControl = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
|
114
115
|
|
|
115
116
|
var nextFormValue = _extends({}, formValue, (_extends2 = {}, _extends2[name] = value, _extends2));
|
|
116
117
|
|
|
118
|
+
var model = getCombinedModel();
|
|
119
|
+
|
|
117
120
|
if (checkAsync) {
|
|
118
121
|
return model === null || model === void 0 ? void 0 : model.checkForFieldAsync(name, nextFormValue).then(function (checkResult) {
|
|
119
122
|
return callbackEvents(checkResult);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { FieldRuleType } from '../Form/useSchemaModel';
|
|
2
|
+
import type { CheckType } from 'schema-typed';
|
|
3
|
+
declare function useRegisterModel(name: string, pushFieldRule: (n: string, r: FieldRuleType) => void, removeFieldRule: (n: string) => void, rule?: CheckType<unknown, any>): void;
|
|
4
|
+
export default useRegisterModel;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useRef, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
function useRegisterModel(name, pushFieldRule, removeFieldRule, rule) {
|
|
4
|
+
var refRule = useRef(rule);
|
|
5
|
+
refRule.current = rule;
|
|
6
|
+
useEffect(function () {
|
|
7
|
+
pushFieldRule(name, refRule);
|
|
8
|
+
return function () {
|
|
9
|
+
removeFieldRule(name);
|
|
10
|
+
};
|
|
11
|
+
}, [name, pushFieldRule, removeFieldRule]);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default useRegisterModel;
|