| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import * as React from 'react'; import PropTypes from 'prop-types'; import classnames from 'classnames'; import uuid from 'uuid'; let opositeProperty = function(obj, key, targetKey) { Object.defineProperty(obj, key, { get: () => !obj[targetKey], set: val => obj[targetKey] = !val, enumerable: true }); } class Control extends React.Component { constructor(props) { super(props); this.controlState = { focus: false, touched: false, dirty: false, invalid: false, pending: false, empty: true, }; opositeProperty(this.controlState, 'blur', 'focus'); opositeProperty(this.controlState, 'untouched', 'touched'); opositeProperty(this.controlState, 'pristine', 'dirty'); opositeProperty(this.controlState, 'valid', 'invalid'); opositeProperty(this.controlState, 'with-value', 'empty'); this._errors = []; } static defaultProps = { isEmpty: v => !Boolean(v), validators: [], showErrorOn: ['touched'], multipleErrors: false } static contextTypes = { onControlMount: PropTypes.func, onControlUnmount: PropTypes.func, onControlChange: PropTypes.func, } componentDidMount() { this._id = uuid.v4(); this.context.onControlMount(this._id, this); } componentWillUnmount() { this.context.onControlUnmount(this._id, this); } get value() { return this._value; } setForm(f) { this._form = f; } getControlState() { let state = {...this.state}; for (let [stateKey, targetKey] of Object.entries(this.opositeState)) { state[targetKey] = !this.state[stateKey]; } return state; } handleFocus(e) { this.controlState.focus = true; this.forceUpdate(); if (this.props.onFocus) { this.props.onFocus(e); } } handleBlur(e) { this.controlState.focus = false; this.controlState.touched = true; this.forceUpdate(); if (this.props.onBlur) { this.props.onBlur(e); } } handleChange(e) { let val = e.target.value; this.controlState.dirty = true; this.controlState.empty = this.props.isEmpty(val); this._value = val; this.context.onControlChange(this._id, val); this.validate(); this.forceUpdate(); if (this.props.onChange) { this.props.onChange(e); } } validate() { let data = this._form.value; this._errors = this.props.validators.filter(v => !v.f(this.value, data)); } render() { let props = {...this.props}; let errors = this._errors; if (props.showErrorOn.find(s => !this.controlState[s])) { errors = []; } if (!props.multipleErrors) { errors = errors.slice(0, 1); } let errElement = null; if (errors.length) { errElement = ( <div className='form-group__errors'> {errors.map((e, i) => { let msg = e.msg; if (typeof msg == 'function') { msg = msg(this.value, this._form.value); } return <div className='form-group-error' key={i}>{msg}</div>; })} </div> ); } let baseCls = 'form-group'; let classes = classnames(['form-group', ...Object.entries(this.controlState).filter(v => v[1]).map(v => v[0]), {[`${baseCls}_has-error`]: errors.length > 0} ]); delete props['validators']; delete props['component']; delete props['showErrorOn']; delete props['isEmpty']; delete props['multipleErrors']; return ( <div className={classes}> {this.props.component({ state: this.controlState, errors, controlProps: { ...props, onChange: ::this.handleChange, onBlur: ::this.handleBlur, onFocus: ::this.handleFocus, } })} {errElement} </div> ); } } export default Control; |