MCPcopy Create free account
hub / github.com/adobe/react-spectrum / LocaleControl

Function LocaleControl

packages/dev/s2-docs/src/VisualExampleClient.tsx:1245–1364  ·  view source on GitHub ↗
({control, value, onChange}: ControlProps)

Source from the content-addressed store, hash-verified

1243}
1244
1245function LocaleControl({control, value, onChange}: ControlProps) {
1246 let {locale: defaultLocale} = useLocale();
1247 let langDisplay = useMemo(
1248 () => new Intl.DisplayNames(defaultLocale, {type: 'language'}),
1249 [defaultLocale]
1250 );
1251 let regionDisplay = useMemo(
1252 () => new Intl.DisplayNames(defaultLocale, {type: 'region'}),
1253 [defaultLocale]
1254 );
1255 let locales = useMemo(() => {
1256 return preferences
1257 .map(item => {
1258 let locale = new Intl.Locale(item.value);
1259 return {
1260 ...item,
1261 label: `${langDisplay.of(locale.language)} (${regionDisplay.of(locale.region!)})`
1262 };
1263 })
1264 .sort((a, b) => a.label.localeCompare(b.label));
1265 }, [langDisplay, regionDisplay]);
1266
1267 let matched = useMemo(() => matchLocale(value), [value]);
1268 let pref = preferences.find(p => p.value === matched);
1269 // @ts-ignore there cannot be any undefined values in the array
1270 let preferredCalendars: Array<{key: string; name: string}> = useMemo(
1271 () =>
1272 pref
1273 ? (pref.ordering || 'gregory')
1274 .split(' ')
1275 .map(p => calendars.find(c => c.key === p))
1276 .filter(Boolean)
1277 : [calendars[0]],
1278 [pref]
1279 );
1280 let otherCalendars = useMemo(
1281 () => calendars.filter(c => !preferredCalendars.some(p => p?.key === c.key)),
1282 [preferredCalendars]
1283 );
1284
1285 let extension = control.options ?? 'calendar';
1286 let updateLocale = locale => {
1287 let calendar, numberingSystem;
1288 if (extension === 'calendar') {
1289 calendar = (preferences.find(p => p.value === locale)?.ordering || 'gregory').split(' ')[0];
1290 } else if (extension === 'numberingSystem') {
1291 numberingSystem = new Intl.NumberFormat(locale).resolvedOptions().numberingSystem;
1292 if (numberingSystem === 'arabext') {
1293 numberingSystem = 'arab';
1294 }
1295 }
1296 let newLocale = new Intl.Locale(locale, {
1297 calendar,
1298 numberingSystem
1299 });
1300 onChange(newLocale.toString());
1301 };
1302

Callers

nothing calls this directly

Calls 5

useLocaleFunction · 0.90
matchLocaleFunction · 0.70
sortMethod · 0.65
ofMethod · 0.65
filterMethod · 0.65

Tested by

no test coverage detected