headlessui icon indicating copy to clipboard operation
headlessui copied to clipboard

Error when trying to use Listbox as input and typing space

Open Dueen opened this issue 4 years ago • 1 comments

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v1.4.2

What browser are you using?

Chrome

Reproduction URL

Repo is private but here is an example snippet

import { Fragment, useState } from "react";
import { Listbox, Transition } from "@headlessui/react";
import { CheckIcon, SelectorIcon } from "@heroicons/react/solid";

const people = [
  { name: "Wade Cooper" },
  { name: "Arlene Mccoy" },
  { name: "Devon Webb" },
  { name: "Tom Cook" },
  { name: "Tanya Fox" },
  { name: "Hellen Schmidt" },
];

export default function IssueList() {
  const [selected, setSelected] = useState(people[0]);

  const [inputValue, setInputValue] = useState("");
  const handleInput = (e: any) => setInputValue(e.target.value);

  return (
    <div className='w-72 fixed top-16'>
      <Listbox value={selected} onChange={setSelected}>
        <div className='relative mt-1'>
          <Listbox.Button
            as='input'
            value={inputValue}
            onChange={handleInput}
            className='relative w-full 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-orange-300 focus-visible:ring-offset-2 focus-visible:border-indigo-500 sm:text-sm'
          />
          <Listbox.Options
            static
            className='absolute w-full py-1 mt-1 overflow-auto text-base bg-white rounded-md shadow-lg max-h-60 ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm'
          >
            {people.map((person, personIdx) => (
              <Listbox.Option
                key={personIdx}
                className={({ active }) =>
                  `${active ? "text-amber-900 bg-amber-100" : "text-gray-900"}
                          cursor-default select-none relative py-2 pl-10 pr-4`
                }
                value={person}
              >
                {({ selected, active }) => (
                  <>
                    <span
                      className={`${
                        selected ? "font-medium" : "font-normal"
                      } block truncate`}
                    >
                      {person.name}
                    </span>
                    {selected ? (
                      <span
                        className={`${
                          active ? "text-amber-600" : "text-amber-600"
                        }
                                absolute inset-y-0 left-0 flex items-center pl-3`}
                      >
                        <CheckIcon className='w-5 h-5' aria-hidden='true' />
                      </span>
                    ) : null}
                  </>
                )}
              </Listbox.Option>
            ))}
          </Listbox.Options>
        </div>
      </Listbox>
    </div>
  );
}

Describe your issue

When trying to use the Listbox with a Listbox.Button as=" input " component, due to the preventDefault of the handleKeyUp callback the space is not typed into the input but is disregarded and the input loses focus

Dueen avatar Feb 06 '22 21:02 Dueen

Hey! Thank you for your bug report! Much appreciated! 🙏

The Listbox is not meant to be used with an input, you should think of it as your <select> component that exists in native HTML.

We are working on a Combobox component where we do have an input field. This is already implemented in #254 and you can play with it using our insiders build if you want.

You can already try it using:

  • npm install @headlessui/react@insiders or yarn add @headlessui/react@insiders.
  • npm install @headlessui/vue@insiders or yarn add @headlessui/vue@insiders.

RobinMalfait avatar Feb 08 '22 10:02 RobinMalfait