/
index.jsx
127 lines (113 loc) · 2.66 KB
/
index.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
import React from 'react';
import styled, { css } from 'styled-components';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import CancelIcon from '@material-ui/icons/Cancel';
import { useColor } from 'hooks/useColor';
const containedStyle = css`
background: ${(props) => props.$color};
color: #FFF;
`;
const outlinedStyle = css`
background: #FFF;
color: ${(props) => props.$color};
`;
const variantMap = {
contained: containedStyle,
outlined: outlinedStyle,
};
const StyledChip = styled.div`
display: inline-flex;
align-items: center;
border-radius: 50px;
height: 32px;
border: 1px solid ${(props) => props.$color};
${(props) => variantMap[props.$variant] || variantMap.contained}
.chip__start-icon {
margin-left: 4px;
margin-right: -6px;
}
.chip__end-icon {
margin-right: 4px;
margin-left: -6px;
${(props) => (props.$hasDelete ? 'cursor: pointer;' : null)}
&:hover {
${(props) => (props.$hasDelete ? 'opacity: 0.8;' : null)}
}
}
`;
const Label = styled.span`
padding: 0px 12px;
`;
/**
* `Chip` 元件用於標記事物的屬性、標籤或用於分類、篩選。
*/
const Chip = ({
className,
variant,
label,
themeColor,
icon,
deleteIcon,
onDelete,
}) => {
const { makeColor } = useColor();
const color = makeColor({ themeColor });
const endIcon = deleteIcon || <CancelIcon />;
return (
<StyledChip
className={className}
$variant={variant}
$color={color}
$hasDelete={!!onDelete}
>
{icon && React.cloneElement(icon, {
className: clsx(icon.props.className, 'chip__start-icon'),
})}
<Label>{label}</Label>
{(deleteIcon || onDelete) && React.cloneElement(endIcon, {
className: clsx(endIcon.props.className, 'chip__end-icon'),
onClick: onDelete,
})}
</StyledChip>
);
};
Chip.propTypes = {
/**
* 客製化樣式
*/
className: PropTypes.string,
/**
* 設置變化模式
*/
variant: PropTypes.oneOf(['contained', 'outlined']),
/**
* 內容
*/
label: PropTypes.oneOfType([PropTypes.element, PropTypes.string]).isRequired,
/**
* 主題配色,primary、secondary 或是自己傳入色票
*/
themeColor: PropTypes.oneOfType([PropTypes.oneOf(['primary', 'secondary']), PropTypes.string]),
/**
* 圖示
*/
icon: PropTypes.element,
/**
* 刪除圖示
*/
deleteIcon: PropTypes.element,
/**
* 刪除事件
*/
onDelete: PropTypes.oneOfType([PropTypes.func, PropTypes.oneOf([null])]),
};
Chip.defaultProps = {
className: null,
variant: 'contained',
themeColor: 'primary',
icon: null,
deleteIcon: null,
onDelete: null,
};
export default Chip;