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

Function LocaleControl

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

Source from the content-addressed store, hash-verified

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

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