-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathField.js
139 lines (127 loc) · 4.02 KB
/
Field.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
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
import PropTypes from 'prop-types';
import React, {Component} from 'react';
import MenuPanel from '../containers/MenuPanel';
import classnames from 'classnames';
import {bem} from 'lib';
import {getMultiValueText} from 'lib/constants';
import {CloseIcon} from 'plotly-icons';
import nestedProperty from 'plotly.js/src/lib/nested_property';
export class FieldDelete extends Component {
render() {
const {onClick} = this.props;
return (
<div className="field__delete" onClick={onClick}>
<CloseIcon />
</div>
);
}
}
class Field extends Component {
render() {
const {
center,
children,
label,
multiValued,
suppressMultiValuedMessage,
units,
extraComponent,
fieldContainerClassName,
labelWidth,
noDefaultIndicator,
} = this.props;
const {localize: _, container, attr} = this.context;
let fieldClass;
if (!label) {
fieldClass = classnames('field__no-title', {
'field__no-title--center': center,
});
} else {
fieldClass = classnames('field__widget', {
'field__widget--units': Boolean(units),
});
}
let tooltip = this.context.attr;
if (this.context.description) {
tooltip += ' – ' + this.context.description.replace(/`/g, '"').replace(/\*/g, '"');
}
const containerClassName = classnames(bem('field'), {
[fieldContainerClassName]: Boolean(fieldContainerClassName),
});
const isDefaultValue = attr && nestedProperty(container, attr).get() !== undefined; // eslint-disable-line no-undefined
const defaultIndicatorClassName = classnames('field__default-indicator', {
'field__default-indicator__is-default': Boolean(isDefaultValue),
});
return (
<div className={containerClassName}>
{!noDefaultIndicator && (
<div onHover={this.setState({showClear: true})} className={defaultIndicatorClassName} />
)}
{!noDefaultIndicator && <div className="field__default-indicator__clear" />}
{label ? (
<div
className={bem('field', 'title')}
style={labelWidth ? {minWidth: labelWidth + 'px'} : {}}
>
{this.context.showFieldTooltips ? (
<div
className={bem('field', 'title-text')}
aria-label={tooltip}
data-microtip-position="bottom-right"
data-microtip-size="large"
role="tooltip"
>
{label}
</div>
) : (
<div className={bem('field', 'title-text')}>{label}</div>
)}
</div>
) : null}
<div className={fieldClass}>
{children}
{extraComponent ? extraComponent : null}
{multiValued && !suppressMultiValuedMessage ? (
<MenuPanel label={getMultiValueText('title', _)} ownline question>
<div className="info__title">{getMultiValueText('title', _)}</div>
<div className="info__text">{getMultiValueText('text', _)}</div>
<div className="info__sub-text">{getMultiValueText('subText', _)}</div>
</MenuPanel>
) : null}
</div>
{units ? (
<div className={bem('field', 'units')}>
<div className={bem('field', 'units-text')}>{units}</div>
</div>
) : null}
</div>
);
}
}
Field.propTypes = {
labelWidth: PropTypes.number,
center: PropTypes.bool,
label: PropTypes.any,
units: PropTypes.string,
multiValued: PropTypes.bool,
suppressMultiValuedMessage: PropTypes.bool,
children: PropTypes.node,
extraComponent: PropTypes.any,
fieldContainerClassName: PropTypes.string,
noDefaultIndicator: PropTypes.bool,
};
Field.contextTypes = {
localize: PropTypes.func,
description: PropTypes.string,
attr: PropTypes.string,
showFieldTooltips: PropTypes.bool,
container: PropTypes.object,
};
Field.defaultProps = {
center: false,
multiValued: false,
};
FieldDelete.propTypes = {
onClick: PropTypes.func,
};
export default Field;