feat(SW-1509): new select component in design-system
This commit is contained in:
89
packages/design-system/lib/components/Select/Select.tsx
Normal file
89
packages/design-system/lib/components/Select/Select.tsx
Normal file
@@ -0,0 +1,89 @@
|
||||
import {
|
||||
Select as AriaSelect,
|
||||
SelectValue,
|
||||
Popover,
|
||||
ListBox,
|
||||
Button,
|
||||
} from 'react-aria-components'
|
||||
import { Typography } from '../Typography'
|
||||
|
||||
import styles from './select.module.css'
|
||||
import { MaterialIcon } from '../Icons/MaterialIcon'
|
||||
import { SelectItem } from './SelectItem'
|
||||
|
||||
import type { SelectProps } from './types'
|
||||
|
||||
export function Select({
|
||||
name,
|
||||
label,
|
||||
items,
|
||||
isRequired,
|
||||
isDisabled,
|
||||
icon,
|
||||
itemIcon,
|
||||
}: SelectProps) {
|
||||
const iconColor = isDisabled ? 'Icon/Interactive/Disabled' : 'Icon/Default'
|
||||
|
||||
return (
|
||||
<AriaSelect
|
||||
className={styles.select}
|
||||
name={name}
|
||||
aria-label={label}
|
||||
isRequired={isRequired}
|
||||
isDisabled={isDisabled}
|
||||
>
|
||||
<Button className={styles.button}>
|
||||
{icon ? (
|
||||
<MaterialIcon
|
||||
icon={icon}
|
||||
size={24}
|
||||
color={iconColor}
|
||||
aria-hidden="true"
|
||||
/>
|
||||
) : null}
|
||||
<SelectValue className={styles.selectValue}>
|
||||
{({ isPlaceholder, selectedText }) => (
|
||||
<>
|
||||
<Typography
|
||||
variant={
|
||||
isPlaceholder ? 'Body/Paragraph/mdRegular' : 'Label/xsRegular'
|
||||
}
|
||||
>
|
||||
<span className={styles.label}>{label}</span>
|
||||
</Typography>
|
||||
{!isPlaceholder && selectedText}
|
||||
</>
|
||||
)}
|
||||
</SelectValue>
|
||||
|
||||
<MaterialIcon
|
||||
icon="chevron_right"
|
||||
size={24}
|
||||
color={iconColor}
|
||||
aria-hidden="true"
|
||||
className={styles.chevron}
|
||||
/>
|
||||
</Button>
|
||||
<Popover className={styles.popover} shouldFlip={false}>
|
||||
<ListBox className={styles.listBox}>
|
||||
{items.map((item, idx) => (
|
||||
<Typography variant="Body/Paragraph/mdRegular" key={idx}>
|
||||
{typeof item === 'object' ? (
|
||||
<SelectItem
|
||||
icon={item.icon || itemIcon}
|
||||
isDisabled={item.isDisabled}
|
||||
>
|
||||
{item.label}
|
||||
</SelectItem>
|
||||
) : (
|
||||
<SelectItem icon={itemIcon} isDisabled={isDisabled}>
|
||||
{item.toString()}
|
||||
</SelectItem>
|
||||
)}
|
||||
</Typography>
|
||||
))}
|
||||
</ListBox>
|
||||
</Popover>
|
||||
</AriaSelect>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user