react-validation-component
Advanced tools
| import { Component } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import ReactPropTypesSecret from 'prop-types/lib/ReactPropTypesSecret'; | ||
| const DEFAULT_NAME = 'React Validate Component'; | ||
| export default class ReactValidation extends Component{ | ||
| componentDidMount(){ | ||
| return this.validate.call(this); | ||
| } | ||
| shouldComponentUpdate({ children, propTypes }){ | ||
| return this.props.children !== children || this.props.propTypes !== propTypes; | ||
| } | ||
| componentDidUpdate(){ | ||
| return this.validate.call(this); | ||
| } | ||
| validate(){ | ||
| const { children, propTypes, onValidation, title } = this.props; | ||
| const errors = []; | ||
| for(let prop in propTypes){ | ||
| const propHandler = propTypes[prop]; | ||
| const propHandlers = typeof propHandler === 'function' ? [ propHandler ] : propHandler; | ||
| if (!Array.isArray(propHandlers)){ | ||
| throw new TypeError("propHandlers should be an array"); | ||
| } | ||
| for(let i in propHandlers){ | ||
| const e = propHandlers[i](children, prop, title || DEFAULT_NAME, '', null, ReactPropTypesSecret); | ||
| if(e){ | ||
| errors.push({ error: e, property: prop }); | ||
| } | ||
| } | ||
| } | ||
| return onValidation(errors.length === 0 ? null : errors); | ||
| } | ||
| render(){ | ||
| return null; | ||
| } | ||
| } | ||
| ReactValidation.propTypes = { | ||
| children: PropTypes.object.isRequired, | ||
| propTypes: PropTypes.object.isRequired, | ||
| onValidation: PropTypes.func.isRequired, | ||
| title: PropTypes.string | ||
| }; |
+2
-2
| { | ||
| "name": "react-validation-component", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "dependencies": { | ||
@@ -18,3 +18,3 @@ "prop-types": "^15.6.2", | ||
| }, | ||
| "main": "./lib/validation.jsx" | ||
| "main": "./src/lib/validation.jsx" | ||
| } |
| import { Component } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import ReactPropTypesSecret from 'prop-types/lib/ReactPropTypesSecret'; | ||
| const DEFAULT_NAME = 'React Validate Component'; | ||
| export default class ReactValidation extends Component{ | ||
| componentDidMount(){ | ||
| return this.validate.call(this); | ||
| } | ||
| shouldComponentUpdate({ children, propTypes }){ | ||
| return this.props.children !== children || this.props.propTypes !== propTypes; | ||
| } | ||
| componentDidUpdate(){ | ||
| return this.validate.call(this); | ||
| } | ||
| validate(){ | ||
| const { children, propTypes, onValidation, title } = this.props; | ||
| const errors = []; | ||
| for(let prop in propTypes){ | ||
| const e = propTypes[prop](children, prop, title || DEFAULT_NAME, '', null, ReactPropTypesSecret); | ||
| if(e){ | ||
| errors.push({ error: e, property: prop }); | ||
| } | ||
| } | ||
| return onValidation(errors.length === 0 ? null : errors); | ||
| } | ||
| render(){ | ||
| return null; | ||
| } | ||
| } | ||
| ReactValidation.propTypes = { | ||
| children: PropTypes.object.isRequired, | ||
| propTypes: PropTypes.object.isRequired, | ||
| onValidation: PropTypes.func.isRequired, | ||
| title: PropTypes.string | ||
| }; |
3296
12.53%42
20%