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.

Custom Dropdown/Select

See original GitHub issue

Description

Create a custom, keyboard compliant dropdown/select component with search capabilities.

Problem Statement/Justification

  • Most designs and apps need a more dynamic select component, such as react-select.
  • I needed to create a custom Select component, while using Chakra-UI’s infra and system utils, hooks. Referencing the Menu component, I added optional search to filter out the listed options, the DescendantsContext for keyboard focus management, along with custom SelectOptions for rending labels and optional icons, etc.

Proposed Solution or API

I found the react-select format much more versatile in terms of controlling and rendering data.

Select will render and array of OptionTypes.

export type OptionType = {
  icon?: string;
  label: string;
  value: string; 
}

Example usage:

function SelectUsage() {
  const [state, setState] = React.useState(null)
  const options = [
    { label: 'Green', value: 'green' },
    { label: 'Green-Yellow', value: 'greenyellow' },
    { label: 'Red', value: 'red' },
    { label: 'Violet', value: 'violet' },
    { label: 'Forest', value: 'forest' },
    { label: 'Tangerine', value: 'tangerine' },
    { label: 'Blush', value: 'blush' },
    { label: 'Purple', value: 'purple' },
  ]
  return (
    <>
      <Select
        isSearchable
        defaultValue={options[0].value}
        value={state}
        options={options}
        onChange={(value) => setState(value)}
      />
    </>
  )
}
  • container SelectDescendantsProvider, SelectProvider & StylesProvider
  • Select consumes four internal components, each with its relative context.
<SelectContainer />
  <SelectInputValue />
<SelectList />
   <SelectOption />

Alternatives

  • Structure: maybe moving the components out of the context, leaving the developers more control of the general structure of the elements? I found this be more difficult in terms of controlling data.
<Select>
  <SelectContainer>
    <SelectInputValue /> // handles current state, and search state
  </SelectContainer>
  <SelectList>
    <SelectOption label="Green" value="green" />
    <SelectOption label="Red" value="red" />
    <SelectOption label="Yellow" value="yellow" />
    <SelectOption label="Blue" value="blue" />
    <SelectOption label="Black" value="black" />
  </SelectList>
</Select>

Additional Information

  • current component development

https://user-images.githubusercontent.com/19299064/124046312-e5fd3880-d9ce-11eb-8938-25b946a9cfd9.mov

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:43
  • Comments:39 (1 by maintainers)

github_iconTop GitHub Comments

29reactions
csandmancommented, Sep 29, 2021

I made a chakra wrapper for react-select recently, if anyone else is looking for something like this! https://github.com/csandman/chakra-react-select

15reactions
anubra266commented, Jul 1, 2021

You could check out choc autocomplete.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Create Custom Select Menus - W3Schools
Learn how to create custom select boxes with CSS and JavaScript. ... Surround the select box within a "custom-select" DIV element.
Read more >
How to Create a Custom Select Dropdown using HTML, CSS ...
A post on how to create a custom select dropdown using HTML, CSS and JavaScript. Should you build it from scratch or use...
Read more >
Custom Select Styles with Pure CSS
A few properties and techniques our solution will use: clip-path to create the custom dropdown arrow; CSS grid layout to align the native...
Read more >
Awesome CSS Select Styles You Can Use Right Now
CSS select styles offer visitors a dropdown selection of options. ... This is a demo showing how to make a cross-browser custom-styled ...
Read more >
31 CSS Select Boxes - Free Frontend
Collection of free HTML and CSS custom select box code examples: dropdown, multiple, custom arrow, etc. Update of April 2019 collection.
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