signal-desktop/ts/components/Select.tsx

73 lines
1.6 KiB
TypeScript
Raw Normal View History

// Copyright 2021-2022 Signal Messenger, LLC
2021-06-01 20:45:43 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { ChangeEvent } from 'react';
import React from 'react';
2021-06-01 20:45:43 +00:00
import classNames from 'classnames';
export type Option = Readonly<{
2021-08-05 12:35:33 +00:00
disabled?: boolean;
2021-06-01 20:45:43 +00:00
text: string;
value: string | number;
}>;
export type PropsType = Readonly<{
ariaLabel?: string;
2021-08-18 20:08:14 +00:00
disabled?: boolean;
id?: string;
2021-06-01 20:45:43 +00:00
moduleClassName?: string;
2021-08-18 20:08:14 +00:00
name?: string;
2021-06-01 20:45:43 +00:00
options: ReadonlyArray<Option>;
onChange(value: string): void;
2021-08-18 20:08:14 +00:00
value?: string | number;
2021-06-01 20:45:43 +00:00
}>;
export const Select = React.forwardRef(
(
{
ariaLabel,
disabled,
id,
moduleClassName,
name,
onChange,
options,
value,
}: PropsType,
ref: React.Ref<HTMLSelectElement>
): JSX.Element => {
const onSelectChange = (event: ChangeEvent<HTMLSelectElement>) => {
onChange(event.target.value);
};
2021-06-01 20:45:43 +00:00
return (
<div className={classNames(['module-select', moduleClassName])}>
<select
aria-label={ariaLabel}
disabled={disabled}
id={id}
name={name}
value={value}
onChange={onSelectChange}
ref={ref}
>
{options.map(
({ disabled: optionDisabled, text, value: optionValue }) => {
return (
<option
disabled={optionDisabled}
value={optionValue}
key={optionValue}
aria-label={text}
>
{text}
</option>
);
}
)}
</select>
</div>
);
}
);