fix(timezoneselector): Correct the order to match names first (#31941)

This commit is contained in:
Mehmet Salih Yavuz 2025-01-22 15:04:20 +03:00 committed by GitHub
parent 983aa827a8
commit 7383e4348b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 7 deletions

View File

@ -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();
});

View File

@ -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(),