question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Listbox placeholder text

See original GitHub issue

Hi there,

Wondering if there is some way you can recommend to provide placeholder text for a Listbox control?

i.e. some initial text that gets displayed but doesn’t actually result in the Listbox having any selection value.

I was thinking you could have empty state to begin with and have some initial text in the <Listbox.Button>

For example:

import React, { useState } from 'react'
import { Listbox } from '@headlessui/react'

const people = [
  { id: 1, name: 'Durward Reynolds' },
  { id: 2, name: 'Kenton Towne' },
  { id: 3, name: 'Therese Wunsch' },
  { id: 4, name: 'Benedict Kessler' },
  { id: 5, name: 'Katelyn Rohan' },
]

function MyListbox() {
  const [selectedPerson, setSelectedPerson] = useState()

  return (
    <Listbox value={selectedPerson} onChange={setSelectedPerson}>
      <Listbox.Button>{selectedPerson ? selectedPerson.name : "Select person"}</Listbox.Button>
      <Listbox.Options>
        {people.map(person => (
          <Listbox.Option key={person.id} value={person}>
            {person.name}
          </Listbox.Option>
        ))}
      </Listbox.Options>
    </Listbox>
  )
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

3reactions
RobinMalfaitcommented, Jul 14, 2022

@OneTuskedMario not sure what you are trying to do, but you can add a placeholder to the input like this:

<Combobox.Input
  // ...
  placeholder="Search here..."
/>
1reaction
fullhdpixelcommented, Oct 20, 2021

@amrithmmh exactly as the OP stated. Just make sure to not use any value or label from the selectedOption without checking if it exists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make a placeholder in a listBox - Stack Overflow
to achieve your example you should use dropDownList instead of listBox echo $form->field($model, 'additionalRoles', [ 'options' => [ 'class' ...
Read more >
HeaderTitle or Placeholder for Listbox? | Angular - EJ 2 Forums
In the "value" I'm only getting the text, and the id is just 0, 1, etc.. I tried to add something like. [fields]="{...
Read more >
In a responsive web app, does the ListBox allow placeholder ...
In a responsive web app, does the ListBox allow placeholder text? I am not able to get placeholder text to work with a...
Read more >
How to Make a Placeholder for a Select Box in HTML
Answer: Use the disabled and selected Attribute. There is no attribute like input's placeholder for select box dropdown. However, you can create similar...
Read more >
Como colocar placeholder em listBox com Select2 - OutSystems
If you are using a listBox you can use "data-placeholder" as a extended property and as value use the text you want. This...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found