├── .babelrc ├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── package.json ├── src └── withValidations.js └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015", 4 | "stage-0", 5 | "react" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | docs 3 | dist 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | 2 | docs 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | # The MIT License (MIT) 3 | Copyright (c) 2017 Brent Jackson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # React Validations HOC 3 | 4 | React higher order component for form validations 5 | 6 | ## Usage 7 | 8 | ```jsx 9 | import React from 'react' 10 | import withValidations from 'react-hoc-validations' 11 | 12 | const SignupForm = ({ 13 | username, 14 | password, 15 | zip, 16 | onChange, 17 | onSubmit, 18 | errors, 19 | onBlur 20 | }) => { 21 | return ( 22 |
23 |
24 | 25 | 33 | {errors.username && ( 34 | {errors.username} 35 | )} 36 |
37 |
38 | 39 | 47 | {errors.password && ( 48 | {errors.password} 49 | )} 50 |
51 |
52 | 53 | 61 | {errors.zip && ( 62 | {errors.zip} 63 | )} 64 |
65 |
66 | ) 67 | } 68 | 69 | const validations = { 70 | username: (value) => { 71 | if (value.length < 6) { 72 | return 'Must be at least 6 characters' 73 | } 74 | return null 75 | }, 76 | password: (value, props) => { 77 | if (value === props.password) { 78 | return 'Password must be different from username' 79 | } 80 | if (value.length < 8) { 81 | return 'Must be at least 8 characters' 82 | } 83 | return null 84 | }, 85 | zip: (value) => { 86 | if (value.length !== 5) { 87 | return 'ZIP code must be 5 digits' 88 | } 89 | return null 90 | } 91 | } 92 | 93 | export default withValidations(validations)(SignupForm) 94 | ``` 95 | 96 | ## API 97 | 98 | ### `withValidations(validations)(Component)` 99 | 100 | Returns a higher order component for form components, 101 | which provides the following props: 102 | 103 | - `onBlur` - an onBlur handler to be passed onto form inputs 104 | - `touched` - object of keys for form fields that have values and have been blurred. This requires the `onBlur` handler be passed to each input. 105 | - `errors` - object of validation errors 106 | 107 | The `validations` argument is an object of validation functions. 108 | Each validation function should return null for valid values and a string for invalid values. The first argument is the form input value; the second is the component props. 109 | 110 | 111 | [MIT License](LICENSE.md) 112 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-validate-hoc", 3 | "version": "1.0.0-b0", 4 | "description": "React higher order component for form validations", 5 | "main": "dist/withValidations.js", 6 | "scripts": { 7 | "prepublish": "babel src --out-dir dist", 8 | "start": "webpack-dev-server" 9 | }, 10 | "keywords": [], 11 | "author": "Brent Jackson", 12 | "license": "MIT", 13 | "devDependencies": { 14 | "axs": "^1.0.5", 15 | "axs-ui": "^1.0.4", 16 | "babel-cli": "^6.22.2", 17 | "babel-core": "^6.22.1", 18 | "babel-loader": "^6.2.10", 19 | "babel-preset-es2015": "^6.22.0", 20 | "babel-preset-react": "^6.22.0", 21 | "babel-preset-stage-0": "^6.22.0", 22 | "html-webpack-plugin": "^2.26.0", 23 | "react": "^15.4.2", 24 | "react-dom": "^15.4.2", 25 | "webpack": "^2.2.0", 26 | "webpack-dev-server": "^2.2.0" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/withValidations.js: -------------------------------------------------------------------------------- 1 | 2 | import React from 'react' 3 | 4 | const withValidations = (validations = {}) => (Component) => { 5 | class FormComponent extends React.Component { 6 | constructor () { 7 | super() 8 | this.state = { 9 | touched: {}, 10 | errors: {} 11 | } 12 | this.onBlur = this.onBlur.bind(this) 13 | this.validate = this.validate.bind(this) 14 | } 15 | 16 | onBlur (e) { 17 | const { name } = e.target 18 | const { touched } = this.state 19 | 20 | if (this.props[name] && this.props[name].length) { 21 | touched[name] = true 22 | this.setState({ touched }) 23 | this.validate(this.props) 24 | } 25 | } 26 | 27 | validate (props) { 28 | const errors = {} 29 | for (let key in validations) { 30 | const touched = this.state.touched[key] 31 | 32 | if (!touched) continue 33 | 34 | const validator = validations[key] 35 | const value = props[key] 36 | errors[key] = validator(value, props) 37 | } 38 | this.setState({ errors }) 39 | } 40 | 41 | componentWillReceiveProps (nextProps) { 42 | this.validate(nextProps) 43 | } 44 | 45 | render () { 46 | return ( 47 | 52 | ) 53 | } 54 | } 55 | 56 | return FormComponent 57 | } 58 | 59 | export default withValidations 60 | 61 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | 2 | const path = require('path') 3 | const HtmlWebpackPlugin = require('html-webpack-plugin') 4 | 5 | module.exports = { 6 | entry: './docs/entry.js', 7 | 8 | output: { 9 | path: path.join(__dirname, 'docs'), 10 | filename: 'bundle.js' 11 | }, 12 | 13 | resolve: { 14 | alias: { 15 | rev: path.join(__dirname, 'src/withValidations') 16 | } 17 | }, 18 | 19 | module: { 20 | rules: [ 21 | { 22 | test: /\.js$/, 23 | exclude: /node_modules/, 24 | use: 'babel-loader' 25 | } 26 | ] 27 | }, 28 | 29 | plugins: [ 30 | new HtmlWebpackPlugin({ 31 | template: 'docs/template.ejs' 32 | }) 33 | ], 34 | 35 | devServer: { 36 | contentBase: 'docs/' 37 | } 38 | } 39 | 40 | --------------------------------------------------------------------------------