Skip to content

Commit

Permalink
v1.5.0
Browse files Browse the repository at this point in the history
1. FormItem新增showRequiredMark属性
2. 细节调整
  • Loading branch information
bplok20010 committed Nov 8, 2019
1 parent 04399e9 commit 58f5ec3
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 46 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ labelPosition?: "top" | "left";
labelAlign?: "left" | "right";
controlStyle?: React.CSSProperties;
controlClassName?: string;
showRequiredMark?: boolean;
required?: boolean;
requiredMessage?: string;
clearErrorOnFocus?: boolean;
Expand Down Expand Up @@ -275,6 +276,10 @@ $form-item-cls: nex-form-item;
flex: none;
}

&-label-left {
text-align: left;
}

&-top {
display: block;
}
Expand Down Expand Up @@ -303,6 +308,7 @@ $form-item-cls: nex-form-item;
}
}


```


Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
@@ -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.b2e6f73b.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.b2e6f73b.chunk.js"></script></body></html>
<!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>
4 changes: 2 additions & 2 deletions docs/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"index.css": "static/css/index.b2e6f73b.chunk.css",
"index.js": "static/js/index.b2e6f73b.chunk.js",
"index.css": "static/css/index.d7a93482.chunk.css",
"index.js": "static/js/index.d7a93482.chunk.js",
"runtime~index.js": "static/js/runtime~index.f795885b.js",
"static/css/2.71a0092d.chunk.css": "static/css/2.71a0092d.chunk.css",
"static/js/2.71a0092d.chunk.js": "static/js/2.71a0092d.chunk.js",
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion docs/static/js/index.b2e6f73b.chunk.js

This file was deleted.

1 change: 1 addition & 0 deletions docs/static/js/index.d7a93482.chunk.js

Large diffs are not rendered by default.

34 changes: 24 additions & 10 deletions esm/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ function (_React$Component) {
formValue: _this.props.defaultFormValue || {}
});

_defineProperty(_assertThisInitialized(_this), "handleSubmit", function (e) {
e.preventDefault();
});

return _this;
}

Expand All @@ -63,7 +67,8 @@ function (_React$Component) {
var idx = this.fields.indexOf(field);

if (idx !== -1) {
var name = field.props.name;
var name = field.props.name; // eslint-disable-next-line

this.state.formError[name] = null;
this.fields.splice(idx, 1);
}
Expand All @@ -75,11 +80,14 @@ function (_React$Component) {
initialFormValue[field.props.name] = field._initialValue;
});
this.fieldLocks = {};
this.formLockId++;
this.state.validatingFields = {};
this.state.formValue = initialFormValue; // this.formError = {};
this.formLockId = 1; // eslint-disable-next-line

this.state.validatingFields = {}; // eslint-disable-next-line

this.state.formValue = initialFormValue; // eslint-disable-next-line

this.cleanErrors(); // this.setState({
this.state.formError = {}; // this.cleanErrors();
// this.setState({
// formError: {},
// validatingFields: {},
// formValue: initialFormValue
Expand All @@ -96,10 +104,12 @@ function (_React$Component) {
initialValue = field._initialValue;
}
});
this.fieldLocks[name] = 1;
this.state.validatingFields[name] = false; // this.formError[name] = null;
this.fieldLocks[name] = 1; // eslint-disable-next-line

this.state.validatingFields[name] = false; // eslint-disable-next-line

this.state.formError[name] = null; // this.cleanError(name);

this.cleanError(name);
this.setValue(name, initialValue, cb);
};

Expand Down Expand Up @@ -435,6 +445,8 @@ function (_React$Component) {
var validCounter = 0;

var updateFormState = function updateFormState() {
if (lockId !== _this3.formLockId) return;

_this3.setState({
formError: formError,
validatingFields: validatingFields
Expand All @@ -454,9 +466,12 @@ function (_React$Component) {
callback(allErrors.length ? allErrors : null, formValue, true
/* abort state */
);
console.log("abort");
return;
}

console.log("validate");

_this3.setState({
formError: formError,
validatingFields: {}
Expand Down Expand Up @@ -489,7 +504,6 @@ function (_React$Component) {
asyncTimer = null;
if (hasUpdate) return;
hasUpdate = true;
if (lockId !== _this3.formLockId) return;
updateFormState();
}, asyncTestDelay);

Expand Down Expand Up @@ -560,7 +574,7 @@ function (_React$Component) {
}, React.createElement(Component, {
style: style,
className: classnames(prefixCls, className),
onSubmit: onSubmit
onSubmit: onSubmit || this.handleSubmit
}, typeof children === "function" ? children(this) : children));
};

Expand Down
11 changes: 8 additions & 3 deletions esm/FormItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ function (_React$Component) {
};

_proto.getFormItemContext = function getFormItemContext() {
return Object.create(this);
return {
formItem: this
};
};

_proto.render = function render() {
Expand All @@ -262,6 +264,7 @@ function (_React$Component) {
var _this$props2 = this.props,
name = _this$props2.name,
label = _this$props2.label,
showRequiredMark = _this$props2.showRequiredMark,
required = _this$props2.required,
className = _this$props2.className,
prefixCls = _this$props2.prefixCls,
Expand Down Expand Up @@ -294,7 +297,7 @@ function (_React$Component) {
}, React.createElement("div", {
style: style,
ref: this.saveDOM,
className: classnames(prefixCls, (_classnames = {}, _classnames[prefixCls + "-inline"] = inline, _classnames[prefixCls + "-" + labelPosition] = labelPosition, _classnames["has-error"] = hasError, _classnames["is-validating"] = isValidating, _classnames["is-required"] = required, _classnames["" + className] = className, _classnames))
className: classnames(prefixCls, (_classnames = {}, _classnames[prefixCls + "-inline"] = inline, _classnames[prefixCls + "-" + labelPosition] = labelPosition, _classnames["has-error"] = hasError, _classnames["is-validating"] = isValidating, _classnames["is-required"] = required || showRequiredMark, _classnames["" + className] = className, _classnames))
}, label && React.createElement("label", {
htmlFor: this.getProp("labelFor"),
className: classnames((_classnames2 = {}, _classnames2[prefixCls + "-label"] = true, _classnames2[prefixCls + "-label-left"] = labelAlign === "left" && labelPosition === "left", _classnames2), this.getProp("labelClassName")),
Expand Down Expand Up @@ -327,6 +330,7 @@ FormItem.propTypes = process.env.NODE_ENV !== "production" ? {
controlStyle: PropTypes.object,
controlClassName: PropTypes.string,
validator: PropTypes.oneOfType([PropTypes.func, PropTypes.array]),
showRequiredMark: PropTypes.bool,
required: PropTypes.bool,
requiredMessage: PropTypes.string,
clearErrorOnFocus: PropTypes.bool,
Expand All @@ -338,6 +342,7 @@ FormItem.propTypes = process.env.NODE_ENV !== "production" ? {
inline: PropTypes.bool
} : {};
FormItem.defaultProps = {
prefixCls: "nex-form-item"
prefixCls: "nex-form-item",
showRequiredMark: false
};
export default FormItem;
1 change: 1 addition & 0 deletions esm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ declare namespace ReactWidgetForm {
labelAlign?: "left" | "right";
controlStyle?: React.CSSProperties;
controlClassName?: string;
showRequiredMark?: boolean;
required?: boolean;
requiredMessage?: string;
clearErrorOnFocus?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion esm/useFormItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
import React from "react";
import FormItemContext from "./FormItemContext";
export default (function () {
return React.useContext(FormItemContext);
var context = React.useContext(FormItemContext);
return context.formItem;
});
1 change: 0 additions & 1 deletion examples/demos/demo1.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export default class DEMO extends Component {
};

renderFieldExtra = component => {
console.log("update");
return (
<div
style={{
Expand Down
33 changes: 23 additions & 10 deletions lib/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function (_React$Component) {
validatingFields: {},
formValue: _this.props.defaultFormValue || {}
});
(0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleSubmit", function (e) {
e.preventDefault();
});
return _this;
}

Expand All @@ -76,7 +79,8 @@ function (_React$Component) {
var idx = this.fields.indexOf(field);

if (idx !== -1) {
var name = field.props.name;
var name = field.props.name; // eslint-disable-next-line

this.state.formError[name] = null;
this.fields.splice(idx, 1);
}
Expand All @@ -88,11 +92,14 @@ function (_React$Component) {
initialFormValue[field.props.name] = field._initialValue;
});
this.fieldLocks = {};
this.formLockId++;
this.state.validatingFields = {};
this.state.formValue = initialFormValue; // this.formError = {};
this.formLockId = 1; // eslint-disable-next-line

this.state.validatingFields = {}; // eslint-disable-next-line

this.cleanErrors(); // this.setState({
this.state.formValue = initialFormValue; // eslint-disable-next-line

this.state.formError = {}; // this.cleanErrors();
// this.setState({
// formError: {},
// validatingFields: {},
// formValue: initialFormValue
Expand All @@ -109,10 +116,12 @@ function (_React$Component) {
initialValue = field._initialValue;
}
});
this.fieldLocks[name] = 1;
this.state.validatingFields[name] = false; // this.formError[name] = null;
this.fieldLocks[name] = 1; // eslint-disable-next-line

this.state.validatingFields[name] = false; // eslint-disable-next-line

this.state.formError[name] = null; // this.cleanError(name);

this.cleanError(name);
this.setValue(name, initialValue, cb);
};

Expand Down Expand Up @@ -446,6 +455,8 @@ function (_React$Component) {
var validCounter = 0;

var updateFormState = function updateFormState() {
if (lockId !== _this3.formLockId) return;

_this3.setState({
formError: formError,
validatingFields: validatingFields
Expand All @@ -465,9 +476,12 @@ function (_React$Component) {
callback(allErrors.length ? allErrors : null, formValue, true
/* abort state */
);
console.log("abort");
return;
}

console.log("validate");

_this3.setState({
formError: formError,
validatingFields: {}
Expand Down Expand Up @@ -500,7 +514,6 @@ function (_React$Component) {
asyncTimer = null;
if (hasUpdate) return;
hasUpdate = true;
if (lockId !== _this3.formLockId) return;
updateFormState();
}, asyncTestDelay);

Expand Down Expand Up @@ -571,7 +584,7 @@ function (_React$Component) {
}, _react.default.createElement(Component, {
style: style,
className: (0, _classnames.default)(prefixCls, className),
onSubmit: onSubmit
onSubmit: onSubmit || this.handleSubmit
}, typeof children === "function" ? children(this) : children));
};

Expand Down
11 changes: 8 additions & 3 deletions lib/FormItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ function (_React$Component) {
};

_proto.getFormItemContext = function getFormItemContext() {
return Object.create(this);
return {
formItem: this
};
};

_proto.render = function render() {
Expand All @@ -274,6 +276,7 @@ function (_React$Component) {
var _this$props2 = this.props,
name = _this$props2.name,
label = _this$props2.label,
showRequiredMark = _this$props2.showRequiredMark,
required = _this$props2.required,
className = _this$props2.className,
prefixCls = _this$props2.prefixCls,
Expand Down Expand Up @@ -306,7 +309,7 @@ function (_React$Component) {
}, _react.default.createElement("div", {
style: style,
ref: this.saveDOM,
className: (0, _classnames3.default)(prefixCls, (_classnames = {}, _classnames[prefixCls + "-inline"] = inline, _classnames[prefixCls + "-" + labelPosition] = labelPosition, _classnames["has-error"] = hasError, _classnames["is-validating"] = isValidating, _classnames["is-required"] = required, _classnames["" + className] = className, _classnames))
className: (0, _classnames3.default)(prefixCls, (_classnames = {}, _classnames[prefixCls + "-inline"] = inline, _classnames[prefixCls + "-" + labelPosition] = labelPosition, _classnames["has-error"] = hasError, _classnames["is-validating"] = isValidating, _classnames["is-required"] = required || showRequiredMark, _classnames["" + className] = className, _classnames))
}, label && _react.default.createElement("label", {
htmlFor: this.getProp("labelFor"),
className: (0, _classnames3.default)((_classnames2 = {}, _classnames2[prefixCls + "-label"] = true, _classnames2[prefixCls + "-label-left"] = labelAlign === "left" && labelPosition === "left", _classnames2), this.getProp("labelClassName")),
Expand Down Expand Up @@ -338,6 +341,7 @@ FormItem.propTypes = process.env.NODE_ENV !== "production" ? {
controlStyle: _propTypes.default.object,
controlClassName: _propTypes.default.string,
validator: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.array]),
showRequiredMark: _propTypes.default.bool,
required: _propTypes.default.bool,
requiredMessage: _propTypes.default.string,
clearErrorOnFocus: _propTypes.default.bool,
Expand All @@ -349,7 +353,8 @@ FormItem.propTypes = process.env.NODE_ENV !== "production" ? {
inline: _propTypes.default.bool
} : {};
FormItem.defaultProps = {
prefixCls: "nex-form-item"
prefixCls: "nex-form-item",
showRequiredMark: false
};
var _default = FormItem;
exports.default = _default;
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ declare namespace ReactWidgetForm {
labelAlign?: "left" | "right";
controlStyle?: React.CSSProperties;
controlClassName?: string;
showRequiredMark?: boolean;
required?: boolean;
requiredMessage?: string;
clearErrorOnFocus?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion lib/useFormItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ var _react = _interopRequireDefault(require("react"));
var _FormItemContext = _interopRequireDefault(require("./FormItemContext"));

var _default = function _default() {
return _react.default.useContext(_FormItemContext.default);
var context = _react.default.useContext(_FormItemContext.default);

return context.formItem;
};

exports.default = _default;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-widget-form",
"version": "1.4.0",
"version": "1.5.0",
"description": "",
"main": "lib/index.js",
"module": "esm/index.js",
Expand Down
Loading

0 comments on commit 58f5ec3

Please sign in to comment.