UNPKG

react-verificator

Version:
200 lines (163 loc) 7.26 kB
/** * react-verificator v1.0.0-beta.2 * (c) 2018 Nikita Stenin * @license MIT */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('verificator/es')) : typeof define === 'function' && define.amd ? define(['exports', 'react', 'verificator/es'], factory) : (factory((global.ReactVerificator = {}),global.React,global.Verificator)); }(this, (function (exports,React,es) { 'use strict'; var React__default = 'default' in React ? React['default'] : React; var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var hoistNonReactStatics = createCommonjsModule(function (module, exports) { /** * Copyright 2015, Yahoo! Inc. * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ (function (global, factory) { module.exports = factory(); })(commonjsGlobal, function () { var REACT_STATICS = { childContextTypes: true, contextTypes: true, defaultProps: true, displayName: true, getDefaultProps: true, getDerivedStateFromProps: true, mixins: true, propTypes: true, type: true }; var KNOWN_STATICS = { name: true, length: true, prototype: true, caller: true, callee: true, arguments: true, arity: true }; var defineProperty = Object.defineProperty; var getOwnPropertyNames = Object.getOwnPropertyNames; var getOwnPropertySymbols = Object.getOwnPropertySymbols; var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; var getPrototypeOf = Object.getPrototypeOf; var objectPrototype = getPrototypeOf && getPrototypeOf(Object); return function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) { if (typeof sourceComponent !== 'string') { // don't hoist over string (html) components if (objectPrototype) { var inheritedComponent = getPrototypeOf(sourceComponent); if (inheritedComponent && inheritedComponent !== objectPrototype) { hoistNonReactStatics(targetComponent, inheritedComponent, blacklist); } } var keys = getOwnPropertyNames(sourceComponent); if (getOwnPropertySymbols) { keys = keys.concat(getOwnPropertySymbols(sourceComponent)); } for (var i = 0; i < keys.length; ++i) { var key = keys[i]; if (!REACT_STATICS[key] && !KNOWN_STATICS[key] && (!blacklist || !blacklist[key])) { var descriptor = getOwnPropertyDescriptor(sourceComponent, key); try { // Avoid failures from read-only properties defineProperty(targetComponent, key, descriptor); } catch (e) {} } } return targetComponent; } return targetComponent; }; }); }); var classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; var inherits = function (subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }; var possibleConstructorReturn = function (self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }; var defaultMapToProps = function defaultMapToProps(_ref) { var validator = _ref.validator; return { validator: validator }; }; function validator(options) { var mapToProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultMapToProps; var validator = new es.Validator({}, options.rules, options.customLocale); if (typeof mapToProps !== 'function') { mapToProps = defaultMapToProps; } function wrapWithValidatorComponent(WrappedComponent) { var displayName = 'Validator(' + (WrappedComponent.displayName || WrappedComponent.name || 'Component') + ')'; var Validator = function (_Component) { inherits(Validator, _Component); function Validator(props) { classCallCheck(this, Validator); var _this = possibleConstructorReturn(this, _Component.call(this, props)); _this.state = { validator: validator }; return _this; } Validator.prototype.componentWillMount = function componentWillMount() { var _this2 = this; this.unsubscribe = this.state.validator.subscribe(function () { _this2.setState({ validator: _this2.state.validator }); }); }; Validator.prototype.componentWillUnmount = function componentWillUnmount() { this.unsubscribe(); }; Validator.prototype.calculateMapToProps = function calculateMapToProps() { var result = { validator: validator, ownProps: this.props }; return mapToProps(result); }; Validator.prototype.render = function render() { var props = this.props; var validatorProps = this.calculateMapToProps(); var mergedPropsAndValidator = Object.assign({}, props, validatorProps); return React__default.createElement(WrappedComponent, Object.assign({}, mergedPropsAndValidator)); }; return Validator; }(React.Component); Validator.displayName = displayName; Validator.WrappedComponent = WrappedComponent; return hoistNonReactStatics(Validator, WrappedComponent); } return wrapWithValidatorComponent; } exports.Validator = es.Validator; exports.validator = validator; Object.defineProperty(exports, '__esModule', { value: true }); })));