|
1
|
import { XMarkMini } from "@medusajs/icons"; |
|
2
|
import { Button, clx } from "@medusajs/ui"; |
|
3
|
import { Children, PropsWithChildren, createContext, useContext } from "react"; |
|
4
|
|
|
5
|
type ChipGroupVariant = "base" | "component"; |
|
6
|
|
|
7
|
type ChipGroupProps = PropsWithChildren<{ |
|
8
|
onClearAll?: () => void; |
|
9
|
onRemove?: (index: number) => void; |
|
10
|
variant?: ChipGroupVariant; |
|
11
|
className?: string; |
|
12
|
afterChildren?: React.ReactNode; |
|
13
|
}>; |
|
14
|
|
|
15
|
type GroupContextValue = { |
|
16
|
onRemove?: (index: number) => void; |
|
17
|
variant: ChipGroupVariant; |
|
18
|
}; |
|
19
|
|
|
20
|
const GroupContext = createContext<GroupContextValue | null>(null); |
|
21
|
|
|
22
|
const useGroupContext = () => { |
|
23
|
const context = useContext(GroupContext); |
|
24
|
|
|
25
|
if (!context) { |
|
26
|
throw new Error( |
|
27
|
"useGroupContext must be used within a ChipGroup component" |
|
28
|
); |
|
29
|
} |
|
30
|
|
|
31
|
return context; |
|
32
|
}; |
|
33
|
|
|
34
|
const Group = ({ |
|
35
|
onClearAll, |
|
36
|
onRemove, |
|
37
|
variant = "component", |
|
38
|
className, |
|
39
|
children, |
|
40
|
afterChildren, |
|
41
|
}: ChipGroupProps) => { |
|
42
|
const showClearAll = !!onClearAll && Children.count(children) > 0; |
|
43
|
|
|
44
|
return ( |
|
45
|
<GroupContext.Provider value={{ onRemove, variant }}> |
|
46
|
<ul |
|
47
|
role="application" |
|
48
|
className={clx("flex flex-wrap items-center gap-2", className)} |
|
49
|
> |
|
50
|
{children} |
|
51
|
{afterChildren} |
|
52
|
{showClearAll && ( |
|
53
|
<li> |
|
54
|
<Button |
|
55
|
size="small" |
|
56
|
variant="transparent" |
|
57
|
type="button" |
|
58
|
onClick={onClearAll} |
|
59
|
className="text-ui-fg-muted active:text-ui-fg-subtle" |
|
60
|
> |
|
61
|
Clear All |
|
62
|
</Button> |
|
63
|
</li> |
|
64
|
)} |
|
65
|
</ul> |
|
66
|
</GroupContext.Provider> |
|
67
|
); |
|
68
|
}; |
|
69
|
|
|
70
|
type ChipProps = PropsWithChildren<{ |
|
71
|
index: number; |
|
72
|
className?: string; |
|
73
|
}>; |
|
74
|
|
|
75
|
const Chip = ({ index, className, children }: ChipProps) => { |
|
76
|
const { onRemove, variant } = useGroupContext(); |
|
77
|
|
|
78
|
return ( |
|
79
|
<li |
|
80
|
className={clx( |
|
81
|
"bg-ui-bg-component shadow-borders-base flex items-stretch divide-x overflow-hidden rounded-md", |
|
82
|
{ |
|
83
|
"bg-ui-bg-component": variant === "component", |
|
84
|
"bg-ui-bg-base-": variant === "base", |
|
85
|
}, |
|
86
|
className |
|
87
|
)} |
|
88
|
> |
|
89
|
<span className="txt-compact-small-plus text-ui-fg-subtle flex items-center justify-center px-2 py-1"> |
|
90
|
{children} |
|
91
|
</span> |
|
92
|
{!!onRemove && ( |
|
93
|
<button |
|
94
|
onClick={() => onRemove(index)} |
|
95
|
type="button" |
|
96
|
className={clx( |
|
97
|
"text-ui-fg-muted active:text-ui-fg-subtle transition-fg flex items-center justify-center p-1", |
|
98
|
{ |
|
99
|
"hover:bg-ui-bg-component-hover active:bg-ui-bg-component-pressed": |
|
100
|
variant === "component", |
|
101
|
"hover:bg-ui-bg-base-hover active:bg-ui-bg-base-pressed": |
|
102
|
variant === "base", |
|
103
|
} |
|
104
|
)} |
|
105
|
> |
|
106
|
<XMarkMini /> |
|
107
|
</button> |
|
108
|
)} |
|
109
|
</li> |
|
110
|
); |
|
111
|
}; |
|
112
|
|
|
113
|
export const ChipGroup = Object.assign(Group, { Chip }); |