Skip to content

Commit bb11af8

Browse files
committed
fix(locale-picker): complete first-day table and refine option search
Complete the REGION_FIRST_DAY fallback to mirror current CLDR for every currency-mapped region whose week does not start on Monday (40 entries): add the 27 missing regions (incl. ID and MV, which starts Friday), change SA to Sunday, and drop IQ, IR, LY, SD, SY and YE, which current CLDR reports as Monday-starting and which would contradict getWeekInfo on modern engines. AE is correctly absent (Monday). Omit the BCP 47 tag from an option's search text: every option shares the active language subtag, so typing it used to match every region. The region part of the tag is already the option's region code. Pin the fallback table to the runtime's week info with a source-reading test that compares it against getWeekInfo for all currency regions, and add a search regression test for the shared language subtag. Align the docs Usage section, the preview demo, and the PR snippet: imports block plus a bare component block, no state or wrapper.
1 parent 0a55fdc commit bb11af8

4 files changed

Lines changed: 122 additions & 51 deletions

File tree

‎content/docs/components/locale-pickers/locale-picker.mdx‎

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,15 @@ import { Tabs, Tab } from "fumadocs-ui/components/tabs";
3939
## Usage
4040

4141
```tsx
42-
"use client";
43-
44-
import { useState } from "react";
4542
import { LocalePicker } from "@/components/ui/locale-picker";
43+
```
4644

47-
export function SettingsRegion() {
48-
const [locale, setLocale] = useState("en-IN");
49-
50-
return (
51-
<LocalePicker
52-
regions={["IN", "US", "GB", "DE", "JP"]} // ISO 3166-1 alpha-2 codes
53-
defaultValue="en-IN" // omit for uncontrolled, use value instead
54-
onValueChange={setLocale} // receives the full BCP 47 tag, e.g. "en-US"
55-
/>
56-
);
57-
}
45+
```tsx
46+
<LocalePicker
47+
regions={["IN", "US", "GB", "DE", "JP"]}
48+
defaultValue="en-IN"
49+
onValueChange={(locale) => console.log(locale)}
50+
/>
5851
```
5952

6053
## Props
@@ -119,19 +112,7 @@ announced through `aria-activedescendant`.
119112
### Controlled
120113

121114
```tsx
122-
"use client";
123-
124-
import { LocalePicker } from "@/components/ui/locale-picker";
125-
126-
export function SettingsRegion({ locale, setLocale }) {
127-
return (
128-
<LocalePicker
129-
regions={["IN", "US", "GB", "DE", "JP"]}
130-
value={locale}
131-
onValueChange={setLocale}
132-
/>
133-
);
134-
}
115+
<LocalePicker regions={["IN", "US", "GB", "DE", "JP"]} value={locale} onValueChange={setLocale} />
135116
```
136117

137118
### Native region names
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
"use client";
22

3-
import { useState } from "react";
43
import { LocalePicker } from "@/registry/components/locale-picker";
54

65
export function LocalePickerDemo() {
7-
const [locale, setLocale] = useState("en-IN");
8-
96
return (
10-
<div className="flex w-full max-w-sm flex-col gap-3">
11-
<LocalePicker
12-
regions={["IN", "US", "GB", "DE", "JP"]} // ISO 3166-1 alpha-2 codes
13-
defaultValue="en-IN" // omit for uncontrolled, use value instead
14-
onValueChange={setLocale} // receives the full BCP 47 tag, e.g. "en-US"
15-
/>
16-
<p className="text-sm text-zinc-500 dark:text-zinc-400">
17-
Selected locale: <code>{locale}</code>
18-
</p>
19-
</div>
7+
<LocalePicker
8+
regions={["IN", "US", "GB", "DE", "JP"]}
9+
defaultValue="en-IN"
10+
onValueChange={(locale) => console.log(locale)}
11+
/>
2012
);
2113
}

‎src/registry/components/locale-picker.tsx‎

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,29 +128,53 @@ const REGION_CURRENCY: Record<string, string> = {
128128
};
129129

130130
/**
131-
* First day of week (1 = Monday … 7 = Sunday) for regions whose week starts
132-
* somewhere other than Monday, consulted only on engines without
133-
* `Intl.Locale.prototype.getWeekInfo`.
131+
* First day of week (1 = Monday … 7 = Sunday) for every currency-mapped
132+
* region whose CLDR week does not start on Monday, consulted only on engines
133+
* without `Intl.Locale.prototype.getWeekInfo`. Mirrors current CLDR (Monday-
134+
* starting regions are omitted) and is kept in sync with the runtime's week
135+
* data by the tests.
134136
*/
135137
const REGION_FIRST_DAY: Record<string, number> = {
136138
AF: 6,
139+
BD: 7,
137140
BH: 6,
141+
BR: 7,
142+
BZ: 7,
143+
CA: 7,
144+
CO: 7,
145+
DO: 7,
146+
DZ: 6,
138147
EG: 6,
148+
ET: 7,
149+
GT: 7,
150+
HK: 7,
151+
ID: 7,
139152
IL: 7,
140153
IN: 7,
141-
IQ: 6,
142-
IR: 6,
154+
IS: 7,
143155
JO: 6,
144156
JP: 7,
157+
KE: 7,
158+
KR: 7,
145159
KW: 6,
146-
LY: 6,
160+
MO: 7,
161+
MT: 7,
162+
MX: 7,
163+
MV: 5,
164+
NP: 7,
147165
OM: 6,
166+
PH: 7,
167+
PK: 7,
168+
PR: 7,
169+
PT: 7,
148170
QA: 6,
149-
SA: 6,
150-
SD: 6,
151-
SY: 6,
171+
SA: 7,
172+
SG: 7,
173+
TH: 7,
174+
TW: 7,
152175
US: 7,
153-
YE: 6,
176+
VE: 7,
177+
ZA: 7,
154178
};
155179

156180
/** English weekday names, used only when `Intl.DateTimeFormat` cannot help. */
@@ -369,7 +393,9 @@ export const LocalePicker = React.forwardRef<HTMLDivElement, LocalePickerProps>(
369393
label,
370394
flag: regionFlag(upper),
371395
rtl: isRtl(code),
372-
search: `${upper} ${label} ${english} ${code}`.toLowerCase(),
396+
// The BCP 47 tag is omitted: every option shares the active language
397+
// subtag, so it would match all of them. The region subtag is `upper`.
398+
search: `${upper} ${label} ${english}`.toLowerCase(),
373399
};
374400
});
375401

‎test/locale-picker.test.tsx‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,23 @@ describe("LocalePicker", () => {
207207
expect(screen.getByRole("option", { name: /Deutschland/ })).toBeInTheDocument();
208208
});
209209

210+
it("does not match every option on the shared language subtag", async () => {
211+
render(<LocalePicker regions={regions} defaultValue="en-IN" />);
212+
await userEvent.click(trigger());
213+
214+
// Every option's tag shares the active language subtag ("en-IN", "en-US",
215+
// …), so the tag must stay out of the search text — only the region part
216+
// of the tag is useful, and that is the region code itself.
217+
await userEvent.type(searchBox(), "en");
218+
expect(screen.queryAllByRole("option")).toHaveLength(0);
219+
expect(screen.getByText("No results")).toBeInTheDocument();
220+
221+
await userEvent.clear(searchBox());
222+
await userEvent.type(searchBox(), "ind");
223+
expect(screen.getAllByRole("option")).toHaveLength(1);
224+
expect(screen.getByRole("option", { name: /India/ })).toBeInTheDocument();
225+
});
226+
210227
it("renders deterministic markup for the server", () => {
211228
const html = renderToString(<LocalePicker regions={regions} defaultValue="en-IN" />);
212229

@@ -401,3 +418,58 @@ describe("LocalePicker currency table", () => {
401418
}
402419
});
403420
});
421+
422+
/**
423+
* The fallback table exists only for engines without `getWeekInfo` — if it
424+
* drifts from what `getWeekInfo` reports, those engines contradict modern
425+
* ones. Reading it from the source and comparing against the runtime's week
426+
* data pins every currency-mapped region: entries present, values current,
427+
* Monday-starting regions absent.
428+
*/
429+
const firstDayTableBlock = pickerSource.slice(
430+
pickerSource.indexOf("const REGION_FIRST_DAY"),
431+
pickerSource.indexOf("};", pickerSource.indexOf("const REGION_FIRST_DAY")),
432+
);
433+
const FIRST_DAY_TABLE = Object.fromEntries(
434+
[...firstDayTableBlock.matchAll(/^ {2}([A-Z]{2}): (\d),?$/gm)].map((match) => [
435+
match[1],
436+
Number(match[2]),
437+
]),
438+
);
439+
440+
const weekInfoSupported =
441+
typeof (new Intl.Locale("en") as Intl.Locale & { getWeekInfo?(): unknown }).getWeekInfo ===
442+
"function";
443+
444+
describe("LocalePicker first-day fallback table", () => {
445+
it.skipIf(!weekInfoSupported)(
446+
"mirrors the runtime's week info for every currency-mapped region",
447+
() => {
448+
const expected: Record<string, number> = {};
449+
for (const region of Object.keys(CURRENCY_TABLE)) {
450+
const tag = new Intl.Locale(`en-${region}`) as Intl.Locale & {
451+
getWeekInfo(): { firstDay: number };
452+
};
453+
const firstDay = tag.getWeekInfo().firstDay;
454+
if (firstDay !== 1) expected[region] = firstDay;
455+
}
456+
expect(FIRST_DAY_TABLE).toEqual(expected);
457+
},
458+
);
459+
460+
it("only holds non-Monday days for real regions", () => {
461+
expect(Object.keys(FIRST_DAY_TABLE).length).toBeGreaterThanOrEqual(35);
462+
expect(Object.values(FIRST_DAY_TABLE)).not.toContain(1);
463+
expect(FIRST_DAY_TABLE).toMatchObject({
464+
// Indonesia and Saudi Arabia start the week on Sunday.
465+
ID: 7,
466+
SA: 7,
467+
// The Maldives start it on Friday.
468+
MV: 5,
469+
// The United Arab Emirates start on Monday and must not appear.
470+
});
471+
expect(FIRST_DAY_TABLE.AE).toBeUndefined();
472+
expect(FIRST_DAY_TABLE).toHaveProperty("IN", 7);
473+
expect(FIRST_DAY_TABLE).toHaveProperty("US", 7);
474+
});
475+
});

0 commit comments

Comments
 (0)