valedictorian
Version:
Another React validation library
134 lines (105 loc) • 4.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _propTypes = require("prop-types");
var _propTypes2 = _interopRequireDefault(_propTypes);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(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; }
function _inherits(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 Form = function (_React$Component) {
_inherits(Form, _React$Component);
function Form(props) {
_classCallCheck(this, Form);
var _this = _possibleConstructorReturn(this, (Form.__proto__ || Object.getPrototypeOf(Form)).call(this, props));
_this.components = [];
_this.listeners = {};
_this.nameMap = [];
_this.state = {
valid: false
};
return _this;
}
_createClass(Form, [{
key: "componentDidMount",
value: function componentDidMount() {
this.getChildContext().validation.validate();
}
}, {
key: "getChildContext",
value: function getChildContext() {
var _this2 = this;
return {
validation: {
register: function register(component) {
if (_this2.components.indexOf(component) === -1) {
_this2.components.push(component);
_this2.nameMap.push(component.props.name);
}
},
unregister: function unregister(component) {
var index = _this2.components.indexOf(component);
_this2.components = _this2.components.filter(function (c, i) {
return index !== i;
});
_this2.nameMap = _this2.nameMap.filter(function (c, i) {
return index !== i;
});
},
addListener: function addListener(name, listener) {
if (typeof _this2.listeners[name] === "undefined") {
_this2.listeners[name] = [];
}
if (_this2.listeners[name].indexOf(listener) == -1) {
_this2.listeners[name].push(listener);
}
},
removeListener: function removeListener(name, listener) {
_this2.listeners[name] = _this2.listeners[name].filter(function (l) {
return l !== listener;
});
},
validate: function validate() {
var valid = _this2.components.map(function (component) {
return component.validate();
});
_this2.setState({
valid: valid.reduce(function (acc, validator) {
return acc && validator.valid;
}, true)
});
// Notify all the listeners that validation has occurred.
_this2.nameMap.forEach(function (name, index) {
if (typeof name !== "undefined" && typeof _this2.listeners[name] !== "undefined") {
_this2.listeners[name].forEach(function (listener) {
listener(valid[index]);
});
}
});
},
valid: function valid() {
return _this2.state.valid;
}
}
};
}
}, {
key: "render",
value: function render() {
return _react2.default.createElement(
"form",
this.props,
this.props.children
);
}
}]);
return Form;
}(_react2.default.Component);
exports.default = Form;
Form.childContextTypes = {
validation: _propTypes2.default.object
};