forked from yuristrelets/react-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChip.js
More file actions
62 lines (55 loc) · 1.84 KB
/
Chip.js
File metadata and controls
62 lines (55 loc) · 1.84 KB
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
import React, { PropTypes } from 'react';
import classnames from 'classnames';
import { themr } from 'react-css-themr';
import { CHIP } from '../identifiers.js';
import InjectAvatar from '../avatar/Avatar.js';
const factory = (Avatar) => {
const Chip = ({children, className, deletable, onDeleteClick, theme, ...other}) => {
let hasAvatar = false;
if (React.Children.count(children)) {
const firstChild = children[0];
hasAvatar = firstChild && firstChild.type && firstChild.type === Avatar;
}
const classes = classnames(theme.chip, {
[theme.deletable]: !!deletable,
[theme.avatar]: !!hasAvatar
}, className);
return (
<div data-react-toolbox='chip' className={classes} {...other}>
{typeof children === 'string' ? <span>{children}</span> : children}
{
deletable ? (
<span className={theme.delete} onClick={onDeleteClick}>
<svg viewBox="0 0 40 40" className={theme.deleteIcon}>
<path className={theme.deleteX} d="M 12,12 L 28,28 M 28,12 L 12,28" />
</svg>
</span>
) : null
}
</div>
);
};
Chip.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
deletable: PropTypes.bool,
onDeleteClick: PropTypes.func,
theme: React.PropTypes.shape({
avatar: React.PropTypes.string.isRequired,
chip: React.PropTypes.string.isRequired,
deletable: React.PropTypes.string.isRequired,
delete: React.PropTypes.string.isRequired,
deleteIcon: React.PropTypes.string.isRequired,
deleteX: React.PropTypes.string.isRequired
})
};
Chip.defaultProps = {
className: '',
deletable: false
};
return Chip;
};
const Chip = factory(InjectAvatar);
export default themr(CHIP)(Chip);
export { factory as chipFactory };
export { Chip };