|
2 | 2 | import React from 'react';
|
3 | 3 | import getError from './get-error';
|
4 | 4 |
|
| 5 | +const filters = { |
| 6 | + checkbox: value => value || value === false, |
| 7 | + radio: value => value || value === false, |
| 8 | + default: value => value, |
| 9 | +}; |
| 10 | + |
| 11 | +const getValue = (type, values) => { |
| 12 | + let filter = filters[type] || filters.default; |
| 13 | + |
| 14 | + let value = values.filter(filter)[0]; |
| 15 | + if (filter(value)) return value; |
| 16 | + |
| 17 | + return ''; |
| 18 | +}; |
| 19 | + |
5 | 20 | export default (child, propValues, children, errors, component) => {
|
6 |
| - let { name, onKeyUp, onEnter } = child.props; |
| 21 | + let { name, onKeyUp, onEnter, type } = child.props; |
7 | 22 |
|
8 | 23 | return React.cloneElement(child, {
|
9 | 24 | children,
|
10 | 25 | onChange: e =>
|
11 | 26 | component.validateOnBlurOrChange(name, () => component.onChange(e)),
|
12 | 27 | onBlur: () => component.validateOnBlurOrChange(name),
|
13 | 28 | error: getError(errors, component.props.errors, name),
|
14 |
| - value: |
15 |
| - component.state.values[name] || component.state.values[name] === false |
16 |
| - ? component.state.values[name] |
17 |
| - : propValues[name] || propValues[name] === false |
18 |
| - ? propValues[name] |
19 |
| - : '', |
| 29 | + value: getValue(type, [component.state.values[name], propValues[name]]), |
20 | 30 | onKeyUp: e => {
|
21 | 31 | if (onKeyUp) onKeyUp(e);
|
22 | 32 | if (e.keyCode !== 13) return;
|
|
0 commit comments