Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Combobox component #1047

Merged
merged 79 commits into from
Jan 27, 2022
Merged

Add Combobox component #1047

merged 79 commits into from
Jan 27, 2022

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Jan 18, 2022

This PR will add a new Combobox component.

This component will allow you to render an Input and Options (think of it as a Listbox).
You are in charge of actually filtering the items yourself.

Here is an example of the API using React:

let people = [
  { value: 'alice', name: 'alice' },
  { value: 'bob', name: 'bob' },
  { value: 'charlie', name: 'charlie' },
]

function Example(props) {
  let [value, setValue] = useState(undefined)
  let [query, setQuery] = useState('')

  let filteredPeople =
    query === ''
      ? people
      : people.filter(person => person.name.toLowerCase().includes(query.toLowerCase()))

  return (
    <Combobox value={value} onChange={setValue}>
      <Combobox.Input onChange={event => setQuery(event.target.value)} />
      <Combobox.Button></Combobox.Button>
      <Combobox.Options>
        {filteredPeople.map(person => (
          <Combobox.Option key={person.value} value={person.value}>
            {person.name}
          </Combobox.Option>
        ))}
      </Combobox.Options>
    </Combobox>
  )
}

Here is an example of the API using Vue:

let people = [
  { value: 'alice', name: 'alice' },
  { value: 'bob', name: 'bob' },
  { value: 'charlie', name: 'charlie' },
]

let Example = defineComponent({
  template: `
    <Combobox v-model="value">
      <ComboboxInput @change="setQuery" />
      <ComboboxButton>↕</ComboboxButton>
      <ComboboxOptions>
        <ComboboxOption
          v-for="person in filteredPeople"
          :key="person.value"
          :value="person.value"
        >
          {{ person.name }}
        </ComboboxOption>
      </ComboboxOptions>
    </Combobox>
  `,

  setup(props) {
    let value = ref(null)
    let query = ref('')

    let filteredPeople = computed(() => {
      return query.value === ''
        ? people
        : people.filter(person => person.name.toLowerCase().includes(query.value.toLowerCase()))
    })

    return {
      value,
      query,
      filteredPeople,
      setQuery: event => {
        query.value = event.target.value
      },
    }
  },
})

@vercel
Copy link

vercel bot commented Jan 18, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

headlessui-vue – ./packages/playground-vue

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-vue/6L9FuKMNxFDMGh2d4bZxufaN4qet
✅ Preview: https://headlessui-vue-git-combobox-tailwindlabs.vercel.app

[Deployment for 3e5fc4f failed]

headlessui-react – ./packages/playground-react

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-react/HnNsmg3hxWZ6yQAZa5c6565VrzeM
✅ Preview: https://headlessui-react-git-combobox-tailwindlabs.vercel.app

[Deployment for 3e5fc4f failed]

RobinMalfait and others added 11 commits January 20, 2022 14:14
The spec says that the combobox itself is labelled directly by the associated label. The button can however be labelled by the label or itself.
Right now the option list *is* just a listbox. If we were to allow other types in the future this will need to be changable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants