Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Semantic-UI / ReactJS | Use DropDown as group

I'm using Semantic-UI/ReactJS and want to use DropDown component as group but there is not such option, but I googled, and find a fiddle that provide this but in standalone semantic ui not for reactjs, how can I use dropdown as group?

<Dropdown
    id="Province"
    search selection clearable
    options={this.state.ProvinceData && this.state.ProvinceData.map(v => {
        return {
            key: v.id,
            text: v.name,
            value: v.id
        }
    })}
    onChange={this.getProvince}
    placeholder="-- Choose --"
    value={this.state.provinceValue}
    noResultsMessage={'Not Found!'}
/>
like image 525
tour travel Avatar asked Mar 08 '26 16:03

tour travel


1 Answers

Dropdown component with children can not have "selection" property and that is the problem. Although You can "hack" it. Note that this is not a "clean" solution, but visually makes difference.

Let's say your "options" array of objects is like this:

[
  {
    key: v.id,
    text: v.name,
    value: v.id
  },
  ...
]

You can create a simple React Component and style it as You wish:

const OptGroupHeader = props => {
  const { header } = props
    return (
      <div>
        <hr />
        <strong><i>{ header }</i></strong>
       <hr />
      </div>
    )
  }

Now what you can do is provide it as a disabled option where you want to use your header.

options.splice(x, 0, {
  key: 'key - xxx', 
  value: 'value', 
  text: (<OptGroupHeader header='My Fancy Header' />), 
  disabled: true
})

A component inside the "text" property will be rendered.

like image 72
Milan Babic Avatar answered Mar 11 '26 07:03

Milan Babic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!