Skip to content

Commit bf29ae4

Browse files
committed
v1.6.0
1. 细节调整 2. 新增getInitialFormValue,getInitialValue方法
1 parent 58f5ec3 commit bf29ae4

16 files changed

+118
-58
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ export default function NativeInput(props) {
201201
```ts
202202
{
203203
getValue(name: string): any;
204+
getInitialFormValue(): {};
204205
reset(cb: () => void): void;
206+
getInitialValue(name: string): any;
205207
resetField(cb: () => void): void;
206208
setValue(
207209
name: string,
@@ -238,6 +240,7 @@ export default function NativeInput(props) {
238240
{
239241
getDOM(): any;
240242
getForm(): Form;
243+
getInitialValue(): any;
241244
reset(cb: () => void): void;
242245
getValue(): any;
243246
setValue(value: any, callback: (formValue: {}) => void): void;

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html style="width:100%;height:100%;overflow:auto"><head><meta charset="utf-8"><title>Layout</title><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1"><style>.demo{width:1100px;height:500px;margin:50px auto;background:#fff;font-size:12px;overflow:auto}.rw-layout-content{height:200px}.rw-layout.rw-layout-has-sider{text-align:center;background:#3ba0e9}.rw-layout-sider{width:200px;text-align:center;background:#3ba0e9;color:#fff}.rw-layout-footer,.rw-layout-header{background:#7dbcea;color:#fff;height:64px;line-height:64px;text-align:center}.rw-layout-content{background:rgba(16,142,233,1);color:#fff;text-align:center}</style><link href="static/css/2.71a0092d.chunk.css" rel="stylesheet"><link href="static/css/index.d7a93482.chunk.css" rel="stylesheet"></head><body style="background:#f5f5f5"><div class="demo" id="demo"></div><script src="static/js/runtime~index.f795885b.js"></script><script src="static/js/2.71a0092d.chunk.js"></script><script src="static/js/index.d7a93482.chunk.js"></script></body></html>
1+
<!doctype html><html style="width:100%;height:100%;overflow:auto"><head><meta charset="utf-8"><title>Layout</title><meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1"><style>.demo{width:1100px;height:500px;margin:50px auto;background:#fff;font-size:12px;overflow:auto}.rw-layout-content{height:200px}.rw-layout.rw-layout-has-sider{text-align:center;background:#3ba0e9}.rw-layout-sider{width:200px;text-align:center;background:#3ba0e9;color:#fff}.rw-layout-footer,.rw-layout-header{background:#7dbcea;color:#fff;height:64px;line-height:64px;text-align:center}.rw-layout-content{background:rgba(16,142,233,1);color:#fff;text-align:center}</style><link href="static/css/2.71a0092d.chunk.css" rel="stylesheet"><link href="static/css/index.7ab493ed.chunk.css" rel="stylesheet"></head><body style="background:#f5f5f5"><div class="demo" id="demo"></div><script src="static/js/runtime~index.f795885b.js"></script><script src="static/js/2.71a0092d.chunk.js"></script><script src="static/js/index.7ab493ed.chunk.js"></script></body></html>

docs/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"index.css": "static/css/index.d7a93482.chunk.css",
3-
"index.js": "static/js/index.d7a93482.chunk.js",
2+
"index.css": "static/css/index.7ab493ed.chunk.css",
3+
"index.js": "static/js/index.7ab493ed.chunk.js",
44
"runtime~index.js": "static/js/runtime~index.f795885b.js",
55
"static/css/2.71a0092d.chunk.css": "static/css/2.71a0092d.chunk.css",
66
"static/js/2.71a0092d.chunk.js": "static/js/2.71a0092d.chunk.js",

docs/static/js/index.7ab493ed.chunk.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/static/js/index.d7a93482.chunk.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

esm/Form.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ function (_React$Component) {
7474
}
7575
};
7676

77-
_proto.reset = function reset(cb) {
77+
_proto.getInitialFormValue = function getInitialFormValue() {
7878
var initialFormValue = {};
7979
this.fields.forEach(function (field) {
8080
initialFormValue[field.props.name] = field._initialValue;
8181
});
82+
return initialFormValue;
83+
};
84+
85+
_proto.reset = function reset(cb) {
86+
var initialFormValue = this.getInitialFormValue();
8287
this.fieldLocks = {};
8388
this.formLockId = 1; // eslint-disable-next-line
8489

@@ -96,14 +101,19 @@ function (_React$Component) {
96101
this.setValues({}, cb);
97102
};
98103

99-
_proto.resetField = function resetField(name, cb) {
100-
this.cleanError(name);
104+
_proto.getInitialValue = function getInitialValue(name) {
101105
var initialValue;
102106
this.fields.forEach(function (field) {
103107
if (field.props.name === name) {
104108
initialValue = field._initialValue;
105109
}
106110
});
111+
return initialValue;
112+
};
113+
114+
_proto.resetField = function resetField(name, cb) {
115+
this.cleanError(name);
116+
var initialValue = this.getInitialValue(name);
107117
this.fieldLocks[name] = 1; // eslint-disable-next-line
108118

109119
this.state.validatingFields[name] = false; // eslint-disable-next-line
@@ -130,9 +140,8 @@ function (_React$Component) {
130140
path2obj = _this$props.path2obj,
131141
onChange = _this$props.onChange;
132142
var formValue = this.state.formValue;
133-
134-
var nextFormValue = _extends({}, formValue);
135-
143+
var isControlled = "formValue" in this.props;
144+
var nextFormValue = formValue;
136145
Object.keys(obj).forEach(function (name) {
137146
var value = obj[name];
138147

@@ -143,7 +152,7 @@ function (_React$Component) {
143152
}
144153
});
145154

146-
if (!("formValue" in this.props)) {
155+
if (!isControlled) {
147156
this.setState({
148157
formValue: nextFormValue
149158
});
@@ -174,17 +183,17 @@ function (_React$Component) {
174183
var _this$props2 = this.props,
175184
path2obj = _this$props2.path2obj,
176185
onChange = _this$props2.onChange;
177-
var formValue = this.state.formValue; // TODO: 后面再考虑下特殊场景
178-
179-
var nextFormValue = _extends({}, formValue);
186+
var formValue = this.state.formValue;
187+
var isControlled = "formValue" in this.props;
188+
var nextFormValue = formValue;
180189

181190
if (path2obj) {
182191
set(nextFormValue, name, value);
183192
} else {
184193
nextFormValue[name] = value;
185194
}
186195

187-
if (!("formValue" in this.props)) {
196+
if (!isControlled) {
188197
this.setState({
189198
formValue: nextFormValue
190199
});

esm/FormItem.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ function (_React$Component) {
3030
_defineProperty(_assertThisInitialized(_this), "_validateTimer", null);
3131

3232
_defineProperty(_assertThisInitialized(_this), "handleChange", function (value, callback) {
33-
var name = _this.props.name;
33+
var oldValue = _this.getValue(); //是否有必要检测?
3434

35-
var oldValue = _this.getValue();
35+
36+
if (value === oldValue) {
37+
return;
38+
}
3639

3740
_this.setValue(value, function (formValue) {
38-
if (formValue[name]
39-
/*newValue*/
40-
=== oldValue
41-
/*oldValue*/
42-
) return;
4341
callback && callback();
4442

4543
if (_this.hasValidateTrigger("change")) {
@@ -106,6 +104,12 @@ function (_React$Component) {
106104
return "validateDelay" in props ? props.validateDelay : validateDelay;
107105
};
108106

107+
_proto.getInitialValue = function getInitialValue() {
108+
var name = this.props.name;
109+
var form = this.getForm();
110+
return form.getInitialValue(name);
111+
};
112+
109113
_proto.reset = function reset(cb) {
110114
var form = this.getForm();
111115
var name = this.props.name;
@@ -199,7 +203,7 @@ function (_React$Component) {
199203
var getInputProps = this.getFormProp("getInputProps", function () {
200204
return {};
201205
});
202-
var customProps = getInputProps(this); //valueTrigger 收集子节点的值的时机 待开发...
206+
var customProps = getInputProps(this); //valueTrigger 收集子节点的值的时机,暂不开发...
203207

204208
return _extends({
205209
value: this.getValue()

esm/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ declare namespace ReactWidgetForm {
114114
value: any,
115115
callback: (formValue: {}) => void
116116
): void;
117+
getInitialFormValue(): {};
117118
reset(cb: () => void): void;
119+
getInitialValue(name: string): any;
118120
resetField(cb: () => void): void;
119121
hasError(name: string): boolean;
120122
getError(name: string): any;
@@ -153,6 +155,7 @@ declare namespace ReactWidgetForm {
153155
export class FormItem extends React.Component<FormItemProps, {}> {
154156
getDOM(): any;
155157
getForm(): Form;
158+
getInitialValue(): any;
156159
reset(cb: () => void): void;
157160
getValue(): any;
158161
setValue(value: any, callback: (formValue: {}) => void): void;

lib/Form.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,16 @@ function (_React$Component) {
8686
}
8787
};
8888

89-
_proto.reset = function reset(cb) {
89+
_proto.getInitialFormValue = function getInitialFormValue() {
9090
var initialFormValue = {};
9191
this.fields.forEach(function (field) {
9292
initialFormValue[field.props.name] = field._initialValue;
9393
});
94+
return initialFormValue;
95+
};
96+
97+
_proto.reset = function reset(cb) {
98+
var initialFormValue = this.getInitialFormValue();
9499
this.fieldLocks = {};
95100
this.formLockId = 1; // eslint-disable-next-line
96101

@@ -108,14 +113,19 @@ function (_React$Component) {
108113
this.setValues({}, cb);
109114
};
110115

111-
_proto.resetField = function resetField(name, cb) {
112-
this.cleanError(name);
116+
_proto.getInitialValue = function getInitialValue(name) {
113117
var initialValue;
114118
this.fields.forEach(function (field) {
115119
if (field.props.name === name) {
116120
initialValue = field._initialValue;
117121
}
118122
});
123+
return initialValue;
124+
};
125+
126+
_proto.resetField = function resetField(name, cb) {
127+
this.cleanError(name);
128+
var initialValue = this.getInitialValue(name);
119129
this.fieldLocks[name] = 1; // eslint-disable-next-line
120130

121131
this.state.validatingFields[name] = false; // eslint-disable-next-line
@@ -142,7 +152,8 @@ function (_React$Component) {
142152
path2obj = _this$props.path2obj,
143153
onChange = _this$props.onChange;
144154
var formValue = this.state.formValue;
145-
var nextFormValue = (0, _extends7.default)({}, formValue);
155+
var isControlled = "formValue" in this.props;
156+
var nextFormValue = formValue;
146157
Object.keys(obj).forEach(function (name) {
147158
var value = obj[name];
148159

@@ -153,7 +164,7 @@ function (_React$Component) {
153164
}
154165
});
155166

156-
if (!("formValue" in this.props)) {
167+
if (!isControlled) {
157168
this.setState({
158169
formValue: nextFormValue
159170
});
@@ -184,17 +195,17 @@ function (_React$Component) {
184195
var _this$props2 = this.props,
185196
path2obj = _this$props2.path2obj,
186197
onChange = _this$props2.onChange;
187-
var formValue = this.state.formValue; // TODO: 后面再考虑下特殊场景
188-
189-
var nextFormValue = (0, _extends7.default)({}, formValue);
198+
var formValue = this.state.formValue;
199+
var isControlled = "formValue" in this.props;
200+
var nextFormValue = formValue;
190201

191202
if (path2obj) {
192203
(0, _set.default)(nextFormValue, name, value);
193204
} else {
194205
nextFormValue[name] = value;
195206
}
196207

197-
if (!("formValue" in this.props)) {
208+
if (!isControlled) {
198209
this.setState({
199210
formValue: nextFormValue
200211
});

0 commit comments

Comments
 (0)