signal-desktop/ts/components/Select.tsx

60 lines
1.3 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 React, { ChangeEvent } 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
}>;
2021-08-18 20:08:14 +00:00
export function Select({
disabled,
moduleClassName,
name,
onChange,
options,
value,
}: PropsType): JSX.Element {
const onSelectChange = (event: ChangeEvent<HTMLSelectElement>) => {
2021-06-01 20:45:43 +00:00
onChange(event.target.value);
};
return (
<div className={classNames(['module-select', moduleClassName])}>
2021-08-18 20:08:14 +00:00
<select
disabled={disabled}
name={name}
value={value}
onChange={onSelectChange}
>
{options.map(
({ disabled: optionDisabled, text, value: optionValue }) => {
return (
<option
disabled={optionDisabled}
value={optionValue}
key={optionValue}
aria-label={text}
>
{text}
</option>
);
}
)}
2021-06-01 20:45:43 +00:00
</select>
</div>
);
}