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

Function LocaleControl

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

Source from the content-addressed store, hash-verified

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

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