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