1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-desktop/gui/Dropdown/Dropdown.tsx

55 lines
1.4 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import { useMemo, useCallback } from 'react';
export type DropdownOptions = Record<string, string>;
export enum DropdownVariant {
Default = 1,
NoBorder,
}
export interface ChangeEvent {
value: string;
}
export type ChangeEventHandler = (event: ChangeEvent)=> void;
interface Props {
options: DropdownOptions;
variant?: DropdownVariant;
className?: string;
onChange?: ChangeEventHandler;
value?: string;
disabled?: boolean;
}
export const Dropdown = (props: Props) => {
const renderOptions = () => {
const optionComps = [];
for (const [value, label] of Object.entries(props.options)) {
optionComps.push(<option key={value} value={value}>{label}</option>);
}
return optionComps;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
const onChange = useCallback((event: any) => {
props.onChange({ value: event.target.value });
}, [props.onChange]);
const classNames = useMemo(() => {
const variant = props.variant || DropdownVariant.Default;
const output = [
'dropdown-control',
`-variant${variant}`,
];
if (props.className) output.push(props.className);
return output.join(' ');
}, [props.variant, props.className]);
return (
<select disabled={props.disabled} className={classNames} onChange={onChange} value={props.value}>
{renderOptions()}
</select>
);
};