mango-ui-v2/components/Select.tsx

70 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-04-13 07:10:57 -07:00
import { Listbox } from '@headlessui/react'
import styled from '@emotion/styled'
2021-04-13 07:10:57 -07:00
import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/solid'
const StyledDiv = styled.div`
min-height: 2.5rem;
`
2021-04-24 19:10:28 -07:00
const Select = ({
value,
onChange,
children,
className = '',
placeholder = '',
disabled = false,
}) => {
2021-04-13 07:10:57 -07:00
return (
<div className={`relative ${className}`}>
2021-04-24 19:10:28 -07:00
<Listbox value={value} onChange={onChange} disabled={disabled}>
2021-04-13 07:10:57 -07:00
{({ open }) => (
<>
<Listbox.Button
className={`h-full w-full font-normal bg-th-bkg-1 ring-1 ring-th-fgd-4 ring-inset rounded hover:ring-th-primary focus:outline-none focus:border-th-primary`}
2021-04-13 07:10:57 -07:00
>
<StyledDiv
className={`flex items-center justify-between space-x-4 p-2 text-th-fgd-1`}
2021-04-13 07:10:57 -07:00
>
{value ? value : placeholder}
2021-04-13 07:10:57 -07:00
{open ? (
<ChevronUpIcon className={`h-5 w-5 mr-1 text-th-primary`} />
) : (
<ChevronDownIcon className={`h-5 w-5 mr-1 text-th-primary`} />
)}
</StyledDiv>
2021-04-13 07:10:57 -07:00
</Listbox.Button>
{open ? (
<Listbox.Options
static
2021-05-27 06:44:25 -07:00
className={`text-th-fgd-1 max-h-56 overflow-auto z-20 w-full p-1 absolute left-0 mt-1 bg-th-bkg-1 origin-top-left divide-y divide-th-bkg-3 shadow-lg outline-none rounded-md thin-scroll`}
2021-04-13 07:10:57 -07:00
>
{children}
2021-04-13 07:10:57 -07:00
</Listbox.Options>
) : null}
</>
)}
</Listbox>
</div>
)
}
2021-04-24 19:10:28 -07:00
const Option = ({ value, children, className = '' }) => {
return (
2021-04-24 19:10:28 -07:00
<Listbox.Option value={value}>
{({ selected }) => (
<div
className={`p-2 hover:bg-th-bkg-3 hover:cursor-pointer tracking-wider ${
selected && `text-th-primary`
2021-04-24 19:10:28 -07:00
} ${className}`}
>
{children}
</div>
)}
</Listbox.Option>
)
}
Select.Option = Option
2021-04-13 07:10:57 -07:00
export default Select