Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-timezone-select",
"version": "3.3.1",
"version": "3.3.2",
"description": "Usable, dynamic React Timezone Select",
"scripts": {
"dev": "concurrently \"tsup --watch\" \"cd example && pnpm dev\"",
Expand Down
15 changes: 10 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,25 @@ export function useTimezoneSelect({
.sort((a: ITimezoneOption, b: ITimezoneOption) => a.offset - b.offset)
}, [labelStyle, timezones, currentDatetime])

const customTimezoneKeys = useMemo(() => {
const defaultKeys = new Set(Object.keys(allTimezones))
return new Set(Object.keys(timezones).filter((key) => !defaultKeys.has(key)))
}, [timezones])

const options = useMemo(() => {
return allOptions
.filter(
(item: ITimezoneOption, idx: number, arr: ITimezoneOption[]) =>
arr.findIndex((t) => t.offset === item.offset) === idx,
)
.filter((item: ITimezoneOption, idx: number, arr: ITimezoneOption[]) => {
if (customTimezoneKeys.has(item.value)) return true
return arr.findIndex((t) => t.offset === item.offset) === idx
})
.map((item) => ({
...item,
searchTerms: allOptions
.filter((t) => t.offset === item.offset)
.map((t) => t.label)
.join(" "),
}))
}, [allOptions])
}, [allOptions, customTimezoneKeys])

const filterOption = (option: { label: string; value: string; data: ITimezone }, inputValue: string): boolean => {
const data = option.data as ITimezoneOption
Expand Down
17 changes: 17 additions & 0 deletions src/tests/TimezoneSelect.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ test("load and displays custom timezone", async () => {
expect(getByText(/\(GMT-[5-6]:00\) Pittsburgh$/)).toBeInTheDocument()
})

test("custom timezone key not in default list is not deduped from dropdown", async () => {
const { getAllByText } = render(
<TimezoneSelect
value={"America/Detroit"}
timezones={{
...allTimezones,
"America/Detroit": "Detroit",
}}
menuIsOpen={true}
onChange={(e) => e}
/>,
)

// Detroit should appear in both the selected value and as a dropdown option
expect(getAllByText(/Detroit/).length).toBeGreaterThanOrEqual(2)
})

test("load and passes react-select props", async () => {
const { getByText } = render(
<TimezoneSelect
Expand Down