react-mui-form-validator 1.0.3 → 1.0.6
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/.eslintrc.js +29 -0
- package/Readme.md +12 -33
- package/lib/components/MuiTextField.js +27 -52
- package/lib/components/MuiTextSelect.js +27 -52
- package/lib/core/context/index.js +12 -0
- package/lib/core/utils/utils.js +35 -0
- package/lib/core/{MuiRules.js → validator/ValidationRules.js} +0 -6
- package/lib/core/{MuiComponent.js → validator/ValidatorComponent.js} +23 -74
- package/lib/core/{MuiForm.js → validator/ValidatorForm.js} +39 -106
- package/lib/index.js +0 -8
- package/package.json +21 -18
- package/tsconfig.json +27 -0
- package/.eslintrc +0 -76
- package/lib/components/MuiCheckbox.js +0 -106
- package/lib/utils/utils.js +0 -53
|
@@ -4,121 +4,82 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
|
-
|
|
8
7
|
var _react = _interopRequireDefault(require("react"));
|
|
9
|
-
|
|
10
8
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
11
|
-
|
|
12
9
|
var _promisePolyfill = _interopRequireDefault(require("promise-polyfill"));
|
|
13
|
-
|
|
14
10
|
var _reactLifecyclesCompat = require("react-lifecycles-compat");
|
|
15
|
-
|
|
16
|
-
var _MuiForm = _interopRequireWildcard(require("./MuiForm"));
|
|
17
|
-
|
|
11
|
+
var _ValidatorForm = _interopRequireWildcard(require("./ValidatorForm"));
|
|
18
12
|
var _utils = require("../utils/utils");
|
|
19
|
-
|
|
20
13
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
21
|
-
|
|
22
14
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
|
-
|
|
24
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
25
|
-
|
|
26
16
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
27
|
-
|
|
28
17
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
29
|
-
|
|
30
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
31
|
-
|
|
18
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
32
19
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
33
|
-
|
|
34
20
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
35
|
-
|
|
36
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
37
|
-
|
|
21
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
38
22
|
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
39
|
-
|
|
40
23
|
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
41
|
-
|
|
42
24
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
43
|
-
|
|
44
25
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
45
|
-
|
|
46
|
-
function
|
|
47
|
-
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
var _super = _createSuper(MuiComponent);
|
|
54
|
-
|
|
55
|
-
function MuiComponent() {
|
|
26
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
27
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
28
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
29
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /* eslint-enable */
|
|
30
|
+
var ValidatorComponent = /*#__PURE__*/function (_React$Component) {
|
|
31
|
+
_inherits(ValidatorComponent, _React$Component);
|
|
32
|
+
var _super = _createSuper(ValidatorComponent);
|
|
33
|
+
function ValidatorComponent() {
|
|
56
34
|
var _this;
|
|
57
|
-
|
|
58
|
-
_classCallCheck(this, MuiComponent);
|
|
59
|
-
|
|
35
|
+
_classCallCheck(this, ValidatorComponent);
|
|
60
36
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
61
37
|
args[_key] = arguments[_key];
|
|
62
38
|
}
|
|
63
|
-
|
|
64
39
|
_this = _super.call.apply(_super, [this].concat(args));
|
|
65
|
-
|
|
66
40
|
_defineProperty(_assertThisInitialized(_this), "state", {
|
|
67
41
|
isValid: true,
|
|
68
42
|
value: _this.props.value,
|
|
69
43
|
errorMessages: _this.props.errorMessages,
|
|
70
44
|
validators: _this.props.validators
|
|
71
45
|
});
|
|
72
|
-
|
|
73
46
|
_defineProperty(_assertThisInitialized(_this), "getErrorMessage", function () {
|
|
74
47
|
var errorMessages = _this.state.errorMessages;
|
|
75
|
-
|
|
76
48
|
var type = _typeof(errorMessages);
|
|
77
|
-
|
|
78
49
|
if (type === 'string') {
|
|
79
50
|
return errorMessages;
|
|
80
51
|
} else if (type === 'object') {
|
|
81
52
|
if (_this.invalid.length > 0) {
|
|
82
53
|
return errorMessages[_this.invalid[0]];
|
|
83
54
|
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
55
|
+
}
|
|
56
|
+
// eslint-disable-next-line
|
|
87
57
|
console.log('unknown errorMessages type', errorMessages);
|
|
88
58
|
return true;
|
|
89
59
|
});
|
|
90
|
-
|
|
91
60
|
_defineProperty(_assertThisInitialized(_this), "instantValidate", true);
|
|
92
|
-
|
|
93
61
|
_defineProperty(_assertThisInitialized(_this), "invalid", []);
|
|
94
|
-
|
|
95
62
|
_defineProperty(_assertThisInitialized(_this), "configure", function () {
|
|
96
63
|
_this.form.attachToForm(_assertThisInitialized(_this));
|
|
97
|
-
|
|
98
64
|
_this.instantValidate = _this.form.instantValidate;
|
|
99
65
|
_this.debounceTime = _this.form.debounceTime;
|
|
100
66
|
_this.validateDebounced = (0, _utils.debounce)(_this.validate, _this.debounceTime);
|
|
101
67
|
});
|
|
102
|
-
|
|
103
68
|
_defineProperty(_assertThisInitialized(_this), "validate", function (value) {
|
|
104
69
|
var includeRequired = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
105
70
|
var dryRun = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
106
|
-
|
|
107
71
|
var validations = _promisePolyfill["default"].all(_this.state.validators.map(function (validator) {
|
|
108
|
-
return
|
|
72
|
+
return _ValidatorForm["default"].getValidator(validator, value, includeRequired);
|
|
109
73
|
}));
|
|
110
|
-
|
|
111
74
|
return validations.then(function (results) {
|
|
112
75
|
_this.invalid = [];
|
|
113
76
|
var valid = true;
|
|
114
77
|
results.forEach(function (result, key) {
|
|
115
78
|
if (!result) {
|
|
116
79
|
valid = false;
|
|
117
|
-
|
|
118
80
|
_this.invalid.push(key);
|
|
119
81
|
}
|
|
120
82
|
});
|
|
121
|
-
|
|
122
83
|
if (!dryRun) {
|
|
123
84
|
_this.setState({
|
|
124
85
|
isValid: valid
|
|
@@ -126,39 +87,31 @@ var MuiComponent = /*#__PURE__*/function (_React$Component) {
|
|
|
126
87
|
_this.props.validatorListener(_this.state.isValid);
|
|
127
88
|
});
|
|
128
89
|
}
|
|
129
|
-
|
|
130
90
|
return valid;
|
|
131
91
|
});
|
|
132
92
|
});
|
|
133
|
-
|
|
134
93
|
_defineProperty(_assertThisInitialized(_this), "isValid", function () {
|
|
135
94
|
return _this.state.isValid;
|
|
136
95
|
});
|
|
137
|
-
|
|
138
96
|
_defineProperty(_assertThisInitialized(_this), "makeInvalid", function () {
|
|
139
97
|
_this.setState({
|
|
140
98
|
isValid: false
|
|
141
99
|
});
|
|
142
100
|
});
|
|
143
|
-
|
|
144
101
|
_defineProperty(_assertThisInitialized(_this), "makeValid", function () {
|
|
145
102
|
_this.setState({
|
|
146
103
|
isValid: true
|
|
147
104
|
});
|
|
148
105
|
});
|
|
149
|
-
|
|
150
106
|
_defineProperty(_assertThisInitialized(_this), "renderComponent", function (form) {
|
|
151
107
|
if (!_this.form) {
|
|
152
108
|
_this.form = form;
|
|
153
109
|
}
|
|
154
|
-
|
|
155
|
-
return _this.renderMuiComponent();
|
|
110
|
+
return _this.renderValidatorComponent();
|
|
156
111
|
});
|
|
157
|
-
|
|
158
112
|
return _this;
|
|
159
113
|
}
|
|
160
|
-
|
|
161
|
-
_createClass(MuiComponent, [{
|
|
114
|
+
_createClass(ValidatorComponent, [{
|
|
162
115
|
key: "componentDidMount",
|
|
163
116
|
value: function componentDidMount() {
|
|
164
117
|
this.configure();
|
|
@@ -185,8 +138,7 @@ var MuiComponent = /*#__PURE__*/function (_React$Component) {
|
|
|
185
138
|
key: "render",
|
|
186
139
|
value: function render() {
|
|
187
140
|
var _this2 = this;
|
|
188
|
-
|
|
189
|
-
return /*#__PURE__*/_react["default"].createElement(_MuiForm.FormContext.Consumer, null, function (_ref) {
|
|
141
|
+
return /*#__PURE__*/_react["default"].createElement(_ValidatorForm.FormContext.Consumer, null, function (_ref) {
|
|
190
142
|
var form = _ref.form;
|
|
191
143
|
return /*#__PURE__*/_react["default"].createElement("div", _this2.props.containerProps, _this2.renderComponent(form));
|
|
192
144
|
});
|
|
@@ -201,17 +153,14 @@ var MuiComponent = /*#__PURE__*/function (_React$Component) {
|
|
|
201
153
|
errorMessages: nextProps.errorMessages
|
|
202
154
|
};
|
|
203
155
|
}
|
|
204
|
-
|
|
205
156
|
return {
|
|
206
157
|
value: nextProps.value
|
|
207
158
|
};
|
|
208
159
|
}
|
|
209
160
|
}]);
|
|
210
|
-
|
|
211
|
-
return MuiComponent;
|
|
161
|
+
return ValidatorComponent;
|
|
212
162
|
}(_react["default"].Component);
|
|
213
|
-
|
|
214
|
-
MuiComponent.propTypes = {
|
|
163
|
+
ValidatorComponent.propTypes = {
|
|
215
164
|
errorMessages: _propTypes["default"].oneOfType([_propTypes["default"].array, _propTypes["default"].string]),
|
|
216
165
|
validators: _propTypes["default"].array,
|
|
217
166
|
value: _propTypes["default"].any,
|
|
@@ -219,11 +168,11 @@ MuiComponent.propTypes = {
|
|
|
219
168
|
withRequiredValidator: _propTypes["default"].bool,
|
|
220
169
|
containerProps: _propTypes["default"].object
|
|
221
170
|
};
|
|
222
|
-
|
|
171
|
+
ValidatorComponent.defaultProps = {
|
|
223
172
|
errorMessages: 'error',
|
|
224
173
|
validators: [],
|
|
225
174
|
validatorListener: function validatorListener() {}
|
|
226
175
|
};
|
|
227
|
-
(0, _reactLifecyclesCompat.polyfill)(
|
|
228
|
-
var _default =
|
|
176
|
+
(0, _reactLifecyclesCompat.polyfill)(ValidatorComponent);
|
|
177
|
+
var _default = ValidatorComponent;
|
|
229
178
|
exports["default"] = _default;
|
|
@@ -1,73 +1,45 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
|
|
5
3
|
Object.defineProperty(exports, "__esModule", {
|
|
6
4
|
value: true
|
|
7
5
|
});
|
|
8
6
|
exports["default"] = exports.FormContext = void 0;
|
|
9
|
-
|
|
10
7
|
var _react = _interopRequireDefault(require("react"));
|
|
11
|
-
|
|
12
8
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
13
|
-
|
|
14
9
|
var _promisePolyfill = _interopRequireDefault(require("promise-polyfill"));
|
|
15
|
-
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
var _MuiRules = _interopRequireDefault(require("./MuiRules"));
|
|
19
|
-
|
|
10
|
+
var _index = _interopRequireDefault(require("../context/index"));
|
|
11
|
+
var _ValidationRules = _interopRequireDefault(require("./ValidationRules"));
|
|
20
12
|
var _excluded = ["onSubmit", "instantValidate", "onError", "debounceTime", "children"];
|
|
21
|
-
|
|
22
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
23
|
-
|
|
24
|
-
function _extends() { _extends = Object.assign
|
|
25
|
-
|
|
14
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
15
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
26
16
|
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
27
|
-
|
|
28
17
|
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
29
|
-
|
|
30
18
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
31
|
-
|
|
32
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
33
|
-
|
|
19
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
34
20
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
35
|
-
|
|
36
21
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
|
|
37
|
-
|
|
38
|
-
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
39
|
-
|
|
22
|
+
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
|
40
23
|
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
41
|
-
|
|
42
24
|
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
|
|
43
|
-
|
|
44
25
|
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
|
45
|
-
|
|
46
26
|
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
47
|
-
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
function
|
|
51
|
-
|
|
52
|
-
var FormContext = (0, _reactCreateThemeContext["default"])('form');
|
|
27
|
+
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
|
28
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
29
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
30
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /* eslint-disable */ /* eslint-enable */
|
|
31
|
+
var FormContext = (0, _index["default"])("form");
|
|
53
32
|
exports.FormContext = FormContext;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
var _super = _createSuper(MuiForm);
|
|
59
|
-
|
|
60
|
-
function MuiForm() {
|
|
33
|
+
var ValidatorForm = /*#__PURE__*/function (_React$Component) {
|
|
34
|
+
_inherits(ValidatorForm, _React$Component);
|
|
35
|
+
var _super = _createSuper(ValidatorForm);
|
|
36
|
+
function ValidatorForm() {
|
|
61
37
|
var _this;
|
|
62
|
-
|
|
63
|
-
_classCallCheck(this, MuiForm);
|
|
64
|
-
|
|
38
|
+
_classCallCheck(this, ValidatorForm);
|
|
65
39
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
66
40
|
args[_key] = arguments[_key];
|
|
67
41
|
}
|
|
68
|
-
|
|
69
42
|
_this = _super.call.apply(_super, [this].concat(args));
|
|
70
|
-
|
|
71
43
|
_defineProperty(_assertThisInitialized(_this), "getFormHelpers", function () {
|
|
72
44
|
return {
|
|
73
45
|
form: {
|
|
@@ -78,56 +50,41 @@ var MuiForm = /*#__PURE__*/function (_React$Component) {
|
|
|
78
50
|
}
|
|
79
51
|
};
|
|
80
52
|
});
|
|
81
|
-
|
|
82
53
|
_defineProperty(_assertThisInitialized(_this), "instantValidate", _this.props.instantValidate !== undefined ? _this.props.instantValidate : true);
|
|
83
|
-
|
|
84
54
|
_defineProperty(_assertThisInitialized(_this), "debounceTime", _this.props.debounceTime);
|
|
85
|
-
|
|
86
55
|
_defineProperty(_assertThisInitialized(_this), "childs", []);
|
|
87
|
-
|
|
88
56
|
_defineProperty(_assertThisInitialized(_this), "errors", []);
|
|
89
|
-
|
|
90
57
|
_defineProperty(_assertThisInitialized(_this), "attachToForm", function (component) {
|
|
91
58
|
if (_this.childs.indexOf(component) === -1) {
|
|
92
59
|
_this.childs.push(component);
|
|
93
60
|
}
|
|
94
61
|
});
|
|
95
|
-
|
|
96
62
|
_defineProperty(_assertThisInitialized(_this), "detachFromForm", function (component) {
|
|
97
63
|
var componentPos = _this.childs.indexOf(component);
|
|
98
|
-
|
|
99
64
|
if (componentPos !== -1) {
|
|
100
65
|
_this.childs = _this.childs.slice(0, componentPos).concat(_this.childs.slice(componentPos + 1));
|
|
101
66
|
}
|
|
102
67
|
});
|
|
103
|
-
|
|
104
68
|
_defineProperty(_assertThisInitialized(_this), "submit", function (event) {
|
|
105
69
|
if (event) {
|
|
106
70
|
event.preventDefault();
|
|
107
71
|
event.persist();
|
|
108
72
|
}
|
|
109
|
-
|
|
110
73
|
_this.errors = [];
|
|
111
|
-
|
|
112
74
|
_this.walk(_this.childs).then(function (result) {
|
|
113
75
|
if (_this.errors.length) {
|
|
114
76
|
_this.props.onError(_this.errors);
|
|
115
77
|
}
|
|
116
|
-
|
|
117
78
|
if (result) {
|
|
118
79
|
_this.props.onSubmit(event);
|
|
119
80
|
}
|
|
120
|
-
|
|
121
81
|
return result;
|
|
122
82
|
});
|
|
123
83
|
});
|
|
124
|
-
|
|
125
84
|
_defineProperty(_assertThisInitialized(_this), "walk", function (children, dryRun) {
|
|
126
85
|
var self = _assertThisInitialized(_this);
|
|
127
|
-
|
|
128
86
|
return new _promisePolyfill["default"](function (resolve) {
|
|
129
87
|
var result = true;
|
|
130
|
-
|
|
131
88
|
if (Array.isArray(children)) {
|
|
132
89
|
_promisePolyfill["default"].all(children.map(function (input) {
|
|
133
90
|
return self.checkInput(input, dryRun);
|
|
@@ -146,18 +103,15 @@ var MuiForm = /*#__PURE__*/function (_React$Component) {
|
|
|
146
103
|
}
|
|
147
104
|
});
|
|
148
105
|
});
|
|
149
|
-
|
|
150
106
|
_defineProperty(_assertThisInitialized(_this), "checkInput", function (input, dryRun) {
|
|
151
107
|
return new _promisePolyfill["default"](function (resolve) {
|
|
152
108
|
var result = true;
|
|
153
109
|
var validators = input.props.validators;
|
|
154
|
-
|
|
155
110
|
if (validators) {
|
|
156
111
|
_this.validate(input, true, dryRun).then(function (data) {
|
|
157
112
|
if (!data) {
|
|
158
113
|
result = false;
|
|
159
114
|
}
|
|
160
|
-
|
|
161
115
|
resolve(result);
|
|
162
116
|
});
|
|
163
117
|
} else {
|
|
@@ -165,7 +119,6 @@ var MuiForm = /*#__PURE__*/function (_React$Component) {
|
|
|
165
119
|
}
|
|
166
120
|
});
|
|
167
121
|
});
|
|
168
|
-
|
|
169
122
|
_defineProperty(_assertThisInitialized(_this), "validate", function (input, includeRequired, dryRun) {
|
|
170
123
|
return new _promisePolyfill["default"](function (resolve) {
|
|
171
124
|
var value = input.props.value;
|
|
@@ -173,24 +126,19 @@ var MuiForm = /*#__PURE__*/function (_React$Component) {
|
|
|
173
126
|
if (!valid) {
|
|
174
127
|
_this.errors.push(input);
|
|
175
128
|
}
|
|
176
|
-
|
|
177
129
|
resolve(valid);
|
|
178
130
|
});
|
|
179
131
|
});
|
|
180
132
|
});
|
|
181
|
-
|
|
182
133
|
_defineProperty(_assertThisInitialized(_this), "find", function (collection, fn) {
|
|
183
134
|
for (var i = 0, l = collection.length; i < l; i++) {
|
|
184
135
|
var item = collection[i];
|
|
185
|
-
|
|
186
136
|
if (fn(item)) {
|
|
187
137
|
return item;
|
|
188
138
|
}
|
|
189
139
|
}
|
|
190
|
-
|
|
191
140
|
return null;
|
|
192
141
|
});
|
|
193
|
-
|
|
194
142
|
_defineProperty(_assertThisInitialized(_this), "resetValidations", function () {
|
|
195
143
|
_this.childs.forEach(function (child) {
|
|
196
144
|
child.validateDebounced.cancel();
|
|
@@ -199,27 +147,23 @@ var MuiForm = /*#__PURE__*/function (_React$Component) {
|
|
|
199
147
|
});
|
|
200
148
|
});
|
|
201
149
|
});
|
|
202
|
-
|
|
203
150
|
_defineProperty(_assertThisInitialized(_this), "isFormValid", function () {
|
|
204
151
|
var dryRun = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
205
152
|
return _this.walk(_this.childs, dryRun);
|
|
206
153
|
});
|
|
207
|
-
|
|
208
154
|
return _this;
|
|
209
155
|
}
|
|
210
|
-
|
|
211
|
-
_createClass(MuiForm, [{
|
|
156
|
+
_createClass(ValidatorForm, [{
|
|
212
157
|
key: "render",
|
|
213
158
|
value: function render() {
|
|
214
159
|
// eslint-disable-next-line
|
|
215
160
|
var _this$props = this.props,
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
161
|
+
onSubmit = _this$props.onSubmit,
|
|
162
|
+
instantValidate = _this$props.instantValidate,
|
|
163
|
+
onError = _this$props.onError,
|
|
164
|
+
debounceTime = _this$props.debounceTime,
|
|
165
|
+
children = _this$props.children,
|
|
166
|
+
rest = _objectWithoutProperties(_this$props, _excluded);
|
|
223
167
|
return /*#__PURE__*/_react["default"].createElement(FormContext.Provider, {
|
|
224
168
|
value: this.getFormHelpers()
|
|
225
169
|
}, /*#__PURE__*/_react["default"].createElement("form", _extends({}, rest, {
|
|
@@ -227,55 +171,44 @@ var MuiForm = /*#__PURE__*/function (_React$Component) {
|
|
|
227
171
|
}), children));
|
|
228
172
|
}
|
|
229
173
|
}]);
|
|
230
|
-
|
|
231
|
-
return MuiForm;
|
|
174
|
+
return ValidatorForm;
|
|
232
175
|
}(_react["default"].Component);
|
|
233
|
-
|
|
234
|
-
_defineProperty(MuiForm, "getValidator", function (validator, value, includeRequired) {
|
|
176
|
+
_defineProperty(ValidatorForm, "getValidator", function (validator, value, includeRequired) {
|
|
235
177
|
var result = true;
|
|
236
178
|
var name = validator;
|
|
237
|
-
|
|
238
|
-
if (name !== 'required' || includeRequired) {
|
|
179
|
+
if (name !== "required" || includeRequired) {
|
|
239
180
|
var extra;
|
|
240
|
-
var splitIdx = validator.indexOf(
|
|
241
|
-
|
|
181
|
+
var splitIdx = validator.indexOf(":");
|
|
242
182
|
if (splitIdx !== -1) {
|
|
243
183
|
name = validator.substring(0, splitIdx);
|
|
244
184
|
extra = validator.substring(splitIdx + 1);
|
|
245
185
|
}
|
|
246
|
-
|
|
247
|
-
result = _MuiRules["default"][name](value, extra);
|
|
186
|
+
result = _ValidationRules["default"][name](value, extra);
|
|
248
187
|
}
|
|
249
|
-
|
|
250
188
|
return result;
|
|
251
189
|
});
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
_MuiRules["default"][name] = callback;
|
|
190
|
+
ValidatorForm.addValidationRule = function (name, callback) {
|
|
191
|
+
_ValidationRules["default"][name] = callback;
|
|
255
192
|
};
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
return _MuiRules["default"][name];
|
|
193
|
+
ValidatorForm.getValidationRule = function (name) {
|
|
194
|
+
return _ValidationRules["default"][name];
|
|
259
195
|
};
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return _MuiRules["default"][name] && typeof _MuiRules["default"][name] === 'function';
|
|
196
|
+
ValidatorForm.hasValidationRule = function (name) {
|
|
197
|
+
return _ValidationRules["default"][name] && typeof _ValidationRules["default"][name] === "function";
|
|
263
198
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
delete _MuiRules["default"][name];
|
|
199
|
+
ValidatorForm.removeValidationRule = function (name) {
|
|
200
|
+
delete _ValidationRules["default"][name];
|
|
267
201
|
};
|
|
268
|
-
|
|
269
|
-
MuiForm.propTypes = {
|
|
202
|
+
ValidatorForm.propTypes = {
|
|
270
203
|
onSubmit: _propTypes["default"].func.isRequired,
|
|
271
204
|
instantValidate: _propTypes["default"].bool,
|
|
272
205
|
children: _propTypes["default"].node,
|
|
273
206
|
onError: _propTypes["default"].func,
|
|
274
207
|
debounceTime: _propTypes["default"].number
|
|
275
208
|
};
|
|
276
|
-
|
|
209
|
+
ValidatorForm.defaultProps = {
|
|
277
210
|
onError: function onError() {},
|
|
278
211
|
debounceTime: 0
|
|
279
212
|
};
|
|
280
|
-
var _default =
|
|
213
|
+
var _default = ValidatorForm;
|
|
281
214
|
exports["default"] = _default;
|
package/lib/index.js
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _MuiComponent = _interopRequireDefault(require("./core/MuiComponent"));
|
|
4
|
-
|
|
5
4
|
var _MuiForm = _interopRequireDefault(require("./core/MuiForm"));
|
|
6
|
-
|
|
7
|
-
var _MuiTextSelect = _interopRequireDefault(require("./components/MuiTextSelect"));
|
|
8
|
-
|
|
9
5
|
var _MuiTextField = _interopRequireDefault(require("./components/MuiTextField"));
|
|
10
|
-
|
|
11
6
|
var _MuiCheckbox = _interopRequireDefault(require("./components/MuiCheckbox"));
|
|
12
|
-
|
|
13
7
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
14
|
-
|
|
15
|
-
exports.MuiTextSelect = _MuiTextSelect["default"];
|
|
16
8
|
exports.MuiTextField = _MuiTextField["default"];
|
|
17
9
|
exports.MuiCheckbox = _MuiCheckbox["default"];
|
|
18
10
|
exports.MuiComponent = _MuiComponent["default"];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-mui-form-validator",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Validator for forms designed with material-ui components.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,29 +27,32 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/Blencm/react-mui-form-validator#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"
|
|
31
|
-
"
|
|
30
|
+
"@mui/material": "^5.13.6",
|
|
31
|
+
"core-js": "^3.31.0",
|
|
32
|
+
"promise-polyfill": "^8.3.0",
|
|
32
33
|
"prop-types": "^15.8.1",
|
|
33
|
-
"react-
|
|
34
|
-
"
|
|
34
|
+
"react-lifecycles-compat": "^3.0.4",
|
|
35
|
+
"tiny-warning": "^1.0.3",
|
|
36
|
+
"typescript": "^5.1.6"
|
|
35
37
|
},
|
|
36
38
|
"peerDependencies": {
|
|
37
|
-
"react": "^16.0.0 || ^17.0.0 || ^18.
|
|
38
|
-
"react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0"
|
|
39
|
+
"react": "^16.0.0 || ^17.0.0 || ^18.2.0 || ^19.0.0 || ^20.0.0"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
|
-
"@babel/cli": "^7.
|
|
42
|
-
"@babel/core": "^7.
|
|
43
|
-
"@babel/plugin-syntax-jsx": "^7.
|
|
44
|
-
"@babel/preset-env": "^7.
|
|
45
|
-
"@babel/preset-react": "^7.
|
|
42
|
+
"@babel/cli": "^7.22.5",
|
|
43
|
+
"@babel/core": "^7.22.5",
|
|
44
|
+
"@babel/plugin-syntax-jsx": "^7.22.5",
|
|
45
|
+
"@babel/preset-env": "^7.22.5",
|
|
46
|
+
"@babel/preset-react": "^7.22.5",
|
|
47
|
+
"@types/react": "18.2.14",
|
|
46
48
|
"babel-eslint": "^10.1.0",
|
|
47
|
-
"babel-loader": "^
|
|
48
|
-
"eslint": "^8.
|
|
49
|
+
"babel-loader": "^9.1.2",
|
|
50
|
+
"eslint": "^8.44.0",
|
|
49
51
|
"eslint-config-airbnb": "^19.0.4",
|
|
50
|
-
"eslint-plugin-import": "^2.
|
|
51
|
-
"eslint-plugin-jsx-a11y": "6.
|
|
52
|
-
"eslint-plugin-react": "^7.
|
|
53
|
-
"
|
|
52
|
+
"eslint-plugin-import": "^2.27.5",
|
|
53
|
+
"eslint-plugin-jsx-a11y": "^6.7.1",
|
|
54
|
+
"eslint-plugin-react": "^7.32.2",
|
|
55
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
56
|
+
"rimraf": "^5.0.1"
|
|
54
57
|
}
|
|
55
58
|
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es6",
|
|
4
|
+
"lib": [
|
|
5
|
+
"dom",
|
|
6
|
+
"dom.iterable",
|
|
7
|
+
"esnext"
|
|
8
|
+
],
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"allowSyntheticDefaultImports": true,
|
|
13
|
+
"strict": false,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"noFallthroughCasesInSwitch": true,
|
|
16
|
+
"module": "esnext",
|
|
17
|
+
"moduleResolution": "node",
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"isolatedModules": true,
|
|
20
|
+
"noEmit": true,
|
|
21
|
+
"jsx": "react-jsx",
|
|
22
|
+
"baseUrl": "src"
|
|
23
|
+
},
|
|
24
|
+
"include": [
|
|
25
|
+
"src"
|
|
26
|
+
]
|
|
27
|
+
}
|