feat: add more generic Select component

This commit is contained in:
Arvid Norlin
2024-05-24 14:04:54 +02:00
parent 675228e4e9
commit 653cccaddb
4 changed files with 169 additions and 45 deletions

View File

@@ -0,0 +1,79 @@
"use client"
import { useRef } from "react"
import {
Button,
type Key,
Label,
ListBox,
ListBoxItem,
Popover,
Select as ReactAriaSelect,
SelectValue,
} from "react-aria-components"
import SelectChevron from "../SelectChevron"
import styles from "./select.module.css"
import type { SelectProps } from "./select"
export default function Select({
"aria-label": ariaLabel,
items,
label,
name,
onSelect,
placeholder,
value,
defaultSelectedKey,
}: SelectProps) {
const divRef = useRef<HTMLDivElement>(null)
function handleOnSelect(key: Key) {
onSelect(key)
}
return (
<div className={styles.date} ref={divRef}>
<ReactAriaSelect
defaultSelectedKey={defaultSelectedKey}
aria-label={ariaLabel}
className={styles.select}
onSelectionChange={handleOnSelect}
placeholder={placeholder}
selectedKey={value as Key}
>
<Label className={styles.label}>{label}</Label>
<Button className={styles.input}>
<SelectValue />
<SelectChevron />
</Button>
<Popover
className={styles.popover}
placement="bottom"
shouldFlip={false}
/**
* react-aria uses portals to render Popover in body
* unless otherwise specified. We need it to be contained
* by this component to both access css variables assigned
* on the container as well as to not overflow it at any time.
*/
UNSTABLE_portalContainer={divRef.current ?? undefined}
>
<ListBox className={styles.listBox}>
{items.map((item) => (
<ListBoxItem
aria-label={String(item)}
className={styles.listBoxItem}
id={item.value}
key={item.label}
>
{item.label}
</ListBoxItem>
))}
</ListBox>
</Popover>
</ReactAriaSelect>
</div>
)
}

View File

@@ -0,0 +1,57 @@
.date {
position: relative;
}
.label {
color: var(--Base-Text-UI-Placeholder);
font-family: var(--ff-fira-sans);
font-size: var(--typography-Footnote-Regular-fontSize);
font-weight: 400;
position: absolute;
left: 1.6rem;
}
.select {
display: flex;
flex-direction: column;
gap: 0.4rem;
}
.input {
align-items: end;
background-color: var(--some-white-color, #fff);
border: 1px solid var(--Base-Input-Controls-Border-Normal, #b8a79a);
border-radius: 0.8rem;
color: var(--some-black-color, #757575);
display: grid;
font-family: var(--ff-fira-sans);
font-size: 1.6rem;
font-weight: 400;
gap: 1rem;
grid-template-columns: 1fr auto;
height: 5.6rem;
/* letter-spacing: -1.5%; */
line-height: 2.4rem;
padding: 0.8rem 1.6rem;
}
.popover {
background-color: var(--some-white-color, #fff);
border: var(--border);
border-radius: var(--radius);
overflow: auto;
width: 100%;
}
.listBox {
padding: 1.6rem 1.6rem 1.6rem 0.8rem;
}
.listBoxItem {
padding: 0 0.8rem;
}
.listBoxItem[data-selected="true"],
.listBoxItem[data-focused="true"] {
background-color: rgba(75, 75, 75, 0.2);
}

View File

@@ -0,0 +1,11 @@
import type { Key } from "react-aria-components"
export interface SelectProps
extends Omit<React.SelectHTMLAttributes<HTMLSelectElement>, "onSelect"> {
items: { label: string; value: Key }[]
label: string
name: string
onSelect: (key: Key) => void
placeholder?: string
defaultSelectedKey: Key
}