-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Chip.js
312 lines (279 loc) · 8.08 KB
/
Chip.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import keycode from 'keycode';
import {fade, emphasize} from '../utils/colorManipulator';
import EnhancedButton from '../internal/EnhancedButton';
import DeleteIcon from '../svg-icons/navigation/cancel';
function getStyles(props, context, state) {
const {chip} = context.muiTheme;
const backgroundColor = props.backgroundColor || chip.backgroundColor;
const focusColor = emphasize(backgroundColor, 0.08);
const pressedColor = emphasize(backgroundColor, 0.12);
return {
avatar: {
marginRight: -4,
},
deleteIcon: {
color: (state.deleteHovered) ? fade(chip.deleteIconColor, 0.4) : chip.deleteIconColor,
cursor: 'pointer',
margin: '4px 4px 0px -8px',
},
label: {
color: props.labelColor || chip.textColor,
fontSize: chip.fontSize,
fontWeight: chip.fontWeight,
lineHeight: '32px',
paddingLeft: 12,
paddingRight: 12,
userSelect: 'none',
whiteSpace: 'nowrap',
},
root: {
backgroundColor: state.clicked ? pressedColor : (state.focused || state.hovered) ? focusColor : backgroundColor,
borderRadius: 16,
boxShadow: state.clicked ? chip.shadow : null,
cursor: props.onClick ? 'pointer' : 'default',
display: 'flex',
whiteSpace: 'nowrap',
width: 'fit-content',
},
};
}
class Chip extends Component {
static propTypes = {
/**
* Override the background color of the chip.
*/
backgroundColor: PropTypes.string,
/**
* Used to render elements inside the Chip.
*/
children: PropTypes.node,
/**
* CSS `className` of the root element.
*/
className: PropTypes.node,
/**
* The element to use as the container for the Chip. Either a string to
* use a DOM element or a ReactElement.
*/
containerElement: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
]),
/**
* Override the inline-styles of the delete icon.
*/
deleteIconStyle: PropTypes.object,
/**
* Override the label color.
*/
labelColor: PropTypes.string,
/**
* Override the inline-styles of the label.
*/
labelStyle: PropTypes.object,
/** @ignore */
onBlur: PropTypes.func,
/**
* Callback function fired when the `Chip` element is clicked.
*
* @param {object} event Click event targeting the element.
*/
onClick: PropTypes.func,
/** @ignore */
onFocus: PropTypes.func,
/** @ignore */
onKeyDown: PropTypes.func,
/** @ignore */
onKeyboardFocus: PropTypes.func,
/** @ignore */
onMouseDown: PropTypes.func,
/** @ignore */
onMouseEnter: PropTypes.func,
/** @ignore */
onMouseLeave: PropTypes.func,
/** @ignore */
onMouseUp: PropTypes.func,
/**
* Callback function fired when the delete icon is clicked. If set, the delete icon will be shown.
* @param {object} event `click` event targeting the element.
*/
onRequestDelete: PropTypes.func,
/** @ignore */
onTouchEnd: PropTypes.func,
/** @ignore */
onTouchStart: PropTypes.func,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
};
static defaultProps = {
containerElement: 'div', // Firefox doesn't support nested buttons
onBlur: () => {},
onFocus: () => {},
onKeyDown: () => {},
onKeyboardFocus: () => {},
onMouseDown: () => {},
onMouseEnter: () => {},
onMouseLeave: () => {},
onMouseUp: () => {},
onTouchEnd: () => {},
onTouchStart: () => {},
};
static contextTypes = {muiTheme: PropTypes.object.isRequired};
state = {
clicked: false,
deleteHovered: false,
focused: false,
hovered: false,
};
handleBlur = (event) => {
this.setState({clicked: false, focused: false});
this.props.onBlur(event);
};
handleFocus = (event) => {
if (this.props.onClick || this.props.onRequestDelete) {
this.setState({focused: true});
}
this.props.onFocus(event);
};
handleKeyboardFocus = (event, keyboardFocused) => {
if (keyboardFocused) {
this.handleFocus();
this.props.onFocus(event);
} else {
this.handleBlur();
}
this.props.onKeyboardFocus(event, keyboardFocused);
};
handleKeyDown = (event) => {
if (keycode(event) === 'backspace') {
event.preventDefault();
if (this.props.onRequestDelete) {
this.props.onRequestDelete(event);
}
}
this.props.onKeyDown(event);
};
handleMouseDown = (event) => {
// Only listen to left clicks
if (event.button === 0) {
event.stopPropagation();
if (this.props.onClick) {
this.setState({clicked: true});
}
}
this.props.onMouseDown(event);
};
handleMouseEnter = (event) => {
if (this.props.onClick) {
this.setState({hovered: true});
}
this.props.onMouseEnter(event);
};
handleMouseEnterDeleteIcon = () => {
this.setState({deleteHovered: true});
};
handleMouseLeave = (event) => {
this.setState({
clicked: false,
hovered: false,
});
this.props.onMouseLeave(event);
};
handleMouseLeaveDeleteIcon = () => {
this.setState({deleteHovered: false});
};
handleMouseUp = (event) => {
this.setState({clicked: false});
this.props.onMouseUp(event);
};
handleClickDeleteIcon = (event) => {
// Stop the event from bubbling up to the `Chip`
event.stopPropagation();
this.props.onRequestDelete(event);
};
handleTouchEnd = (event) => {
this.setState({clicked: false});
this.props.onTouchEnd(event);
};
handleTouchStart = (event) => {
event.stopPropagation();
if (this.props.onClick) {
this.setState({clicked: true});
}
this.props.onTouchStart(event);
};
render() {
const buttonEventHandlers = {
onBlur: this.handleBlur,
onFocus: this.handleFocus,
onKeyDown: this.handleKeyDown,
onMouseDown: this.handleMouseDown,
onMouseEnter: this.handleMouseEnter,
onMouseLeave: this.handleMouseLeave,
onMouseUp: this.handleMouseUp,
onTouchEnd: this.handleTouchEnd,
onTouchStart: this.handleTouchStart,
onKeyboardFocus: this.handleKeyboardFocus,
};
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context, this.state);
const {
children: childrenProp,
containerElement,
style,
className,
deleteIconStyle,
labelStyle,
labelColor, // eslint-disable-line no-unused-vars,prefer-const
backgroundColor, // eslint-disable-line no-unused-vars,prefer-const
onRequestDelete, // eslint-disable-line no-unused-vars,prefer-const
...other
} = this.props;
const deletable = this.props.onRequestDelete;
let avatar = null;
const deleteIcon = deletable ? (
<DeleteIcon
color={styles.deleteIcon.color}
style={Object.assign(styles.deleteIcon, deleteIconStyle)}
onClick={this.handleClickDeleteIcon}
onMouseEnter={this.handleMouseEnterDeleteIcon}
onMouseLeave={this.handleMouseLeaveDeleteIcon}
/>
) : null;
let children = childrenProp;
const childCount = React.Children.count(children);
// If the first child is an avatar, extract it and style it
if (childCount > 1) {
children = React.Children.toArray(children);
if (React.isValidElement(children[0]) && children[0].type.muiName === 'Avatar') {
avatar = children.shift();
avatar = React.cloneElement(avatar, {
style: Object.assign(styles.avatar, avatar.props.style),
size: 32,
});
}
}
return (
<EnhancedButton
{...other}
{...buttonEventHandlers}
className={className}
containerElement={containerElement}
disableTouchRipple={true}
disableFocusRipple={true}
style={Object.assign(styles.root, style)}
>
{avatar}
<span style={prepareStyles(Object.assign(styles.label, labelStyle))}>
{children}
</span>
{deleteIcon}
</EnhancedButton>
);
}
}
export default Chip;