fix(timezoneselector): Correct the order to match names first (#31941)
This commit is contained in:
parent
983aa827a8
commit
7383e4348b
|
|
@ -124,13 +124,12 @@ test('can update props and rerender with different values', async () => {
|
|||
timezone="Asia/Dubai"
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByTitle('GMT +04:00 (Asia/Baku)')).toBeInTheDocument();
|
||||
expect(screen.getByTitle('GMT +04:00 (Asia/Dubai)')).toBeInTheDocument();
|
||||
rerender(
|
||||
<TimezoneSelector
|
||||
onTimezoneChange={onTimezoneChange}
|
||||
timezone="Australia/Perth"
|
||||
/>,
|
||||
);
|
||||
expect(screen.getByTitle('GMT +08:00 (Asia/Brunei)')).toBeInTheDocument();
|
||||
expect(onTimezoneChange).toHaveBeenCalledTimes(2);
|
||||
expect(screen.getByTitle('GMT +08:00 (Australia/Perth)')).toBeInTheDocument();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -112,10 +112,23 @@ export default function TimezoneSelector({
|
|||
// pre-sort timezone options by time offset
|
||||
TIMEZONE_OPTIONS.sort(TIMEZONE_OPTIONS_SORT_COMPARATOR);
|
||||
|
||||
const matchTimezoneToOptions = (timezone: string) =>
|
||||
TIMEZONE_OPTIONS.find(
|
||||
option => option.offsets === getOffsetKey(timezone),
|
||||
)?.value || DEFAULT_TIMEZONE.value;
|
||||
const matchTimezoneToOptions = (timezone: string) => {
|
||||
const offsetKey = getOffsetKey(timezone);
|
||||
let fallbackValue: string | undefined;
|
||||
|
||||
for (const option of TIMEZONE_OPTIONS) {
|
||||
if (
|
||||
option.offsets === offsetKey &&
|
||||
option.timezoneName === timezone
|
||||
) {
|
||||
return option.value;
|
||||
}
|
||||
if (!fallbackValue && option.offsets === offsetKey) {
|
||||
fallbackValue = option.value;
|
||||
}
|
||||
}
|
||||
return fallbackValue || DEFAULT_TIMEZONE.value;
|
||||
};
|
||||
|
||||
const validTimezone = matchTimezoneToOptions(
|
||||
timezone || extendedDayjs.tz.guess(),
|
||||
|
|
|
|||
Loading…
Reference in New Issue