Skip to content

Commit c33e3f7

Browse files
author
Edward Xiao
committed
- Fix async problen with customFunc in <Textbox/> and <Textarea/>
1 parent 6de865c commit c33e3f7

File tree

15 files changed

+717
-282
lines changed

15 files changed

+717
-282
lines changed

example/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,12 @@ class Index extends Component {
883883
// locale: 'en-US', // Optional.[String].Default: "en-US". For error message display. Current options are ['zh-CN', 'en-US']; Default is 'en-US'.
884884
// msgOnError: "Your custom error message if you provide the validationOption['msgOnError']", // Optional.[String].Default: "" Show your custom error message no matter what(except the message from customFunc) when it has error if it is provied.
885885
// msgOnSuccess: "Your custom success message if you provide the validationOption['msgOnSuccess']. Otherwise, it will not show, not even green border." // Optional.[String].Default: "". Show your custom success message no matter what when it has error if it is provied.
886-
// customFunc: res => { // Optional.[Func].Default: none. Custom function. Returns true or err message
887-
// if (res != 'milk') {
888-
// return 'Name cannot be other things but milk';
889-
// }
890-
// return true;
891-
// }
886+
customFunc: res => { // Optional.[Func].Default: none. Custom function. Returns true or err message
887+
if (res != 'milk') {
888+
return 'Name cannot be other things but milk';
889+
}
890+
return true;
891+
}
892892
}}
893893
// asyncMsgObj={{
894894
// error: false, // Optional.[Bool].Default: false. (Server response) Backend validation result.
@@ -1169,12 +1169,12 @@ class Index extends Component {
11691169
// regMsg: 'failed in reg.test(${value})', // Optional.[String].Default: "". Custom regex error message.
11701170
// msgOnError: "Your custom error message if you provide the validationOption['msgOnError']", // Optional.[String].Default: "". Show your custom error message no matter what(except the message from customFunc) when it has error if it is provied.
11711171
// msgOnSuccess: "Your custom success message if you provide the validationOption['msgOnSuccess']. Otherwise, it will not show, not even green border." // Optional.[String].Default: "". Show your custom success message no matter what when it has error if it is provied.
1172-
// customFunc: res => { // Optional.[Func].Default: none. Custom function. Returns true or err message
1173-
// if (res != 'banana') {
1174-
// return 'Description cannot be other things but banana';
1175-
// }
1176-
// return true;
1177-
// }
1172+
customFunc: res => { // Optional.[Func].Default: none. Custom function. Returns true or err message
1173+
if (res != 'banana') {
1174+
return 'Description cannot be other things but banana';
1175+
}
1176+
return true;
1177+
}
11781178
}}
11791179
// asyncMsgObj={{
11801180
// error: false, // Optional.[Bool].Default: false. (Server response) Backend validation result.

lib/components/Textarea.js

Lines changed: 177 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "d
1919

2020
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; return newObj; } }
2121

22+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
23+
24+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
25+
2226
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
2327

2428
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
@@ -262,103 +266,194 @@ var component = function component(_ref) {
262266
setSuccessMsg('');
263267
}
264268
}, [err]);
265-
var check = useCallback(function () {
266-
var reg = option.reg,
267-
min = option.min,
268-
max = option.max,
269-
type = option.type,
270-
name = option.name,
271-
check = option.check,
272-
length = option.length,
273-
regMsg = option.regMsg,
274-
locale = option.locale,
275-
required = option.required,
276-
msgOnSuccess = option.msgOnSuccess,
277-
customFunc = option.customFunc;
278-
279-
if (!check) {
280-
return;
281-
}
269+
var check = useCallback(
270+
/*#__PURE__*/
271+
_asyncToGenerator(
272+
/*#__PURE__*/
273+
regeneratorRuntime.mark(function _callee() {
274+
var reg, min, max, type, name, check, length, regMsg, locale, required, msgOnSuccess, customFunc, _msg, nameText, customFuncResult;
275+
276+
return regeneratorRuntime.wrap(function _callee$(_context) {
277+
while (1) {
278+
switch (_context.prev = _context.next) {
279+
case 0:
280+
reg = option.reg, min = option.min, max = option.max, type = option.type, name = option.name, check = option.check, length = option.length, regMsg = option.regMsg, locale = option.locale, required = option.required, msgOnSuccess = option.msgOnSuccess, customFunc = option.customFunc;
281+
282+
if (check) {
283+
_context.next = 3;
284+
break;
285+
}
282286

283-
if (type) {
284-
if (VALIDATE_OPTION_TYPE_LIST.indexOf(type) !== -1) {
285-
if (!_message["default"][locale] || !_message["default"][locale][TYPE]) {
286-
console.error(_const.REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE);
287-
return;
288-
}
287+
return _context.abrupt("return");
288+
289+
case 3:
290+
if (!type) {
291+
_context.next = 52;
292+
break;
293+
}
294+
295+
if (!(VALIDATE_OPTION_TYPE_LIST.indexOf(type) !== -1)) {
296+
_context.next = 49;
297+
break;
298+
}
299+
300+
if (!(!_message["default"][locale] || !_message["default"][locale][TYPE])) {
301+
_context.next = 8;
302+
break;
303+
}
304+
305+
console.error(_const.REACT_INPUTS_VALIDATION_CUSTOM_ERROR_MESSAGE_EXAMPLE);
306+
return _context.abrupt("return");
307+
308+
case 8:
309+
_msg = _message["default"][locale][TYPE];
310+
nameText = name ? name : '';
311+
312+
if (!required) {
313+
_context.next = 14;
314+
break;
315+
}
289316

290-
var _msg = _message["default"][locale][TYPE];
291-
var nameText = name ? name : '';
317+
if (!_validator["default"].empty(internalValue)) {
318+
_context.next = 14;
319+
break;
320+
}
292321

293-
if (required) {
294-
if (_validator["default"].empty(internalValue)) {
295322
handleCheckEnd(true, _msg.empty(nameText));
296-
return;
297-
}
298-
}
323+
return _context.abrupt("return");
324+
325+
case 14:
326+
if (!(String(internalValue) !== '')) {
327+
_context.next = 38;
328+
break;
329+
}
299330

300-
if (String(internalValue) !== '') {
301-
if (reg) {
302-
if (_validator["default"]['reg'](reg, internalValue)) {
303-
handleCheckEnd(true, regMsg !== '' ? regMsg : _msg.invalid(nameText));
304-
return;
331+
if (!reg) {
332+
_context.next = 19;
333+
break;
305334
}
306-
}
307-
308-
if (min || max) {
309-
if (min && max) {
310-
if (String(internalValue).length < min || String(internalValue).length > max) {
311-
handleCheckEnd(true, _msg.inBetween(nameText)(min)(max));
312-
return;
313-
}
314-
} else {
315-
if (min) {
316-
if (String(internalValue).length < min) {
317-
handleCheckEnd(true, _msg.lessThan(nameText)(min));
318-
return;
319-
}
320-
}
321-
322-
if (max) {
323-
if (String(internalValue).length > max) {
324-
handleCheckEnd(true, _msg.greaterThan(nameText)(max));
325-
return;
326-
}
327-
}
335+
336+
if (!_validator["default"]['reg'](reg, internalValue)) {
337+
_context.next = 19;
338+
break;
328339
}
329-
}
330340

331-
if (length) {
332-
if (String(internalValue).length !== length) {
333-
handleCheckEnd(true, _msg.lengthEqual(nameText)(length));
334-
return;
341+
handleCheckEnd(true, regMsg !== '' ? regMsg : _msg.invalid(nameText));
342+
return _context.abrupt("return");
343+
344+
case 19:
345+
if (!(min || max)) {
346+
_context.next = 34;
347+
break;
335348
}
336-
}
337-
}
338349

339-
if (customFunc && typeof customFunc === 'function') {
340-
var customFuncResult = customFunc(internalValue);
350+
if (!(min && max)) {
351+
_context.next = 26;
352+
break;
353+
}
354+
355+
if (!(String(internalValue).length < min || String(internalValue).length > max)) {
356+
_context.next = 24;
357+
break;
358+
}
359+
360+
handleCheckEnd(true, _msg.inBetween(nameText)(min)(max));
361+
return _context.abrupt("return");
362+
363+
case 24:
364+
_context.next = 34;
365+
break;
366+
367+
case 26:
368+
if (!min) {
369+
_context.next = 30;
370+
break;
371+
}
372+
373+
if (!(String(internalValue).length < min)) {
374+
_context.next = 30;
375+
break;
376+
}
377+
378+
handleCheckEnd(true, _msg.lessThan(nameText)(min));
379+
return _context.abrupt("return");
380+
381+
case 30:
382+
if (!max) {
383+
_context.next = 34;
384+
break;
385+
}
386+
387+
if (!(String(internalValue).length > max)) {
388+
_context.next = 34;
389+
break;
390+
}
391+
392+
handleCheckEnd(true, _msg.greaterThan(nameText)(max));
393+
return _context.abrupt("return");
394+
395+
case 34:
396+
if (!length) {
397+
_context.next = 38;
398+
break;
399+
}
400+
401+
if (!(String(internalValue).length !== length)) {
402+
_context.next = 38;
403+
break;
404+
}
405+
406+
handleCheckEnd(true, _msg.lengthEqual(nameText)(length));
407+
return _context.abrupt("return");
408+
409+
case 38:
410+
if (!(customFunc && typeof customFunc === 'function')) {
411+
_context.next = 45;
412+
break;
413+
}
414+
415+
_context.next = 41;
416+
return customFunc(internalValue);
417+
418+
case 41:
419+
customFuncResult = _context.sent;
420+
421+
if (!(customFuncResult !== true)) {
422+
_context.next = 45;
423+
break;
424+
}
341425

342-
if (customFuncResult !== true) {
343426
handleCheckEnd(true, customFuncResult, true);
344-
return;
345-
}
346-
}
427+
return _context.abrupt("return");
347428

348-
if (msgOnSuccess) {
349-
setSuccessMsg(msgOnSuccess);
350-
}
429+
case 45:
430+
if (msgOnSuccess) {
431+
setSuccessMsg(msgOnSuccess);
432+
}
433+
434+
handleCheckEnd(false, msgOnSuccess);
435+
_context.next = 50;
436+
break;
351437

352-
handleCheckEnd(false, msgOnSuccess);
353-
} else {
354-
console.error("The valid ".concat(_utils["default"].toCamelCase(TYPE)(true), " \"type\" options in validationOption are [").concat(VALIDATE_OPTION_TYPE_LIST.map(function (i) {
355-
return i;
356-
}), "]"));
438+
case 49:
439+
console.error("The valid ".concat(_utils["default"].toCamelCase(TYPE)(true), " \"type\" options in validationOption are [").concat(VALIDATE_OPTION_TYPE_LIST.map(function (i) {
440+
return i;
441+
}), "]"));
442+
443+
case 50:
444+
_context.next = 53;
445+
break;
446+
447+
case 52:
448+
console.error('Please provide "type" in validationOption');
449+
450+
case 53:
451+
case "end":
452+
return _context.stop();
453+
}
357454
}
358-
} else {
359-
console.error('Please provide "type" in validationOption');
360-
}
361-
}, [internalValue]);
455+
}, _callee);
456+
})), [internalValue]);
362457
var handleCheckEnd = useCallback(function (err, message) {
363458
var fromCustomFunc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
364459
var msg = message;

0 commit comments

Comments
 (0)