react-mui-form-validator 1.0.3 → 1.0.5

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 CHANGED
@@ -1,9 +1,7 @@
1
1
  {
2
- "parser" : "babel-eslint",
3
- "plugins": [
4
- "import"
5
- ],
6
- "extends" : ["airbnb"],
2
+ "parser": "babel-eslint",
3
+ "plugins": ["import"],
4
+ "extends": ["airbnb"],
7
5
  "rules": {
8
6
  // Soft some rules.
9
7
  "camelcase": "off",
@@ -12,7 +10,7 @@
12
10
  "indent": [2, 4, { "SwitchCase": 1, "VariableDeclarator": 1 }],
13
11
  "linebreak-style": "off",
14
12
  "max-len": "off",
15
- "new-cap": [2, {"capIsNew": false, "newIsCap": true}], // For Record() etc.
13
+ "new-cap": [2, { "capIsNew": false, "newIsCap": true }], // For Record() etc.
16
14
  "newline-per-chained-call": 0,
17
15
  "no-cond-assign": "off",
18
16
  "no-floating-decimal": 0, // .5 is just fine.
@@ -24,7 +22,7 @@
24
22
  "react/no-string-refs": 0,
25
23
  "no-underscore-dangle": "off",
26
24
  // eslint-plugin-import
27
- "import/no-unresolved": [2, {"commonjs": true}],
25
+ "import/no-unresolved": [2, { "commonjs": true }],
28
26
  "import/no-extraneous-dependencies": 0,
29
27
  "import/named": 2,
30
28
  "import/default": 2,
@@ -40,7 +38,7 @@
40
38
  "react/no-array-index-key": 0,
41
39
  "react/forbid-prop-types": 0,
42
40
  "react/require-default-props": 0,
43
- "jsx-a11y/anchor-has-content": 0,
41
+ "jsx-a11y/anchor-has-content": 0
44
42
  },
45
43
  "globals": {
46
44
  "after": false,
@@ -57,20 +55,13 @@
57
55
  "location": true,
58
56
  "XMLHttpRequest": true,
59
57
  "XDomainRequest": true,
60
- "Blob": true,
58
+ "Blob": true
61
59
  },
62
60
  "settings": {
63
- "import/ignore": [
64
- "node_modules",
65
- "\\.json$"
66
- ],
61
+ "import/ignore": ["node_modules", "\\.json$"],
67
62
  "import/parser": "babel-eslint",
68
63
  "import/resolve": {
69
- "extensions": [
70
- ".js",
71
- ".jsx",
72
- ".json"
73
- ]
64
+ "extensions": [".js", ".jsx", ".json"]
74
65
  }
75
66
  }
76
67
  }
package/Readme.md CHANGED
@@ -21,15 +21,6 @@ TextField
21
21
  />
22
22
  ```
23
23
 
24
- Select
25
- ```javascript
26
- <MuiTextSelect
27
- {...someProps}
28
- validators={["required"]}
29
- errorMessages={["this select is required"]}
30
- />
31
- ```
32
-
33
24
  Checkbox
34
25
  ```javascript
35
26
  <MuiCheckbox
@@ -69,7 +60,6 @@ class MyForm extends React.Component {
69
60
  const { email } = this.state;
70
61
  return (
71
62
  <MuiForm
72
- ref="form"
73
63
  onSubmit={this.handleSubmit}
74
64
  onError={(errors) => console.log(errors)}
75
65
  >
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-mui-form-validator",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Validator for forms designed with material-ui components.",
5
5
  "main": "./lib/index.js",
6
6
  "scripts": {
@@ -27,10 +27,10 @@
27
27
  },
28
28
  "homepage": "https://github.com/Blencm/react-mui-form-validator#readme",
29
29
  "dependencies": {
30
- "core-js": "^3.21.0",
31
- "promise-polyfill": "^8.2.1",
30
+ "core-js": "^3.21.1",
31
+ "mini-create-react-context": "^0.4.1",
32
+ "promise-polyfill": "^8.2.3",
32
33
  "prop-types": "^15.8.1",
33
- "react-create-theme-context": "^0.0.1",
34
34
  "react-lifecycles-compat": "^3.0.4"
35
35
  },
36
36
  "peerDependencies": {
@@ -38,18 +38,18 @@
38
38
  "react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@babel/cli": "^7.17.0",
42
- "@babel/core": "^7.17.2",
41
+ "@babel/cli": "^7.17.6",
42
+ "@babel/core": "^7.17.8",
43
43
  "@babel/plugin-syntax-jsx": "^7.16.7",
44
44
  "@babel/preset-env": "^7.16.11",
45
45
  "@babel/preset-react": "^7.16.7",
46
46
  "babel-eslint": "^10.1.0",
47
- "babel-loader": "^8.2.3",
48
- "eslint": "^8.9.0",
47
+ "babel-loader": "^8.2.4",
48
+ "eslint": "^8.11.0",
49
49
  "eslint-config-airbnb": "^19.0.4",
50
50
  "eslint-plugin-import": "^2.25.4",
51
51
  "eslint-plugin-jsx-a11y": "6.5.1",
52
- "eslint-plugin-react": "^7.28.0",
52
+ "eslint-plugin-react": "^7.29.4",
53
53
  "rimraf": "^3.0.2"
54
54
  }
55
55
  }
@@ -1,83 +0,0 @@
1
- "use strict";
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
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports["default"] = void 0;
9
-
10
- var _react = _interopRequireDefault(require("react"));
11
-
12
- var _TextField = _interopRequireDefault(require("@mui/material/TextField"));
13
-
14
- var _MuiComponent2 = _interopRequireDefault(require("../core/MuiComponent"));
15
-
16
- var _excluded = ["error", "errorMessages", "validators", "requiredError", "helperText", "validatorListener", "withRequiredValidator", "containerProps"];
17
-
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
19
-
20
- function _extends() { _extends = Object.assign || 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); }
21
-
22
- 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; }
23
-
24
- 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; }
25
-
26
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27
-
28
- 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); } }
29
-
30
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
31
-
32
- 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); }
33
-
34
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
35
-
36
- 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); }; }
37
-
38
- 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); }
39
-
40
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
41
-
42
- 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; } }
43
-
44
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
45
-
46
- var MuiTextSelect = /*#__PURE__*/function (_MuiComponent) {
47
- _inherits(MuiTextSelect, _MuiComponent);
48
-
49
- var _super = _createSuper(MuiTextSelect);
50
-
51
- function MuiTextSelect() {
52
- _classCallCheck(this, MuiTextSelect);
53
-
54
- return _super.apply(this, arguments);
55
- }
56
-
57
- _createClass(MuiTextSelect, [{
58
- key: "renderMuiComponent",
59
- value: function renderMuiComponent() {
60
- var _this$props = this.props,
61
- error = _this$props.error,
62
- errorMessages = _this$props.errorMessages,
63
- validators = _this$props.validators,
64
- requiredError = _this$props.requiredError,
65
- helperText = _this$props.helperText,
66
- validatorListener = _this$props.validatorListener,
67
- withRequiredValidator = _this$props.withRequiredValidator,
68
- containerProps = _this$props.containerProps,
69
- rest = _objectWithoutProperties(_this$props, _excluded);
70
-
71
- var isValid = this.state.isValid;
72
- return /*#__PURE__*/_react["default"].createElement(_TextField["default"], _extends({}, rest, {
73
- select: true,
74
- error: !isValid || error,
75
- helperText: !isValid && this.getErrorMessage() || helperText
76
- }));
77
- }
78
- }]);
79
-
80
- return MuiTextSelect;
81
- }(_MuiComponent2["default"]);
82
-
83
- exports["default"] = MuiTextSelect;
@@ -1,229 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports["default"] = void 0;
7
-
8
- var _react = _interopRequireDefault(require("react"));
9
-
10
- var _propTypes = _interopRequireDefault(require("prop-types"));
11
-
12
- var _promisePolyfill = _interopRequireDefault(require("promise-polyfill"));
13
-
14
- var _reactLifecyclesCompat = require("react-lifecycles-compat");
15
-
16
- var _MuiForm = _interopRequireWildcard(require("./MuiForm"));
17
-
18
- var _utils = require("../utils/utils");
19
-
20
- 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
- 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
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
25
-
26
- 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
- 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
-
32
- 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
- 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
-
38
- 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
- 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
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
43
-
44
- 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
47
-
48
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
49
-
50
- var MuiComponent = /*#__PURE__*/function (_React$Component) {
51
- _inherits(MuiComponent, _React$Component);
52
-
53
- var _super = _createSuper(MuiComponent);
54
-
55
- function MuiComponent() {
56
- var _this;
57
-
58
- _classCallCheck(this, MuiComponent);
59
-
60
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
61
- args[_key] = arguments[_key];
62
- }
63
-
64
- _this = _super.call.apply(_super, [this].concat(args));
65
-
66
- _defineProperty(_assertThisInitialized(_this), "state", {
67
- isValid: true,
68
- value: _this.props.value,
69
- errorMessages: _this.props.errorMessages,
70
- validators: _this.props.validators
71
- });
72
-
73
- _defineProperty(_assertThisInitialized(_this), "getErrorMessage", function () {
74
- var errorMessages = _this.state.errorMessages;
75
-
76
- var type = _typeof(errorMessages);
77
-
78
- if (type === 'string') {
79
- return errorMessages;
80
- } else if (type === 'object') {
81
- if (_this.invalid.length > 0) {
82
- return errorMessages[_this.invalid[0]];
83
- }
84
- } // eslint-disable-next-line
85
-
86
-
87
- console.log('unknown errorMessages type', errorMessages);
88
- return true;
89
- });
90
-
91
- _defineProperty(_assertThisInitialized(_this), "instantValidate", true);
92
-
93
- _defineProperty(_assertThisInitialized(_this), "invalid", []);
94
-
95
- _defineProperty(_assertThisInitialized(_this), "configure", function () {
96
- _this.form.attachToForm(_assertThisInitialized(_this));
97
-
98
- _this.instantValidate = _this.form.instantValidate;
99
- _this.debounceTime = _this.form.debounceTime;
100
- _this.validateDebounced = (0, _utils.debounce)(_this.validate, _this.debounceTime);
101
- });
102
-
103
- _defineProperty(_assertThisInitialized(_this), "validate", function (value) {
104
- var includeRequired = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
105
- var dryRun = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
106
-
107
- var validations = _promisePolyfill["default"].all(_this.state.validators.map(function (validator) {
108
- return _MuiForm["default"].getValidator(validator, value, includeRequired);
109
- }));
110
-
111
- return validations.then(function (results) {
112
- _this.invalid = [];
113
- var valid = true;
114
- results.forEach(function (result, key) {
115
- if (!result) {
116
- valid = false;
117
-
118
- _this.invalid.push(key);
119
- }
120
- });
121
-
122
- if (!dryRun) {
123
- _this.setState({
124
- isValid: valid
125
- }, function () {
126
- _this.props.validatorListener(_this.state.isValid);
127
- });
128
- }
129
-
130
- return valid;
131
- });
132
- });
133
-
134
- _defineProperty(_assertThisInitialized(_this), "isValid", function () {
135
- return _this.state.isValid;
136
- });
137
-
138
- _defineProperty(_assertThisInitialized(_this), "makeInvalid", function () {
139
- _this.setState({
140
- isValid: false
141
- });
142
- });
143
-
144
- _defineProperty(_assertThisInitialized(_this), "makeValid", function () {
145
- _this.setState({
146
- isValid: true
147
- });
148
- });
149
-
150
- _defineProperty(_assertThisInitialized(_this), "renderComponent", function (form) {
151
- if (!_this.form) {
152
- _this.form = form;
153
- }
154
-
155
- return _this.renderMuiComponent();
156
- });
157
-
158
- return _this;
159
- }
160
-
161
- _createClass(MuiComponent, [{
162
- key: "componentDidMount",
163
- value: function componentDidMount() {
164
- this.configure();
165
- }
166
- }, {
167
- key: "shouldComponentUpdate",
168
- value: function shouldComponentUpdate(nextProps, nextState) {
169
- return this.state !== nextState || this.props !== nextProps;
170
- }
171
- }, {
172
- key: "componentDidUpdate",
173
- value: function componentDidUpdate(prevProps, prevState) {
174
- if (this.instantValidate && this.props.value !== prevState.value) {
175
- this.validateDebounced(this.props.value, this.props.withRequiredValidator);
176
- }
177
- }
178
- }, {
179
- key: "componentWillUnmount",
180
- value: function componentWillUnmount() {
181
- this.form.detachFromForm(this);
182
- this.validateDebounced.cancel();
183
- }
184
- }, {
185
- key: "render",
186
- value: function render() {
187
- var _this2 = this;
188
-
189
- return /*#__PURE__*/_react["default"].createElement(_MuiForm.FormContext.Consumer, null, function (_ref) {
190
- var form = _ref.form;
191
- return /*#__PURE__*/_react["default"].createElement("div", _this2.props.containerProps, _this2.renderComponent(form));
192
- });
193
- }
194
- }], [{
195
- key: "getDerivedStateFromProps",
196
- value: function getDerivedStateFromProps(nextProps, prevState) {
197
- if (nextProps.validators && nextProps.errorMessages && (prevState.validators !== nextProps.validators || prevState.errorMessages !== nextProps.errorMessages)) {
198
- return {
199
- value: nextProps.value,
200
- validators: nextProps.validators,
201
- errorMessages: nextProps.errorMessages
202
- };
203
- }
204
-
205
- return {
206
- value: nextProps.value
207
- };
208
- }
209
- }]);
210
-
211
- return MuiComponent;
212
- }(_react["default"].Component);
213
-
214
- MuiComponent.propTypes = {
215
- errorMessages: _propTypes["default"].oneOfType([_propTypes["default"].array, _propTypes["default"].string]),
216
- validators: _propTypes["default"].array,
217
- value: _propTypes["default"].any,
218
- validatorListener: _propTypes["default"].func,
219
- withRequiredValidator: _propTypes["default"].bool,
220
- containerProps: _propTypes["default"].object
221
- };
222
- MuiComponent.defaultProps = {
223
- errorMessages: 'error',
224
- validators: [],
225
- validatorListener: function validatorListener() {}
226
- };
227
- (0, _reactLifecyclesCompat.polyfill)(MuiComponent);
228
- var _default = MuiComponent;
229
- exports["default"] = _default;
@@ -1,281 +0,0 @@
1
- "use strict";
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
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports["default"] = exports.FormContext = void 0;
9
-
10
- var _react = _interopRequireDefault(require("react"));
11
-
12
- var _propTypes = _interopRequireDefault(require("prop-types"));
13
-
14
- var _promisePolyfill = _interopRequireDefault(require("promise-polyfill"));
15
-
16
- var _reactCreateThemeContext = _interopRequireDefault(require("react-create-theme-context"));
17
-
18
- var _MuiRules = _interopRequireDefault(require("./MuiRules"));
19
-
20
- var _excluded = ["onSubmit", "instantValidate", "onError", "debounceTime", "children"];
21
-
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
23
-
24
- function _extends() { _extends = Object.assign || 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); }
25
-
26
- 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
- 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
- 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
-
34
- 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
- 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
-
40
- 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
- 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
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
45
-
46
- 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 _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
49
-
50
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
51
-
52
- var FormContext = (0, _reactCreateThemeContext["default"])('form');
53
- exports.FormContext = FormContext;
54
-
55
- var MuiForm = /*#__PURE__*/function (_React$Component) {
56
- _inherits(MuiForm, _React$Component);
57
-
58
- var _super = _createSuper(MuiForm);
59
-
60
- function MuiForm() {
61
- var _this;
62
-
63
- _classCallCheck(this, MuiForm);
64
-
65
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
66
- args[_key] = arguments[_key];
67
- }
68
-
69
- _this = _super.call.apply(_super, [this].concat(args));
70
-
71
- _defineProperty(_assertThisInitialized(_this), "getFormHelpers", function () {
72
- return {
73
- form: {
74
- attachToForm: _this.attachToForm,
75
- detachFromForm: _this.detachFromForm,
76
- instantValidate: _this.instantValidate,
77
- debounceTime: _this.debounceTime
78
- }
79
- };
80
- });
81
-
82
- _defineProperty(_assertThisInitialized(_this), "instantValidate", _this.props.instantValidate !== undefined ? _this.props.instantValidate : true);
83
-
84
- _defineProperty(_assertThisInitialized(_this), "debounceTime", _this.props.debounceTime);
85
-
86
- _defineProperty(_assertThisInitialized(_this), "childs", []);
87
-
88
- _defineProperty(_assertThisInitialized(_this), "errors", []);
89
-
90
- _defineProperty(_assertThisInitialized(_this), "attachToForm", function (component) {
91
- if (_this.childs.indexOf(component) === -1) {
92
- _this.childs.push(component);
93
- }
94
- });
95
-
96
- _defineProperty(_assertThisInitialized(_this), "detachFromForm", function (component) {
97
- var componentPos = _this.childs.indexOf(component);
98
-
99
- if (componentPos !== -1) {
100
- _this.childs = _this.childs.slice(0, componentPos).concat(_this.childs.slice(componentPos + 1));
101
- }
102
- });
103
-
104
- _defineProperty(_assertThisInitialized(_this), "submit", function (event) {
105
- if (event) {
106
- event.preventDefault();
107
- event.persist();
108
- }
109
-
110
- _this.errors = [];
111
-
112
- _this.walk(_this.childs).then(function (result) {
113
- if (_this.errors.length) {
114
- _this.props.onError(_this.errors);
115
- }
116
-
117
- if (result) {
118
- _this.props.onSubmit(event);
119
- }
120
-
121
- return result;
122
- });
123
- });
124
-
125
- _defineProperty(_assertThisInitialized(_this), "walk", function (children, dryRun) {
126
- var self = _assertThisInitialized(_this);
127
-
128
- return new _promisePolyfill["default"](function (resolve) {
129
- var result = true;
130
-
131
- if (Array.isArray(children)) {
132
- _promisePolyfill["default"].all(children.map(function (input) {
133
- return self.checkInput(input, dryRun);
134
- })).then(function (data) {
135
- data.forEach(function (item) {
136
- if (!item) {
137
- result = false;
138
- }
139
- });
140
- resolve(result);
141
- });
142
- } else {
143
- self.walk([children], dryRun).then(function (result) {
144
- return resolve(result);
145
- });
146
- }
147
- });
148
- });
149
-
150
- _defineProperty(_assertThisInitialized(_this), "checkInput", function (input, dryRun) {
151
- return new _promisePolyfill["default"](function (resolve) {
152
- var result = true;
153
- var validators = input.props.validators;
154
-
155
- if (validators) {
156
- _this.validate(input, true, dryRun).then(function (data) {
157
- if (!data) {
158
- result = false;
159
- }
160
-
161
- resolve(result);
162
- });
163
- } else {
164
- resolve(result);
165
- }
166
- });
167
- });
168
-
169
- _defineProperty(_assertThisInitialized(_this), "validate", function (input, includeRequired, dryRun) {
170
- return new _promisePolyfill["default"](function (resolve) {
171
- var value = input.props.value;
172
- input.validate(value, includeRequired, dryRun).then(function (valid) {
173
- if (!valid) {
174
- _this.errors.push(input);
175
- }
176
-
177
- resolve(valid);
178
- });
179
- });
180
- });
181
-
182
- _defineProperty(_assertThisInitialized(_this), "find", function (collection, fn) {
183
- for (var i = 0, l = collection.length; i < l; i++) {
184
- var item = collection[i];
185
-
186
- if (fn(item)) {
187
- return item;
188
- }
189
- }
190
-
191
- return null;
192
- });
193
-
194
- _defineProperty(_assertThisInitialized(_this), "resetValidations", function () {
195
- _this.childs.forEach(function (child) {
196
- child.validateDebounced.cancel();
197
- child.setState({
198
- isValid: true
199
- });
200
- });
201
- });
202
-
203
- _defineProperty(_assertThisInitialized(_this), "isFormValid", function () {
204
- var dryRun = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
205
- return _this.walk(_this.childs, dryRun);
206
- });
207
-
208
- return _this;
209
- }
210
-
211
- _createClass(MuiForm, [{
212
- key: "render",
213
- value: function render() {
214
- // eslint-disable-next-line
215
- var _this$props = this.props,
216
- onSubmit = _this$props.onSubmit,
217
- instantValidate = _this$props.instantValidate,
218
- onError = _this$props.onError,
219
- debounceTime = _this$props.debounceTime,
220
- children = _this$props.children,
221
- rest = _objectWithoutProperties(_this$props, _excluded);
222
-
223
- return /*#__PURE__*/_react["default"].createElement(FormContext.Provider, {
224
- value: this.getFormHelpers()
225
- }, /*#__PURE__*/_react["default"].createElement("form", _extends({}, rest, {
226
- onSubmit: this.submit
227
- }), children));
228
- }
229
- }]);
230
-
231
- return MuiForm;
232
- }(_react["default"].Component);
233
-
234
- _defineProperty(MuiForm, "getValidator", function (validator, value, includeRequired) {
235
- var result = true;
236
- var name = validator;
237
-
238
- if (name !== 'required' || includeRequired) {
239
- var extra;
240
- var splitIdx = validator.indexOf(':');
241
-
242
- if (splitIdx !== -1) {
243
- name = validator.substring(0, splitIdx);
244
- extra = validator.substring(splitIdx + 1);
245
- }
246
-
247
- result = _MuiRules["default"][name](value, extra);
248
- }
249
-
250
- return result;
251
- });
252
-
253
- MuiForm.addValidationRule = function (name, callback) {
254
- _MuiRules["default"][name] = callback;
255
- };
256
-
257
- MuiForm.getValidationRule = function (name) {
258
- return _MuiRules["default"][name];
259
- };
260
-
261
- MuiForm.hasValidationRule = function (name) {
262
- return _MuiRules["default"][name] && typeof _MuiRules["default"][name] === 'function';
263
- };
264
-
265
- MuiForm.removeValidationRule = function (name) {
266
- delete _MuiRules["default"][name];
267
- };
268
-
269
- MuiForm.propTypes = {
270
- onSubmit: _propTypes["default"].func.isRequired,
271
- instantValidate: _propTypes["default"].bool,
272
- children: _propTypes["default"].node,
273
- onError: _propTypes["default"].func,
274
- debounceTime: _propTypes["default"].number
275
- };
276
- MuiForm.defaultProps = {
277
- onError: function onError() {},
278
- debounceTime: 0
279
- };
280
- var _default = MuiForm;
281
- exports["default"] = _default;
@@ -1,86 +0,0 @@
1
- "use strict";
2
-
3
- var isExisty = function isExisty(value) {
4
- return value !== null && value !== undefined;
5
- };
6
-
7
- var _isEmpty = function isEmpty(value) {
8
- if (value instanceof Array) {
9
- return value.length === 0;
10
- }
11
-
12
- return value === '' || !isExisty(value);
13
- };
14
-
15
- var isEmptyTrimed = function isEmptyTrimed(value) {
16
- if (typeof value === 'string') {
17
- return value.trim() === '';
18
- }
19
-
20
- return true;
21
- };
22
-
23
- var validations = {
24
- matchRegexp: function matchRegexp(value, regexp) {
25
- var validationRegexp = regexp instanceof RegExp ? regexp : new RegExp(regexp);
26
- return _isEmpty(value) || validationRegexp.test(value);
27
- },
28
- // eslint-disable-next-line
29
- isEmail: function isEmail(value) {
30
- return validations.matchRegexp(value, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i);
31
- },
32
- isEmpty: function isEmpty(value) {
33
- return _isEmpty(value);
34
- },
35
- required: function required(value) {
36
- return !_isEmpty(value);
37
- },
38
- trim: function trim(value) {
39
- return !isEmptyTrimed(value);
40
- },
41
- isNumber: function isNumber(value) {
42
- return validations.matchRegexp(value, /^-?[0-9]\d*(\d+)?$/i);
43
- },
44
- isFloat: function isFloat(value) {
45
- return validations.matchRegexp(value, /^(?:-?[1-9]\d*|-?0)?(?:\.\d+)?$/i);
46
- },
47
- isPositive: function isPositive(value) {
48
- if (isExisty(value)) {
49
- return (validations.isNumber(value) || validations.isFloat(value)) && value >= 0;
50
- }
51
-
52
- return true;
53
- },
54
- maxNumber: function maxNumber(value, max) {
55
- return _isEmpty(value) || parseInt(value, 10) <= parseInt(max, 10);
56
- },
57
- minNumber: function minNumber(value, min) {
58
- return _isEmpty(value) || parseInt(value, 10) >= parseInt(min, 10);
59
- },
60
- maxFloat: function maxFloat(value, max) {
61
- return _isEmpty(value) || parseFloat(value) <= parseFloat(max);
62
- },
63
- minFloat: function minFloat(value, min) {
64
- return _isEmpty(value) || parseFloat(value) >= parseFloat(min);
65
- },
66
- isString: function isString(value) {
67
- return _isEmpty(value) || typeof value === 'string' || value instanceof String;
68
- },
69
- minStringLength: function minStringLength(value, length) {
70
- return validations.isString(value) && value.length >= length;
71
- },
72
- maxStringLength: function maxStringLength(value, length) {
73
- return validations.isString(value) && value.length <= length;
74
- },
75
- // eslint-disable-next-line no-undef
76
- isFile: function isFile(value) {
77
- return _isEmpty(value) || value instanceof File;
78
- },
79
- maxFileSize: function maxFileSize(value, max) {
80
- return _isEmpty(value) || validations.isFile(value) && value.size <= parseInt(max, 10);
81
- },
82
- allowedExtensions: function allowedExtensions(value, fileTypes) {
83
- return _isEmpty(value) || validations.isFile(value) && fileTypes.split(',').indexOf(value.type) !== -1;
84
- }
85
- };
86
- module.exports = validations;
package/lib/index.js DELETED
@@ -1,19 +0,0 @@
1
- "use strict";
2
-
3
- var _MuiComponent = _interopRequireDefault(require("./core/MuiComponent"));
4
-
5
- var _MuiForm = _interopRequireDefault(require("./core/MuiForm"));
6
-
7
- var _MuiTextSelect = _interopRequireDefault(require("./components/MuiTextSelect"));
8
-
9
- var _MuiTextField = _interopRequireDefault(require("./components/MuiTextField"));
10
-
11
- var _MuiCheckbox = _interopRequireDefault(require("./components/MuiCheckbox"));
12
-
13
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
14
-
15
- exports.MuiTextSelect = _MuiTextSelect["default"];
16
- exports.MuiTextField = _MuiTextField["default"];
17
- exports.MuiCheckbox = _MuiCheckbox["default"];
18
- exports.MuiComponent = _MuiComponent["default"];
19
- exports.MuiForm = _MuiForm["default"];
@@ -1,53 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.debounce = void 0;
7
-
8
- 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); } }
9
-
10
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
11
-
12
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
13
-
14
- var debounce = function debounce(func, wait, immediate) {
15
- var timeout;
16
-
17
- function cancel() {
18
- if (timeout !== undefined) {
19
- clearTimeout(timeout);
20
- }
21
- }
22
-
23
- var debounced = /*#__PURE__*/_createClass(function debounced() {
24
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
25
- args[_key] = arguments[_key];
26
- }
27
-
28
- _classCallCheck(this, debounced);
29
-
30
- var context = this;
31
-
32
- var later = function delayed() {
33
- timeout = null;
34
-
35
- if (!immediate) {
36
- func.apply(context, args);
37
- }
38
- };
39
-
40
- var callNow = immediate && !timeout;
41
- clearTimeout(timeout);
42
- timeout = setTimeout(later, wait);
43
-
44
- if (callNow) {
45
- func.apply(context, args);
46
- }
47
- });
48
-
49
- debounced.cancel = cancel;
50
- return debounced;
51
- };
52
-
53
- exports.debounce = debounce;