-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.tsx
More file actions
243 lines (223 loc) · 7.46 KB
/
Copy pathindex.tsx
File metadata and controls
243 lines (223 loc) · 7.46 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"use client";
import React, { useCallback, useState, forwardRef, useEffect } from "react";
// shadcn
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
// utils
import { cn } from "@/lib/utils";
// assets
import { ChevronDown, ChevronsUpDown, CheckIcon, Globe } from "lucide-react";
import { CircleFlag } from "react-circle-flags";
// data
import { countries } from "country-data-list";
// Change interface to type and update for multiple selection
export type Country = {
alpha2: string;
alpha3: string;
countryCallingCodes: string[];
currencies: string[];
emoji?: string;
ioc: string;
languages: string[];
name: string;
status: string;
};
type BaseCountryDropdownProps = {
options?: Country[];
disabled?: boolean;
placeholder?: string;
slim?: boolean;
inline?: boolean;
className?: string;
};
type SingleCountryDropdownProps = BaseCountryDropdownProps & {
multiple?: false;
onChange?: (country: Country) => void;
defaultValue?: string;
};
type MultipleCountryDropdownProps = BaseCountryDropdownProps & {
multiple: true;
onChange: (countries: Country[]) => void;
defaultValue?: string[];
};
type CountryDropdownProps =
| SingleCountryDropdownProps
| MultipleCountryDropdownProps;
const CountryDropdownComponent = (
{
options = countries.all.filter(
(country: Country) =>
country.emoji && country.status !== "deleted" && country.ioc !== "PRK"
),
onChange,
defaultValue,
disabled = false,
placeholder = "Select a country",
slim = false,
inline = false,
multiple = false,
className,
...props
}: CountryDropdownProps,
ref: React.ForwardedRef<HTMLButtonElement>
) => {
const [open, setOpen] = useState(false);
const [selectedCountries, setSelectedCountries] = useState<Country[]>([]);
useEffect(() => {
// Skip if no defaultValue
if (!defaultValue) {
if (selectedCountries.length > 0) {
setSelectedCountries([]);
}
return;
}
// For multiple selection
if (multiple && Array.isArray(defaultValue)) {
const currentValues = selectedCountries.map((c) => c.alpha3);
const hasChanges =
defaultValue.length !== currentValues.length ||
!defaultValue.every((v) => currentValues.includes(v));
if (hasChanges) {
const initialCountries = options.filter((country) =>
defaultValue.includes(country.alpha3)
);
setSelectedCountries(initialCountries);
}
}
// For single selection
else if (!multiple && typeof defaultValue === "string") {
const currentValue = selectedCountries[0]?.alpha3;
if (defaultValue !== currentValue) {
const initialCountry = options.find(
(country) => country.alpha3 === defaultValue
);
setSelectedCountries(initialCountry ? [initialCountry] : []);
}
}
}, [defaultValue, options, multiple]);
const handleSelect = useCallback(
(country: Country) => {
if (multiple) {
const newSelection = selectedCountries.some(
(c) => c.alpha3 === country.alpha3
)
? selectedCountries.filter((c) => c.alpha3 !== country.alpha3)
: [...selectedCountries, country];
setSelectedCountries(newSelection);
(onChange as MultipleCountryDropdownProps["onChange"])?.(newSelection);
} else {
setSelectedCountries([country]);
(onChange as SingleCountryDropdownProps["onChange"])?.(country);
setOpen(false);
}
},
[onChange, multiple, selectedCountries]
);
const triggerClasses = cn(
"flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1 hover:bg-secondary/80",
slim === true && "gap-1 w-min",
inline && "rounded-r-none border-r-0 gap-1 pr-1 w-min",
className
);
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger
ref={ref}
className={triggerClasses}
disabled={disabled}
{...props}
>
{selectedCountries.length > 0 ? (
<div className="flex items-center flex-grow gap-2 overflow-hidden">
{multiple ? (
<span className="overflow-hidden text-ellipsis whitespace-nowrap">
{selectedCountries.length} selected
</span>
) : (
<>
<div className="inline-flex items-center justify-center w-4 h-4 shrink-0 overflow-hidden rounded-full">
<CircleFlag
countryCode={selectedCountries[0].alpha2.toLowerCase()}
height={16}
/>
</div>
{slim === false && !inline && (
<span className="overflow-hidden text-ellipsis whitespace-nowrap">
{selectedCountries[0].name}
</span>
)}
</>
)}
</div>
) : (
<span className="flex items-center gap-2">
{inline || slim ? <Globe size={16} /> : placeholder}
</span>
)}
{!inline ? (
<ChevronDown size={16} />
) : (
<ChevronsUpDown size={16} className="text-muted-foreground" />
)}
</PopoverTrigger>
<PopoverContent
collisionPadding={10}
side="bottom"
className="min-w-[--radix-popper-anchor-width] p-0"
>
<Command className="w-full max-h-[200px] sm:max-h-[270px]">
<CommandList>
<div className="sticky top-0 z-10 bg-popover">
<CommandInput placeholder="Search country..." />
</div>
<CommandEmpty>No country found.</CommandEmpty>
<CommandGroup>
{options
.filter((x) => x.name)
.map((option, key: number) => (
<CommandItem
className="flex items-center w-full gap-2"
key={key}
onSelect={() => handleSelect(option)}
>
<div className="flex flex-grow space-x-2 overflow-hidden">
<div className="inline-flex items-center justify-center w-5 h-5 shrink-0 overflow-hidden rounded-full">
<CircleFlag
countryCode={option.alpha2.toLowerCase()}
height={20}
/>
</div>
<span className="overflow-hidden text-ellipsis whitespace-nowrap">
{option.name}
</span>
</div>
<CheckIcon
className={cn(
"ml-auto h-4 w-4 shrink-0",
selectedCountries.some((c) => c.name === option.name)
? "opacity-100"
: "opacity-0"
)}
/>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
};
CountryDropdownComponent.displayName = "CountryDropdownComponent";
export const CountryDropdown = forwardRef(CountryDropdownComponent);