-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCustomCompoundInput.jsx
129 lines (109 loc) · 2.87 KB
/
CustomCompoundInput.jsx
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Immutable from 'immutable';
import classNames from 'classnames';
import get from 'lodash/get';
import { getPath, pathPropType } from '../helpers/pathHelpers';
import { isInput } from '../helpers/inputHelpers';
import styles from '../../styles/cspace-input/CompoundInput.css';
const propTypes = {
children: PropTypes.node,
className: PropTypes.string,
defaultChildSubpath: pathPropType,
name: PropTypes.string,
// TODO: Stop using propTypes in isInput. Until then, these unused props need to be declared so
// this component is recognized as an input.
/* eslint-disable react/no-unused-prop-types */
parentPath: pathPropType,
subpath: pathPropType,
/* eslint-enable react/no-unused-prop-types */
readOnly: PropTypes.bool,
value: PropTypes.oneOfType([
PropTypes.object,
PropTypes.instanceOf(Immutable.Map),
]),
};
const defaultProps = {
children: undefined,
className: undefined,
defaultChildSubpath: undefined,
name: undefined,
parentPath: undefined,
subpath: undefined,
readOnly: undefined,
value: {},
};
const getChildValue = (value, subpath, name) => {
let keyPath = [];
if (subpath) {
keyPath = keyPath.concat(subpath);
}
if (name) {
keyPath = keyPath.concat(name);
}
if (keyPath.length === 0) {
return value;
}
if (Immutable.Map.isMap(value)) {
return value.getIn(keyPath);
}
return get(value, keyPath);
};
export default class CustomCompoundInput extends Component {
decorateInputs(children) {
const {
readOnly,
} = this.props;
return React.Children.map(children, (child) => {
if (!child || !child.type) {
// Undefined/null child or text node. Just return it.
return child;
}
if (isInput(child)) {
const {
name,
} = child.props;
let {
subpath,
} = child.props;
const {
defaultChildSubpath,
value,
} = this.props;
if (typeof subpath === 'undefined') {
subpath = defaultChildSubpath;
}
return React.cloneElement(child, {
readOnly,
subpath,
parentPath: getPath(this.props),
value: getChildValue(value, subpath, name),
});
}
return React.cloneElement(child, {
children: this.decorateInputs(child.props.children),
});
}, this);
}
render() {
const {
children,
className,
name,
readOnly,
} = this.props;
const classes = classNames(className, styles.common, {
[styles.readOnly]: readOnly,
});
return (
<fieldset
className={classes}
data-name={name}
>
{this.decorateInputs(children)}
</fieldset>
);
}
}
CustomCompoundInput.propTypes = propTypes;
CustomCompoundInput.defaultProps = defaultProps;