react-mui-form-validator 1.0.6 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +69 -0
- package/Readme.md +12 -12
- package/dist/index.d.ts +122 -0
- package/dist/index.js +601 -0
- package/package.json +23 -16
- package/tsconfig.json +16 -17
- package/.eslintrc.js +0 -29
- package/lib/components/MuiTextField.js +0 -57
- package/lib/components/MuiTextSelect.js +0 -58
- package/lib/core/context/index.js +0 -12
- package/lib/core/utils/utils.js +0 -35
- package/lib/core/validator/ValidationRules.js +0 -80
- package/lib/core/validator/ValidatorComponent.js +0 -178
- package/lib/core/validator/ValidatorForm.js +0 -214
- package/lib/index.js +0 -11
package/dist/index.js
ADDED
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
9
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
10
|
+
};
|
|
11
|
+
var __export = (target, all) => {
|
|
12
|
+
for (var name in all)
|
|
13
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
+
|
|
33
|
+
// src/core/validator/ValidationRules.jsx
|
|
34
|
+
var require_ValidationRules = __commonJS({
|
|
35
|
+
"src/core/validator/ValidationRules.jsx"(exports, module2) {
|
|
36
|
+
"use strict";
|
|
37
|
+
var isExisty = function(value) {
|
|
38
|
+
return value !== null && value !== void 0;
|
|
39
|
+
};
|
|
40
|
+
var isEmpty = function(value) {
|
|
41
|
+
if (value instanceof Array) {
|
|
42
|
+
return value.length === 0;
|
|
43
|
+
}
|
|
44
|
+
return value === "" || !isExisty(value);
|
|
45
|
+
};
|
|
46
|
+
var isEmptyTrimed = function(value) {
|
|
47
|
+
if (typeof value === "string") {
|
|
48
|
+
return value.trim() === "";
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
var validations = {
|
|
53
|
+
matchRegexp: (value, regexp) => {
|
|
54
|
+
const validationRegexp = regexp instanceof RegExp ? regexp : new RegExp(regexp);
|
|
55
|
+
return isEmpty(value) || validationRegexp.test(value);
|
|
56
|
+
},
|
|
57
|
+
// eslint-disable-next-line
|
|
58
|
+
isEmail: (value) => 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),
|
|
59
|
+
isEmpty: (value) => isEmpty(value),
|
|
60
|
+
required: (value) => !isEmpty(value),
|
|
61
|
+
trim: (value) => !isEmptyTrimed(value),
|
|
62
|
+
isNumber: (value) => validations.matchRegexp(value, /^-?[0-9]\d*(\d+)?$/i),
|
|
63
|
+
isFloat: (value) => validations.matchRegexp(value, /^(?:-?[1-9]\d*|-?0)?(?:\.\d+)?$/i),
|
|
64
|
+
isPositive: (value) => {
|
|
65
|
+
if (isExisty(value)) {
|
|
66
|
+
return (validations.isNumber(value) || validations.isFloat(value)) && value >= 0;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
},
|
|
70
|
+
maxNumber: (value, max) => isEmpty(value) || parseInt(value, 10) <= parseInt(max, 10),
|
|
71
|
+
minNumber: (value, min) => isEmpty(value) || parseInt(value, 10) >= parseInt(min, 10),
|
|
72
|
+
maxFloat: (value, max) => isEmpty(value) || parseFloat(value) <= parseFloat(max),
|
|
73
|
+
minFloat: (value, min) => isEmpty(value) || parseFloat(value) >= parseFloat(min),
|
|
74
|
+
isString: (value) => isEmpty(value) || typeof value === "string" || value instanceof String,
|
|
75
|
+
minStringLength: (value, length) => validations.isString(value) && value.length >= length,
|
|
76
|
+
maxStringLength: (value, length) => validations.isString(value) && value.length <= length,
|
|
77
|
+
// eslint-disable-next-line no-undef
|
|
78
|
+
isFile: (value) => isEmpty(value) || value instanceof File,
|
|
79
|
+
maxFileSize: (value, max) => isEmpty(value) || validations.isFile(value) && value.size <= parseInt(max, 10),
|
|
80
|
+
allowedExtensions: (value, fileTypes) => isEmpty(value) || validations.isFile(value) && fileTypes.split(",").indexOf(value.type) !== -1
|
|
81
|
+
};
|
|
82
|
+
module2.exports = validations;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// src/index.ts
|
|
87
|
+
var src_exports = {};
|
|
88
|
+
__export(src_exports, {
|
|
89
|
+
MuiComponent: () => MuiComponent,
|
|
90
|
+
MuiForm: () => MuiForm,
|
|
91
|
+
MuiSelect: () => MuiSelect2,
|
|
92
|
+
MuiTextField: () => MuiTextField
|
|
93
|
+
});
|
|
94
|
+
module.exports = __toCommonJS(src_exports);
|
|
95
|
+
|
|
96
|
+
// src/components/MuiTextField.tsx
|
|
97
|
+
var import_TextField = __toESM(require("@mui/material/TextField"));
|
|
98
|
+
var import_react5 = __toESM(require("react"));
|
|
99
|
+
|
|
100
|
+
// src/core/validator/ValidatorComponent.jsx
|
|
101
|
+
var import_react4 = __toESM(require("react"));
|
|
102
|
+
var import_prop_types3 = __toESM(require("prop-types"));
|
|
103
|
+
var import_promise_polyfill2 = __toESM(require("promise-polyfill"));
|
|
104
|
+
var import_react_lifecycles_compat = require("react-lifecycles-compat");
|
|
105
|
+
|
|
106
|
+
// src/core/validator/ValidatorForm.jsx
|
|
107
|
+
var import_react3 = __toESM(require("react"));
|
|
108
|
+
var import_prop_types2 = __toESM(require("prop-types"));
|
|
109
|
+
var import_promise_polyfill = __toESM(require("promise-polyfill"));
|
|
110
|
+
|
|
111
|
+
// src/core/context/index.ts
|
|
112
|
+
var import_react2 = __toESM(require("react"));
|
|
113
|
+
|
|
114
|
+
// src/core/context/implementation.js
|
|
115
|
+
var import_react = __toESM(require("react"));
|
|
116
|
+
var import_prop_types = __toESM(require("prop-types"));
|
|
117
|
+
var import_warning = __toESM(require("warning"));
|
|
118
|
+
var MAX_SIGNED_31_BIT_INT = 1073741823;
|
|
119
|
+
var commonjsGlobal = typeof globalThis !== "undefined" ? globalThis : typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : {};
|
|
120
|
+
function getUniqueId() {
|
|
121
|
+
const key = "__global_unique_id__";
|
|
122
|
+
return commonjsGlobal[key] = (commonjsGlobal[key] || 0) + 1;
|
|
123
|
+
}
|
|
124
|
+
function objectIs(x, y) {
|
|
125
|
+
if (x === y) {
|
|
126
|
+
return x !== 0 || 1 / x === 1 / y;
|
|
127
|
+
} else {
|
|
128
|
+
return x !== x && y !== y;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
function createEventEmitter(value) {
|
|
132
|
+
let handlers = [];
|
|
133
|
+
return {
|
|
134
|
+
on(handler) {
|
|
135
|
+
handlers.push(handler);
|
|
136
|
+
},
|
|
137
|
+
off(handler) {
|
|
138
|
+
handlers = handlers.filter((h) => h !== handler);
|
|
139
|
+
},
|
|
140
|
+
get() {
|
|
141
|
+
return value;
|
|
142
|
+
},
|
|
143
|
+
set(newValue, changedBits) {
|
|
144
|
+
value = newValue;
|
|
145
|
+
handlers.forEach((handler) => handler(value, changedBits));
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function onlyChild(children) {
|
|
150
|
+
return Array.isArray(children) ? children[0] : children;
|
|
151
|
+
}
|
|
152
|
+
function createReactContext(defaultValue, calculateChangedBits) {
|
|
153
|
+
const contextProp = "__create-react-context-" + getUniqueId() + "__";
|
|
154
|
+
class Provider extends import_react.Component {
|
|
155
|
+
emitter = createEventEmitter(this.props.value);
|
|
156
|
+
static childContextTypes = {
|
|
157
|
+
[contextProp]: import_prop_types.default.object.isRequired
|
|
158
|
+
};
|
|
159
|
+
getChildContext() {
|
|
160
|
+
return {
|
|
161
|
+
[contextProp]: this.emitter
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
static getDerivedStateFromProps(props, state) {
|
|
165
|
+
if (props.value !== nextProps.value) {
|
|
166
|
+
let oldValue = props.value;
|
|
167
|
+
let newValue = nextProps.value;
|
|
168
|
+
let changedBits;
|
|
169
|
+
if (objectIs(oldValue, newValue)) {
|
|
170
|
+
changedBits = 0;
|
|
171
|
+
} else {
|
|
172
|
+
changedBits = typeof calculateChangedBits === "function" ? calculateChangedBits(oldValue, newValue) : MAX_SIGNED_31_BIT_INT;
|
|
173
|
+
if (process.env.NODE_ENV !== "production") {
|
|
174
|
+
(0, import_warning.default)(
|
|
175
|
+
(changedBits & MAX_SIGNED_31_BIT_INT) === changedBits,
|
|
176
|
+
"calculateChangedBits: Expected the return value to be a 31-bit integer. Instead received: %s",
|
|
177
|
+
changedBits
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
changedBits |= 0;
|
|
181
|
+
if (changedBits !== 0) {
|
|
182
|
+
this.emitter.set(nextProps.value, changedBits);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
render() {
|
|
189
|
+
return this.props.children;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
class Consumer extends import_react.Component {
|
|
193
|
+
static contextTypes = {
|
|
194
|
+
[contextProp]: import_prop_types.default.object
|
|
195
|
+
};
|
|
196
|
+
observedBits;
|
|
197
|
+
state = {
|
|
198
|
+
value: this.getValue()
|
|
199
|
+
};
|
|
200
|
+
static getDerivedStateFromProps(props, state) {
|
|
201
|
+
let { observedBits } = nextProps;
|
|
202
|
+
this.observedBits = observedBits === void 0 || observedBits === null ? MAX_SIGNED_31_BIT_INT : observedBits;
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
componentDidMount() {
|
|
206
|
+
if (this.context[contextProp]) {
|
|
207
|
+
this.context[contextProp].on(this.onUpdate);
|
|
208
|
+
}
|
|
209
|
+
let { observedBits } = this.props;
|
|
210
|
+
this.observedBits = observedBits === void 0 || observedBits === null ? MAX_SIGNED_31_BIT_INT : observedBits;
|
|
211
|
+
}
|
|
212
|
+
componentWillUnmount() {
|
|
213
|
+
if (this.context[contextProp]) {
|
|
214
|
+
this.context[contextProp].off(this.onUpdate);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
getValue() {
|
|
218
|
+
if (this.context[contextProp]) {
|
|
219
|
+
return this.context[contextProp].get();
|
|
220
|
+
} else {
|
|
221
|
+
return defaultValue;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
onUpdate = (newValue, changedBits) => {
|
|
225
|
+
const observedBits = this.observedBits | 0;
|
|
226
|
+
if ((observedBits & changedBits) !== 0) {
|
|
227
|
+
this.setState({ value: this.getValue() });
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
render() {
|
|
231
|
+
return onlyChild(this.props.children)(this.state.value);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
Provider,
|
|
236
|
+
Consumer
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
var implementation_default = createReactContext;
|
|
240
|
+
|
|
241
|
+
// src/core/context/index.ts
|
|
242
|
+
var context_default = import_react2.default.createContext || implementation_default;
|
|
243
|
+
|
|
244
|
+
// src/core/validator/ValidatorForm.jsx
|
|
245
|
+
var import_ValidationRules = __toESM(require_ValidationRules());
|
|
246
|
+
var FormContext = context_default("form");
|
|
247
|
+
var ValidatorForm = class extends import_react3.default.Component {
|
|
248
|
+
static getValidator = (validator, value, includeRequired) => {
|
|
249
|
+
let result = true;
|
|
250
|
+
let name = validator;
|
|
251
|
+
if (name !== "required" || includeRequired) {
|
|
252
|
+
let extra;
|
|
253
|
+
const splitIdx = validator.indexOf(":");
|
|
254
|
+
if (splitIdx !== -1) {
|
|
255
|
+
name = validator.substring(0, splitIdx);
|
|
256
|
+
extra = validator.substring(splitIdx + 1);
|
|
257
|
+
}
|
|
258
|
+
result = import_ValidationRules.default[name](value, extra);
|
|
259
|
+
}
|
|
260
|
+
return result;
|
|
261
|
+
};
|
|
262
|
+
getFormHelpers = () => ({
|
|
263
|
+
form: {
|
|
264
|
+
attachToForm: this.attachToForm,
|
|
265
|
+
detachFromForm: this.detachFromForm,
|
|
266
|
+
instantValidate: this.instantValidate,
|
|
267
|
+
debounceTime: this.debounceTime
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
instantValidate = this.props.instantValidate !== void 0 ? this.props.instantValidate : true;
|
|
271
|
+
debounceTime = this.props.debounceTime;
|
|
272
|
+
childs = [];
|
|
273
|
+
errors = [];
|
|
274
|
+
attachToForm = (component) => {
|
|
275
|
+
if (this.childs.indexOf(component) === -1) {
|
|
276
|
+
this.childs.push(component);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
detachFromForm = (component) => {
|
|
280
|
+
const componentPos = this.childs.indexOf(component);
|
|
281
|
+
if (componentPos !== -1) {
|
|
282
|
+
this.childs = this.childs.slice(0, componentPos).concat(this.childs.slice(componentPos + 1));
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
submit = (event) => {
|
|
286
|
+
if (event) {
|
|
287
|
+
event.preventDefault();
|
|
288
|
+
event.persist();
|
|
289
|
+
}
|
|
290
|
+
this.errors = [];
|
|
291
|
+
this.walk(this.childs).then((result) => {
|
|
292
|
+
if (this.errors.length) {
|
|
293
|
+
this.props.onError(this.errors);
|
|
294
|
+
}
|
|
295
|
+
if (result) {
|
|
296
|
+
this.props.onSubmit(event);
|
|
297
|
+
}
|
|
298
|
+
return result;
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
walk = (children, dryRun) => {
|
|
302
|
+
const self = this;
|
|
303
|
+
return new import_promise_polyfill.default((resolve) => {
|
|
304
|
+
let result = true;
|
|
305
|
+
if (Array.isArray(children)) {
|
|
306
|
+
import_promise_polyfill.default.all(
|
|
307
|
+
children.map((input) => self.checkInput(input, dryRun))
|
|
308
|
+
).then((data) => {
|
|
309
|
+
data.forEach((item) => {
|
|
310
|
+
if (!item) {
|
|
311
|
+
result = false;
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
resolve(result);
|
|
315
|
+
});
|
|
316
|
+
} else {
|
|
317
|
+
self.walk([children], dryRun).then((result2) => resolve(result2));
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
checkInput = (input, dryRun) => new import_promise_polyfill.default((resolve) => {
|
|
322
|
+
let result = true;
|
|
323
|
+
const validators = input.props.validators;
|
|
324
|
+
if (validators) {
|
|
325
|
+
this.validate(input, true, dryRun).then((data) => {
|
|
326
|
+
if (!data) {
|
|
327
|
+
result = false;
|
|
328
|
+
}
|
|
329
|
+
resolve(result);
|
|
330
|
+
});
|
|
331
|
+
} else {
|
|
332
|
+
resolve(result);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
validate = (input, includeRequired, dryRun) => new import_promise_polyfill.default((resolve) => {
|
|
336
|
+
const { value } = input.props;
|
|
337
|
+
input.validate(value, includeRequired, dryRun).then((valid) => {
|
|
338
|
+
if (!valid) {
|
|
339
|
+
this.errors.push(input);
|
|
340
|
+
}
|
|
341
|
+
resolve(valid);
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
find = (collection, fn) => {
|
|
345
|
+
for (let i = 0, l = collection.length; i < l; i++) {
|
|
346
|
+
const item = collection[i];
|
|
347
|
+
if (fn(item)) {
|
|
348
|
+
return item;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return null;
|
|
352
|
+
};
|
|
353
|
+
resetValidations = () => {
|
|
354
|
+
this.childs.forEach((child) => {
|
|
355
|
+
child.validateDebounced.cancel();
|
|
356
|
+
child.setState({ isValid: true });
|
|
357
|
+
});
|
|
358
|
+
};
|
|
359
|
+
isFormValid = (dryRun = true) => this.walk(this.childs, dryRun);
|
|
360
|
+
render() {
|
|
361
|
+
const {
|
|
362
|
+
onSubmit,
|
|
363
|
+
instantValidate,
|
|
364
|
+
onError,
|
|
365
|
+
debounceTime,
|
|
366
|
+
children,
|
|
367
|
+
...rest
|
|
368
|
+
} = this.props;
|
|
369
|
+
return /* @__PURE__ */ import_react3.default.createElement(FormContext.Provider, { value: this.getFormHelpers() }, /* @__PURE__ */ import_react3.default.createElement("form", { ...rest, onSubmit: this.submit }, children));
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
ValidatorForm.addValidationRule = (name, callback) => {
|
|
373
|
+
import_ValidationRules.default[name] = callback;
|
|
374
|
+
};
|
|
375
|
+
ValidatorForm.getValidationRule = (name) => import_ValidationRules.default[name];
|
|
376
|
+
ValidatorForm.hasValidationRule = (name) => import_ValidationRules.default[name] && typeof import_ValidationRules.default[name] === "function";
|
|
377
|
+
ValidatorForm.removeValidationRule = (name) => {
|
|
378
|
+
delete import_ValidationRules.default[name];
|
|
379
|
+
};
|
|
380
|
+
ValidatorForm.propTypes = {
|
|
381
|
+
onSubmit: import_prop_types2.default.func.isRequired,
|
|
382
|
+
instantValidate: import_prop_types2.default.bool,
|
|
383
|
+
children: import_prop_types2.default.node,
|
|
384
|
+
onError: import_prop_types2.default.func,
|
|
385
|
+
debounceTime: import_prop_types2.default.number
|
|
386
|
+
};
|
|
387
|
+
ValidatorForm.defaultProps = {
|
|
388
|
+
onError: () => {
|
|
389
|
+
},
|
|
390
|
+
debounceTime: 0
|
|
391
|
+
};
|
|
392
|
+
var ValidatorForm_default = ValidatorForm;
|
|
393
|
+
|
|
394
|
+
// src/core/utils/utils.ts
|
|
395
|
+
var debounce = (func, wait, immediate) => {
|
|
396
|
+
let timeout;
|
|
397
|
+
function cancel() {
|
|
398
|
+
if (timeout !== void 0) {
|
|
399
|
+
clearTimeout(timeout);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const debounced = function debounced2(...args) {
|
|
403
|
+
const context = args;
|
|
404
|
+
const later = function delayed() {
|
|
405
|
+
timeout = null;
|
|
406
|
+
if (!immediate) {
|
|
407
|
+
func.apply(context, args);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
const callNow = immediate && !timeout;
|
|
411
|
+
clearTimeout(timeout);
|
|
412
|
+
timeout = setTimeout(later, wait);
|
|
413
|
+
if (callNow) {
|
|
414
|
+
func.apply(context, args);
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
debounced.cancel = cancel;
|
|
418
|
+
return debounced;
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
// src/core/validator/ValidatorComponent.jsx
|
|
422
|
+
var ValidatorComponent = class extends import_react4.default.Component {
|
|
423
|
+
static getDerivedStateFromProps(nextProps2, prevState) {
|
|
424
|
+
if (nextProps2.validators && nextProps2.errorMessages && (prevState.validators !== nextProps2.validators || prevState.errorMessages !== nextProps2.errorMessages)) {
|
|
425
|
+
return {
|
|
426
|
+
value: nextProps2.value,
|
|
427
|
+
validators: nextProps2.validators,
|
|
428
|
+
errorMessages: nextProps2.errorMessages
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
return {
|
|
432
|
+
value: nextProps2.value
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
state = {
|
|
436
|
+
isValid: true,
|
|
437
|
+
value: this.props.value,
|
|
438
|
+
errorMessages: this.props.errorMessages,
|
|
439
|
+
validators: this.props.validators
|
|
440
|
+
};
|
|
441
|
+
componentDidMount() {
|
|
442
|
+
this.configure();
|
|
443
|
+
}
|
|
444
|
+
shouldComponentUpdate(nextProps2, nextState) {
|
|
445
|
+
return this.state !== nextState || this.props !== nextProps2;
|
|
446
|
+
}
|
|
447
|
+
componentDidUpdate(prevProps, prevState) {
|
|
448
|
+
if (this.instantValidate && this.props.value !== prevState.value) {
|
|
449
|
+
this.validateDebounced(this.props.value, this.props.withRequiredValidator);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
componentWillUnmount() {
|
|
453
|
+
this.form.detachFromForm(this);
|
|
454
|
+
this.validateDebounced.cancel();
|
|
455
|
+
}
|
|
456
|
+
getErrorMessage = () => {
|
|
457
|
+
const { errorMessages } = this.state;
|
|
458
|
+
const type = typeof errorMessages;
|
|
459
|
+
if (type === "string") {
|
|
460
|
+
return errorMessages;
|
|
461
|
+
} else if (type === "object") {
|
|
462
|
+
if (this.invalid.length > 0) {
|
|
463
|
+
return errorMessages[this.invalid[0]];
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
console.log("unknown errorMessages type", errorMessages);
|
|
467
|
+
return true;
|
|
468
|
+
};
|
|
469
|
+
instantValidate = true;
|
|
470
|
+
invalid = [];
|
|
471
|
+
configure = () => {
|
|
472
|
+
this.form.attachToForm(this);
|
|
473
|
+
this.instantValidate = this.form.instantValidate;
|
|
474
|
+
this.debounceTime = this.form.debounceTime;
|
|
475
|
+
this.validateDebounced = debounce(this.validate, this.debounceTime);
|
|
476
|
+
};
|
|
477
|
+
validate = (value, includeRequired = false, dryRun = false) => {
|
|
478
|
+
const validations = import_promise_polyfill2.default.all(
|
|
479
|
+
this.state.validators.map((validator) => ValidatorForm_default.getValidator(validator, value, includeRequired))
|
|
480
|
+
);
|
|
481
|
+
return validations.then((results) => {
|
|
482
|
+
this.invalid = [];
|
|
483
|
+
let valid = true;
|
|
484
|
+
results.forEach((result, key) => {
|
|
485
|
+
if (!result) {
|
|
486
|
+
valid = false;
|
|
487
|
+
this.invalid.push(key);
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
if (!dryRun) {
|
|
491
|
+
this.setState({ isValid: valid }, () => {
|
|
492
|
+
this.props.validatorListener(this.state.isValid);
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
return valid;
|
|
496
|
+
});
|
|
497
|
+
};
|
|
498
|
+
isValid = () => this.state.isValid;
|
|
499
|
+
makeInvalid = () => {
|
|
500
|
+
this.setState({ isValid: false });
|
|
501
|
+
};
|
|
502
|
+
makeValid = () => {
|
|
503
|
+
this.setState({ isValid: true });
|
|
504
|
+
};
|
|
505
|
+
renderComponent = (form) => {
|
|
506
|
+
if (!this.form) {
|
|
507
|
+
this.form = form;
|
|
508
|
+
}
|
|
509
|
+
return this.renderValidatorComponent();
|
|
510
|
+
};
|
|
511
|
+
render() {
|
|
512
|
+
return /* @__PURE__ */ import_react4.default.createElement(FormContext.Consumer, null, ({ form }) => /* @__PURE__ */ import_react4.default.createElement("div", { ...this.props.containerProps }, this.renderComponent(form)));
|
|
513
|
+
}
|
|
514
|
+
};
|
|
515
|
+
ValidatorComponent.propTypes = {
|
|
516
|
+
errorMessages: import_prop_types3.default.oneOfType([
|
|
517
|
+
import_prop_types3.default.array,
|
|
518
|
+
import_prop_types3.default.string
|
|
519
|
+
]),
|
|
520
|
+
validators: import_prop_types3.default.array,
|
|
521
|
+
value: import_prop_types3.default.any,
|
|
522
|
+
validatorListener: import_prop_types3.default.func,
|
|
523
|
+
withRequiredValidator: import_prop_types3.default.bool,
|
|
524
|
+
containerProps: import_prop_types3.default.object
|
|
525
|
+
};
|
|
526
|
+
ValidatorComponent.defaultProps = {
|
|
527
|
+
errorMessages: "error",
|
|
528
|
+
validators: [],
|
|
529
|
+
validatorListener: () => {
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
(0, import_react_lifecycles_compat.polyfill)(ValidatorComponent);
|
|
533
|
+
var ValidatorComponent_default = ValidatorComponent;
|
|
534
|
+
|
|
535
|
+
// src/components/MuiTextField.tsx
|
|
536
|
+
var TextValidator = class extends ValidatorComponent_default {
|
|
537
|
+
renderValidatorComponent() {
|
|
538
|
+
const {
|
|
539
|
+
error,
|
|
540
|
+
errorMessages,
|
|
541
|
+
validators,
|
|
542
|
+
requiredError,
|
|
543
|
+
helperText,
|
|
544
|
+
validatorListener,
|
|
545
|
+
withRequiredValidator,
|
|
546
|
+
containerProps,
|
|
547
|
+
...rest
|
|
548
|
+
} = this.props;
|
|
549
|
+
const { isValid } = this.state;
|
|
550
|
+
return /* @__PURE__ */ import_react5.default.createElement(
|
|
551
|
+
import_TextField.default,
|
|
552
|
+
{
|
|
553
|
+
...rest,
|
|
554
|
+
error: !isValid || error,
|
|
555
|
+
helperText: !isValid && this.getErrorMessage() || helperText
|
|
556
|
+
}
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
// src/components/MuiSelect.tsx
|
|
562
|
+
var import_material = require("@mui/material");
|
|
563
|
+
var import_react6 = __toESM(require("react"));
|
|
564
|
+
var MuiSelect = class extends ValidatorComponent_default {
|
|
565
|
+
renderValidatorComponent() {
|
|
566
|
+
const {
|
|
567
|
+
error,
|
|
568
|
+
errorMessages,
|
|
569
|
+
validators,
|
|
570
|
+
requiredError,
|
|
571
|
+
helperText,
|
|
572
|
+
validatorListener,
|
|
573
|
+
withRequiredValidator,
|
|
574
|
+
containerProps,
|
|
575
|
+
...rest
|
|
576
|
+
} = this.props;
|
|
577
|
+
const { isValid } = this.state;
|
|
578
|
+
return /* @__PURE__ */ import_react6.default.createElement(
|
|
579
|
+
import_material.TextField,
|
|
580
|
+
{
|
|
581
|
+
...rest,
|
|
582
|
+
select: true,
|
|
583
|
+
error: !isValid || error,
|
|
584
|
+
helperText: !isValid && this.getErrorMessage() || helperText
|
|
585
|
+
}
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
// src/index.ts
|
|
591
|
+
var MuiTextField = TextValidator;
|
|
592
|
+
var MuiSelect2 = MuiSelect;
|
|
593
|
+
var MuiComponent = ValidatorComponent_default;
|
|
594
|
+
var MuiForm = ValidatorForm_default;
|
|
595
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
596
|
+
0 && (module.exports = {
|
|
597
|
+
MuiComponent,
|
|
598
|
+
MuiForm,
|
|
599
|
+
MuiSelect,
|
|
600
|
+
MuiTextField
|
|
601
|
+
});
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-mui-form-validator",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Validator for forms designed with material-ui components.",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"build": "
|
|
8
|
-
"prepublish": "npm run build",
|
|
9
|
-
"lint": "eslint src/**"
|
|
8
|
+
"build": "tsup src/index.ts --dts"
|
|
10
9
|
},
|
|
11
10
|
"repository": {
|
|
12
11
|
"type": "git",
|
|
@@ -14,7 +13,6 @@
|
|
|
14
13
|
},
|
|
15
14
|
"keywords": [
|
|
16
15
|
"react",
|
|
17
|
-
"material-ui",
|
|
18
16
|
"mui",
|
|
19
17
|
"form",
|
|
20
18
|
"form-validator",
|
|
@@ -28,29 +26,38 @@
|
|
|
28
26
|
"homepage": "https://github.com/Blencm/react-mui-form-validator#readme",
|
|
29
27
|
"dependencies": {
|
|
30
28
|
"@mui/material": "^5.13.6",
|
|
31
|
-
"
|
|
29
|
+
"@types/prop-types": "^15.7.5",
|
|
30
|
+
"gud": "^1.0.0",
|
|
32
31
|
"promise-polyfill": "^8.3.0",
|
|
33
32
|
"prop-types": "^15.8.1",
|
|
33
|
+
"react": "^18.2.0",
|
|
34
|
+
"react-dom": "^18.2.0",
|
|
35
|
+
"react-hook-form": "^7.45.1",
|
|
34
36
|
"react-lifecycles-compat": "^3.0.4",
|
|
35
37
|
"tiny-warning": "^1.0.3",
|
|
36
|
-
"
|
|
38
|
+
"tsup": "^7.1.0",
|
|
39
|
+
"typescript": "^5.1.6",
|
|
40
|
+
"warning": "^4.0.3"
|
|
37
41
|
},
|
|
38
42
|
"peerDependencies": {
|
|
39
|
-
"react": "^
|
|
43
|
+
"react": "^17.0.0 || ^18.2.0 || ^19.0.0 || ^20.0.0",
|
|
44
|
+
"react-dom": "^17.0.0 || ^18.2.0 || ^19.0.0 || ^20.0.0"
|
|
40
45
|
},
|
|
41
46
|
"devDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"@
|
|
44
|
-
"@babel/plugin-syntax-jsx": "^7.22.5",
|
|
45
|
-
"@babel/preset-env": "^7.22.5",
|
|
46
|
-
"@babel/preset-react": "^7.22.5",
|
|
47
|
+
"@types/node": "^20.3.3",
|
|
48
|
+
"@types/promise-polyfill": "^6.0.4",
|
|
47
49
|
"@types/react": "18.2.14",
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
+
"@types/react-dom": "^18.2.6",
|
|
51
|
+
"@types/react-lifecycles-compat": "^3.0.1",
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "5.60.1",
|
|
53
|
+
"@typescript-eslint/parser": "5.60.1",
|
|
50
54
|
"eslint": "^8.44.0",
|
|
51
55
|
"eslint-config-airbnb": "^19.0.4",
|
|
56
|
+
"eslint-config-airbnb-typescript": "17.0.0",
|
|
57
|
+
"eslint-config-prettier": "8.8.0",
|
|
52
58
|
"eslint-plugin-import": "^2.27.5",
|
|
53
59
|
"eslint-plugin-jsx-a11y": "^6.7.1",
|
|
60
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
54
61
|
"eslint-plugin-react": "^7.32.2",
|
|
55
62
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
56
63
|
"rimraf": "^5.0.1"
|