2021-09-06 13:43:08 -07:00
|
|
|
import React, { useMemo } from 'react'
|
2021-09-04 21:42:37 -07:00
|
|
|
import { Listbox } from '@headlessui/react'
|
|
|
|
import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/solid'
|
2021-09-06 13:43:08 -07:00
|
|
|
import { isEqual } from '../utils'
|
2021-09-04 21:42:37 -07:00
|
|
|
|
|
|
|
const GroupSize = ({ tickSize, value, onChange, className = '' }) => {
|
2021-09-06 13:43:08 -07:00
|
|
|
const sizes = useMemo(
|
|
|
|
() => [
|
|
|
|
tickSize,
|
|
|
|
tickSize * 5,
|
|
|
|
tickSize * 10,
|
|
|
|
tickSize * 50,
|
|
|
|
tickSize * 100,
|
|
|
|
],
|
|
|
|
[tickSize]
|
|
|
|
)
|
|
|
|
|
2021-09-04 21:42:37 -07:00
|
|
|
return (
|
2021-09-05 06:44:30 -07:00
|
|
|
<div className={`${className}`}>
|
2021-09-04 21:42:37 -07:00
|
|
|
<Listbox value={value} onChange={onChange}>
|
|
|
|
{({ open }) => (
|
|
|
|
<>
|
2021-09-06 13:43:08 -07:00
|
|
|
<Listbox.Button
|
|
|
|
className={`font-normal border border-th-bkg-4 hover:border-th-primary rounded focus:outline-none focus:border-th-primary`}
|
2021-09-04 21:42:37 -07:00
|
|
|
>
|
|
|
|
<div
|
2021-09-06 13:43:08 -07:00
|
|
|
className={`flex items-center justify-between space-x-1 pr-1 pl-2`}
|
2021-09-04 21:42:37 -07:00
|
|
|
>
|
2021-09-06 13:43:08 -07:00
|
|
|
<span>{value}</span>
|
|
|
|
|
2021-09-04 21:42:37 -07:00
|
|
|
{open ? (
|
2021-09-06 07:30:55 -07:00
|
|
|
<ChevronUpIcon className={`h-5 w-5 text-th-primary`} />
|
2021-09-04 21:42:37 -07:00
|
|
|
) : (
|
2021-09-06 07:30:55 -07:00
|
|
|
<ChevronDownIcon className={`h-5 w-5 text-th-primary`} />
|
2021-09-04 21:42:37 -07:00
|
|
|
)}
|
|
|
|
</div>
|
2021-09-06 13:43:08 -07:00
|
|
|
</Listbox.Button>
|
2021-09-04 21:42:37 -07:00
|
|
|
{open ? (
|
|
|
|
<Listbox.Options
|
|
|
|
static
|
2021-09-06 07:30:55 -07:00
|
|
|
className={`w-2/3 z-40 p-1 absolute top-full right-0 mt-1 bg-th-bkg-1 origin-top-left divide-y divide-th-bkg-3 shadow-lg outline-none rounded-md`}
|
2021-09-04 21:42:37 -07:00
|
|
|
>
|
|
|
|
{sizes.map((size) => (
|
|
|
|
<Listbox.Option key={size} value={size}>
|
|
|
|
{({ selected }) => (
|
|
|
|
<div
|
2021-09-06 07:30:55 -07:00
|
|
|
className={`p-1 text-right hover:bg-th-bkg-2 hover:cursor-pointer tracking-wider ${
|
2021-09-04 21:42:37 -07:00
|
|
|
selected && `text-th-primary`
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{size}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Listbox.Option>
|
|
|
|
))}
|
|
|
|
</Listbox.Options>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Listbox>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:43:08 -07:00
|
|
|
export default React.memo(GroupSize, (prevProps, nextProps) =>
|
|
|
|
isEqual(prevProps, nextProps, ['tickSize', 'value'])
|
|
|
|
)
|