UNPKG

@dock365/refield

Version:
204 lines 10.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = __importStar(require("react")); var validator_1 = __importStar(require("@dock365/validator")); var ValidateOnTypes; (function (ValidateOnTypes) { ValidateOnTypes[ValidateOnTypes["OnChange"] = 0] = "OnChange"; ValidateOnTypes[ValidateOnTypes["OnBlur"] = 1] = "OnBlur"; })(ValidateOnTypes = exports.ValidateOnTypes || (exports.ValidateOnTypes = {})); var Field = /** @class */ (function (_super) { __extends(Field, _super); function Field(props) { var _this = _super.call(this, props) || this; _this._resetField = function () { _this.setState({ value: undefined }); }; _this.state = { value: _this.props.defaultValue, errors: [], customErrors: [], }; _this.validator = new validator_1.default({ failMessages: _this.props.validationMessages, }); return _this; } Field.prototype.componentDidUpdate = function (prevProps) { if (prevProps.defaultValue === undefined && this.props.defaultValue !== prevProps.defaultValue || this.props.defaultValueIsUpdatable && this.props.defaultValue !== prevProps.defaultValue) { this.setState({ value: this.props.defaultValue }); } }; Field.prototype.render = function () { var _this = this; var _a = this.props, validationRules = _a.validationRules, validate = _a.validate, showAsteriskOnRequired = _a.showAsteriskOnRequired; var errors = this.state.errors.concat(this.state.customErrors); return (React.createElement(this.props.render, { name: this.props.name, placeholder: this.props.placeholder, defaultValue: this.props.defaultValue, defaultValueIsUpdatable: this.props.defaultValueIsUpdatable, value: this.state.value, customProps: this.props.customProps, className: this.props.className, readOnly: this.props.readOnly, onClick: this.props.onClick, onChange: function (value, e) { var _value = validationRules && validationRules.type === validator_1.validationTypes.String && typeof value === "number" ? "" + value : value; var onChange = _this.props.onChange && _this.props.onChange(_value, _this.props.name, _this._resetField); if (onChange || onChange === undefined) _this.setState(function () { return ({ value: _value }); }); return onChange; }, onBlur: function (value, e) { return __awaiter(_this, void 0, void 0, function () { var _value, customErrors, defaultErrors; return __generator(this, function (_a) { switch (_a.label) { case 0: _value = validationRules && validationRules.type === validator_1.validationTypes.String && typeof value === "number" ? "" + value : value; customErrors = []; defaultErrors = []; if (!validate) return [3 /*break*/, 3]; return [4 /*yield*/, this._validateField(_value)]; case 1: defaultErrors = _a.sent(); if (!this.props.customValidation) return [3 /*break*/, 3]; return [4 /*yield*/, this._updateCustomValidationMessage(this.props.customValidation(_value, this.props.validationRules))]; case 2: customErrors = _a.sent(); _a.label = 3; case 3: return [2 /*return*/, this.props.onBlur && this.props.onBlur(_value, this.props.name, defaultErrors.concat(customErrors), this._resetField)]; } }); }); }, label: !this.props.hideLabel && (showAsteriskOnRequired && this.props.validationRules && this.props.validationRules.required ? this.props.label + "*" : this.props.label) || undefined, validationRules: this.props.validationRules, errors: this.state.errors.concat(this.state.customErrors), })); }; Field.prototype._validateField = function (value) { var _this = this; return new Promise(function (resolve, reject) { var _a = _this.props, validationRules = _a.validationRules, label = _a.label, name = _a.name; if (validationRules && validationRules.type) { var result_1; switch (validationRules.type) { case validator_1.validationTypes.String: result_1 = _this.validator[validator_1.validationTypes.String](label || name || "", value || "", validationRules); break; case validator_1.validationTypes.Number: result_1 = _this.validator[validator_1.validationTypes.Number](label || name || "", value, validationRules); break; case validator_1.validationTypes.Date: result_1 = _this.validator[validator_1.validationTypes.Date](label || name || "", value, validationRules); break; case validator_1.validationTypes.Email: result_1 = _this.validator[validator_1.validationTypes.Email](label || name || "", value || "", validationRules); break; case validator_1.validationTypes.Array: result_1 = _this.validator[validator_1.validationTypes.Array](label || name || "", value || [], validationRules); break; case validator_1.validationTypes.RegEx: result_1 = _this.validator[validator_1.validationTypes.RegEx](label || name || "", value || "", validationRules); break; default: result_1 = { success: true, messages: [], }; break; } if (!result_1.success) { _this.setState({ errors: result_1.messages }, function () { return resolve(result_1.messages); }); } else { _this.setState({ errors: [] }, function () { return resolve([]); }); } } else { resolve([]); } }); }; Field.prototype._updateCustomValidationMessage = function (errors) { var _this = this; return new Promise(function (resolve, reject) { _this.setState({ customErrors: errors }, function () { return resolve(errors); }); }); }; return Field; }(React.Component)); exports.default = Field; //# sourceMappingURL=index.js.map