RGAlex

List Button

Mar 2nd, 2022
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Fragment } from "react";
  2. import { Listbox, Transition } from "@headlessui/react";
  3. import { CheckIcon, ChevronDownIcon } from "@heroicons/react/solid";
  4.  
  5. function ListButton({ type, func, data }) {
  6.   return (
  7.     <Listbox value={type} onChange={func}>
  8.       <div className="relative mt-1">
  9.         <Listbox.Button className="relative w-44 py-2 pl-3 pr-10 text-left bg-white rounded-lg shadow-md cursor-default focus:outline-none focus-visible:ring-2 focus-visible:ring-opacity-75 focus-visible:ring-white  focus-visible:ring-offset-2 text-sm ring-1 ring-gray-100 hover:bg-gray-100">
  10.           <span className="block truncate">{type}</span>
  11.           <span className="absolute inset-y-0 right-0 flex items-center pr-2 pointer-events-none">
  12.             <ChevronDownIcon
  13.               className="w-5 h-5 text-gray-400"
  14.               aria-hidden="true"
  15.             />
  16.           </span>
  17.         </Listbox.Button>
  18.         <Transition
  19.           as={Fragment}
  20.           leave="transition ease-in duration-100"
  21.           leaveFrom="opacity-100"
  22.           leaveTo="opacity-0"
  23.         >
  24.           <Listbox.Options className="relative w-full py-1 mt-1 overflow-auto bg-white rounded-lg shadow-lg max-h-60 ring-opacity-5 focus:outline-none text-sm scrollbar-hide">
  25.             {data.map((data, index) => (
  26.               <Listbox.Option
  27.                 key={index}
  28.                 className={({ active }) =>
  29.                   `${active ? "text-blue-900 bg-blue-100" : "text-gray-900"}
  30.             cursor-default select-none relative pl-10 pr-4`
  31.                 }
  32.                 value={`${data.name}`}
  33.               >
  34.                 {({ selected, active }) => (
  35.                   <>
  36.                     <span
  37.                       className={`${
  38.                         selected ? "font-medium" : "font-normal"
  39.                       } block truncate`}
  40.                     >
  41.                       {`${data.name}`}
  42.                     </span>
  43.                     {selected ? (
  44.                       <span
  45.                         className={`${
  46.                           active ? "text-blue-600" : "text-blue-600"
  47.                         }
  48.                   absolute inset-y-0 left-0 flex items-center pl-3`}
  49.                       >
  50.                         <CheckIcon className="w-5 h-5" aria-hidden="true" />
  51.                       </span>
  52.                     ) : null}
  53.                   </>
  54.                 )}
  55.               </Listbox.Option>
  56.             ))}
  57.           </Listbox.Options>
  58.         </Transition>
  59.       </div>
  60.     </Listbox>
  61.   );
  62. }
  63.  
  64. export default ListButton;
Advertisement
Add Comment
Please, Sign In to add comment