-
Notifications
You must be signed in to change notification settings - Fork 45
/
Tag.tsx
130 lines (111 loc) · 3.52 KB
/
Tag.tsx
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
import * as React from "react";
import { cn, isReactElement } from "../../helpers/utils";
import { type Avatar, type AvatarProps } from "../Avatar";
import { Badge, type BadgeElement } from "../Badge";
import { Button } from "../Button";
import { CloseIcon } from "../icons";
/* ---------------------------------- Types --------------------------------- */
type ClosableProps = {
/**
* Is the alert closable? If true, a close icon will be displayed.
* @default true
*/
closable: true;
/**
* An optional callback function to be called when the close icon is clicked.
* This can be used to handle the removal of the tag.
* If provided, the close icon will be displayed.
*/
onClose?: React.MouseEventHandler<HTMLButtonElement>;
};
type NotClosableProps = {
/**
* Is the alert closable? If true, a close icon will be displayed.
* @default true
*/
closable?: false;
/**
* An optional callback function to be called when the close icon is clicked.
* This can be used to handle the removal of the tag.
* If provided, the close icon will be displayed.
*/
onClose?: never;
};
export type TagProps = React.ComponentPropsWithoutRef<typeof Badge> & {
/**
* An optional avatar to display within the tag.
* Expects Avatar component.
*
* @example
* // Display a tag with an avatar
* <Tag avatar={<Avatar src="..." />} />
*/
avatar?: React.ReactElement<typeof Avatar>;
/**
* An optional element to display before the tag content.
* This can be used to display an icon or other element.
*/
// before?: React.ReactElement<HTMLElement>;
/**
* Specify alternative close icon to display within the tag
*
* * @example
* // Display a tag with an avatar
* <Tag closeIcon={<TrashIcon />} />.
*/
closeIcon?: React.ReactElement<HTMLElement>;
} & (ClosableProps | NotClosableProps);
/* ------------------------------- Components ------------------------------- */
const Tag = React.forwardRef<BadgeElement, TagProps>((props, ref) => {
const { avatar, before, closeIcon: deleteIcon, closable, onClose, ...otherProps } = props;
const [visible, setVisible] = React.useState(true);
/**
* Handle the close event.
* @param event - The event object
*/
const handleClose = React.useCallback(
(event: React.MouseEvent<HTMLButtonElement>) => {
if (onClose) {
onClose(event);
}
if (!event.defaultPrevented) {
setVisible(false);
}
},
[onClose]
);
const renderBefore = (
<>
{isReactElement(before)
? React.cloneElement(before, {
className: cn("size-4", before.props.className || ""),
})
: before}
{avatar &&
React.cloneElement(avatar, {
size: "xxs",
} as Partial<AvatarProps>)}
</>
);
const renderDeleteIcon = isReactElement(deleteIcon) ? (
deleteIcon
) : (
<CloseIcon aria-label="Close" className="size-4" />
);
const renderCloseButton: React.ReactElement<HTMLButtonElement> | undefined = closable ? (
<Button
before={renderDeleteIcon}
className="duration-180 size-auto p-0 text-wg-gray-400 transition-colors hover:text-wg-gray-600 focus:outline-1 dark:text-wg-white-500 dark:hover:text-wg-white-700 [&>svg]:!opacity-100"
shape="pill"
size="xs-icon"
variant="link"
onClick={handleClose}
/>
) : undefined;
if (!visible) {
return null;
}
return <Badge ref={ref} before={renderBefore} {...otherProps} after={renderCloseButton} />;
});
Tag.displayName = "Tag";
export default Tag;