| 1 |
import { Listbox, Transition } from '@headlessui/react'; |
| 2 |
import { ChevronDownIcon } from '@heroicons/react/24/solid'; |
| 3 |
import { __ } from '@wordpress/i18n'; |
| 4 |
import { Fragment } from 'react'; |
| 5 |
import cn from '../utilities/cn'; |
| 6 |
|
| 7 |
const SingleSelect = ({ |
| 8 |
menu = false, |
| 9 |
placeholder = __('Select', 'disco'), |
| 10 |
items, |
| 11 |
onchange = () => {}, |
| 12 |
selected, |
| 13 |
disabled = false, |
| 14 |
className = '', |
| 15 |
buttonClass = '', |
| 16 |
proItems = [], |
| 17 |
proUrl = 'https://discoplugin.com/pricing/?utm_source=pro-text&utm_medium=free-to-pro&utm_campaign=free-to-pro&utm_id=1', |
| 18 |
}) => { |
| 19 |
return ( |
| 20 |
<Listbox |
| 21 |
className={`${className} ${disabled ? 'disco:opacity-50' : ''}`} |
| 22 |
disabled={disabled} |
| 23 |
value={selected} |
| 24 |
onChange={!disabled && onchange} |
| 25 |
> |
| 26 |
<div className="disco:relative"> |
| 27 |
<Listbox.Button |
| 28 |
className={cn( |
| 29 |
`disco:relative disco:cursor-pointer disco:w-full disco:rounded-md disco:border disco:text-base! disco:border-primary disco:py-2 disco:pl-2.5 disco:pr-8 disco:text-left disco:focus:outline-hidden`, |
| 30 |
buttonClass |
| 31 |
)} |
| 32 |
> |
| 33 |
<span className="disco:block disco:truncate"> |
| 34 |
{items[selected] || placeholder} |
| 35 |
</span> |
| 36 |
<span className="disco:pointer-events-none disco:absolute disco:inset-y-0 disco:right-0 disco:flex disco:items-center disco:pr-0.5"> |
| 37 |
<ChevronDownIcon |
| 38 |
className="disco:h-5 disco:w-5 disco:text-primary" |
| 39 |
aria-hidden="true" |
| 40 |
/> |
| 41 |
</span> |
| 42 |
</Listbox.Button> |
| 43 |
<Transition |
| 44 |
as={Fragment} |
| 45 |
leave="disco:transition disco:ease-in disco:duration-100" |
| 46 |
leaveFrom="disco:opacity-100" |
| 47 |
leaveTo="disco:opacity-0" |
| 48 |
> |
| 49 |
<Listbox.Options className="disco:z-50 disco:absolute disco:cursor-pointer disco:mt-1.5 disco:max-h-60 disco:w-full disco:overflow-auto disco:rounded-md disco:bg-white disco:text-base! disco:shadow-lg disco:ring-1 disco:ring-black/5 disco:focus:outline-hidden disco:sm:text-sm!"> |
| 50 |
{items && |
| 51 |
Object.keys(items).map((item) => ( |
| 52 |
<Listbox.Option |
| 53 |
key={item} |
| 54 |
disabled={proItems.includes(item)} |
| 55 |
className={({ active }) => |
| 56 |
`disco:relative disco:py-1 disco:pl-4 disco:mb-0 ${ |
| 57 |
proItems.includes(item) |
| 58 |
? 'disco:text-gray-400! disco:cursor-not-allowed!' |
| 59 |
: `disco:hover:bg-primary-light ${ |
| 60 |
active && !menu |
| 61 |
? 'disco:bg-primary-light' |
| 62 |
: 'disco:text-gray-900' |
| 63 |
}` |
| 64 |
}` |
| 65 |
} |
| 66 |
value={item} |
| 67 |
> |
| 68 |
{({ selected }) => ( |
| 69 |
<> |
| 70 |
<span |
| 71 |
className={`disco:block disco:truncate ${ |
| 72 |
selected && !menu |
| 73 |
? 'disco:font-medium!' |
| 74 |
: 'disco:font-normal!' |
| 75 |
}`} |
| 76 |
> |
| 77 |
{items[item]}{' '} |
| 78 |
{proItems.includes(item) && ( |
| 79 |
<span className="disco:bg-red-500 disco:px-1.5 disco:py-1 disco:text-xs disco:text-white disco:rounded-md"> |
| 80 |
<a |
| 81 |
href={proUrl} |
| 82 |
target="_blank" |
| 83 |
rel="noreferrer" |
| 84 |
onClick={(e) => |
| 85 |
e.stopPropagation() |
| 86 |
} |
| 87 |
className="disco:text-xs disco:text-white disco:hover:text-white! disco:focus:outline-hidden! disco:visited:text-white disco:focus:ring-0!" |
| 88 |
> |
| 89 |
{__('Pro', 'disco')} |
| 90 |
</a> |
| 91 |
</span> |
| 92 |
)} |
| 93 |
</span> |
| 94 |
</> |
| 95 |
)} |
| 96 |
</Listbox.Option> |
| 97 |
))} |
| 98 |
</Listbox.Options> |
| 99 |
</Transition> |
| 100 |
</div> |
| 101 |
</Listbox> |
| 102 |
); |
| 103 |
}; |
| 104 |
export default SingleSelect; |
| 105 |
|