-
Notifications
You must be signed in to change notification settings - Fork 37
/
KeyValue.js
51 lines (45 loc) · 1.16 KB
/
KeyValue.js
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
import React from 'react';
import PropTypes from 'prop-types';
import { pickBy } from 'lodash';
import NoValue from '../NoValue';
import css from './KeyValue.css';
const propTypes = {
children: PropTypes.node,
label: PropTypes.node,
subValue: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
value: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
};
function KeyValue({ children, label, value, subValue, ...rest }) {
// pull any data-test-* props into a spreadable object
const dataProps = pickBy(rest, (_, key) => /^data-test/.test(key));
const displayValue = value == null || value === '' ? <NoValue /> : value;
return (
<div className={css.kvRoot} {...dataProps}>
<div className={css.kvLabel}>
{label}
</div>
<div
className={css.kvValue}
data-test-kv-value
>
{children || displayValue}
</div>
{subValue && (
<em
data-test-kv-sub-value
className={css.kvSub}
>
{subValue}
</em>
)}
</div>
);
}
KeyValue.propTypes = propTypes;
export default KeyValue;