signal-desktop/ts/components/Select.tsx

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-06-01 20:45:43 +00:00
// Copyright 2021 Signal Messenger, LLC
// 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<{
2021-08-18 20:08:14 +00:00
disabled?: boolean;
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(
(
{ disabled, 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
disabled={disabled}
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>
);
}
);