feat(preferences): Defaults segment

feat: Dropdown component
This commit is contained in:
Aman Harwara
2021-09-30 20:50:44 +05:30
parent 0a3521a25c
commit b4fcc993aa
6 changed files with 188 additions and 7 deletions

View File

@@ -0,0 +1,45 @@
import { Listbox, ListboxOption } from '@reach/listbox';
import VisuallyHidden from '@reach/visually-hidden';
import { FunctionComponent } from 'preact';
import { IconType } from './Icon';
import '@reach/listbox/styles.css';
import { useState } from 'preact/hooks';
export type DropdownItem = {
icon?: IconType;
label: string;
value: string;
};
type Props = {
id: string;
srLabel: string;
items: DropdownItem[];
defaultValue: string;
};
export const Dropdown: FunctionComponent<Props> = ({
id,
srLabel,
items,
defaultValue,
}) => {
const [value, setValue] = useState(defaultValue);
const labelId = `${id}-label`;
return (
<>
<VisuallyHidden id={labelId}>{srLabel}</VisuallyHidden>
<Listbox
value={value}
onChange={(value) => setValue(value)}
aria-labelledby={labelId}
>
{items.map((item) => (
<ListboxOption value={item.value}>{item.label}</ListboxOption>
))}
</Listbox>
</>
);
};