Fix DisappearingTimerSelect on Windows

This commit is contained in:
Fedor Indutny 2022-12-14 15:59:09 -08:00 committed by GitHub
parent 7c86f02c7e
commit daf66f33da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 21 deletions

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react'; import type { ReactNode } from 'react';
import React, { useState } from 'react'; import React, { useCallback, useState, useMemo } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import type { LocalizerType } from '../types/Util'; import type { LocalizerType } from '../types/Util';
@ -29,7 +29,8 @@ export function DisappearingTimerSelect(props: Props): JSX.Element {
let expirationTimerOptions: ReadonlyArray<{ let expirationTimerOptions: ReadonlyArray<{
readonly value: DurationInSeconds; readonly value: DurationInSeconds;
readonly text: string; readonly text: string;
}> = expirationTimer.DEFAULT_DURATIONS_IN_SECONDS.map(seconds => { }> = useMemo(() => {
return expirationTimer.DEFAULT_DURATIONS_IN_SECONDS.map(seconds => {
const text = expirationTimer.format(i18n, seconds, { const text = expirationTimer.format(i18n, seconds, {
capitalizeOff: true, capitalizeOff: true,
}); });
@ -38,18 +39,26 @@ export function DisappearingTimerSelect(props: Props): JSX.Element {
text, text,
}; };
}); });
}, [i18n]);
const isCustomTimeSelected = const isCustomTimeSelected =
!expirationTimer.DEFAULT_DURATIONS_SET.has(value); !expirationTimer.DEFAULT_DURATIONS_SET.has(value);
const onSelectChange = (newValue: string) => { const onSelectChange = useCallback(
(newValue: string) => {
const intValue = DurationInSeconds.fromSeconds(parseInt(newValue, 10)); const intValue = DurationInSeconds.fromSeconds(parseInt(newValue, 10));
if (intValue === -1) { if (intValue === -1) {
setIsModalOpen(true); // On Windows we get "change" event followed by "click" (even if
// the <option/> was selected with keyboard. This click unfortunately
// closes the modal so we need to delay opening it until after the
// "click" event.
setTimeout(() => setIsModalOpen(true), 0);
} else { } else {
onChange(intValue); onChange(intValue);
} }
}; },
[onChange]
);
// Custom time... // Custom time...
expirationTimerOptions = [ expirationTimerOptions = [

View file

@ -2,7 +2,7 @@
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import type { ChangeEvent } from 'react'; import type { ChangeEvent } from 'react';
import React from 'react'; import React, { useCallback } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
export type Option = Readonly<{ export type Option = Readonly<{
@ -35,9 +35,12 @@ export const Select = React.forwardRef(function SelectInner(
}: PropsType, }: PropsType,
ref: React.Ref<HTMLSelectElement> ref: React.Ref<HTMLSelectElement>
): JSX.Element { ): JSX.Element {
const onSelectChange = (event: ChangeEvent<HTMLSelectElement>) => { const onSelectChange = useCallback(
(event: ChangeEvent<HTMLSelectElement>) => {
onChange(event.target.value); onChange(event.target.value);
}; },
[onChange]
);
return ( return (
<div className={classNames(['module-select', moduleClassName])}> <div className={classNames(['module-select', moduleClassName])}>