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

Dropdown: automatically highlights the first element #3130

Open
Brqqq opened this issue Sep 5, 2018 · 15 comments · May be fixed by #3814
Open

Dropdown: automatically highlights the first element #3130

Brqqq opened this issue Sep 5, 2018 · 15 comments · May be fixed by #3814
Labels

Comments

@Brqqq
Copy link

Brqqq commented Sep 5, 2018

Bug Report

Steps

  1. Create a dropdown with static data inside of it, with 2 or more items to illustrate it better
  2. Open the dropdown by clicking on it.

Expected Result

None of the options should be highlighted, until you hover over it with your mouse or use your "up" or "down" arrow keys.

Currently the non-React version of Semantic UI already behaves like this.

Actual Result

The first item is highlighted. So it acts as if you have your mouse hovered over this first item.

Version

0.82.3

Testcase

This issue can be seen on the doc page demos. A specific example of a simple dropdown where this issue can be seen:
https://codesandbox.io/s/218np7y82j

Compare this to the non-React version where this issue is NOT present:
https://jsfiddle.net/7adbsm6r/5/

I have assumed it's a bug, because of this behaviour is different than the non-React version. The non-React version behaves in the way that I described in the "expected result".

Here's an image showing the difference. This is an uncontrolled dropdown and when you first open it, without hovering your mouse over any option

Comparison between the react and non-react version of semantic

@welcome
Copy link

welcome bot commented Sep 5, 2018

👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you've completed all the fields in the issue template so we can best help.

We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

@fifthsegment
Copy link

fifthsegment commented Sep 6, 2018

I think this is the author's intended behaviour, because there's a test case in the Dropdown-test.js file which specifically tests if the first item is selected in the case that no value or default value is provided. But you're right, the first value shouldn't be selected by default.

describe('selected item', () => {
    it('defaults to the first item', () => {
      wrapperShallow(<Dropdown options={options} selection />)
        .find('DropdownItem')
        .first()
        .should.have.prop('selected', true)

@yuritoledo
Copy link

yuritoledo commented Oct 3, 2018

hey @Brqqq , try to add this props on Dropdown, setting it to false

image

@Brqqq
Copy link
Author

Brqqq commented Oct 4, 2018

Thanks for the suggestion @yuritoledo . Unfortunately it doesn't work. Those props concern whether a value is picked onblur/navigation, but not if an entry is highlighted when you first open the dropdown.

@yuritoledo
Copy link

Nop. I guess I saw a behaviour when have the multiple props, but its to use only when is multiple :/

@Gerson4G
Copy link

Greetings. Any progress on this? Or as @fifthsegment says, is expected behavior so it won't be "fixed"? Thank you.

@willbryk720
Copy link

willbryk720 commented Jan 8, 2019

Try selectOnBlur={false}
Found in #811
It works with Form.Select at least

@Brqqq
Copy link
Author

Brqqq commented Jan 8, 2019

@willbryk720 It was suggested in the replies here too. It does not work.

Note what is said here. The issue is that "selected" is set to the first option when you open the dropdown. So you open the dropdown and you see the first option highlighted. I was looking for a way to not have the first option highlighted, like in the normal semantic ui. However, this behavior might be intended as mentioned in one of the previous posts.

selectOnBlur means you can close the dropdown and it will or won't actually select the highlighted value. I'm only looking to lose the highlight off the first option when you open the dropdown with no value selected

@Brqqq Brqqq changed the title Dropdown: automatically selects the first element Dropdown: automatically highlights the first element Jan 8, 2019
@Brqqq
Copy link
Author

Brqqq commented Jan 8, 2019

I think I didn't describe the issue very well, so I wrote it differently. The clearest way to see the issue is to check the react and non-react versions side by side and see how the dropdown looks when you first open it.

@aaronetto
Copy link

aaronetto commented Jan 17, 2019

You can fix this problem setting the prop selected in your options like

import React from 'react'
import { Dropdown } from 'semantic-ui-react'

const options = [
  { key: 1, text: 'Choice 1', value: 1, selected: false },
  { key: 2, text: 'Choice 2', value: 2 },
  { key: 3, text: 'Choice 3', value: 3 },
]

const DropdownExample = () => <Dropdown selection options={options} />

export default DropdownExample

:)

@Brqqq
Copy link
Author

Brqqq commented Jan 18, 2019

Thanks, that does get around the issue!

Ideally the first value would not be shown as selected when you open it (in the image, the non-React version shows a grey "Options" and the React version shows a black "option 1") but this is already better.

@CarlosReyesM
Copy link

CarlosReyesM commented Jul 15, 2019

I have notice that hard writing the value selected to false actually make it not possible the arrow navigation to the items with this value hard written, have we find any other option to change this?

@steinwachs
Copy link

steinwachs commented Sep 12, 2019

My project is on version 0.88.1 of semantic-ui-react.

I also ran into this issue. I do not want the value to change on open either. The value should only be changed when the user actively clicks on one of the items in the drop-down.

Setting selectOnBlur={ false } alone did not work with the Dropdown component. Further, as @CarlosReyesM mentioned, setting selected to false leads to another issue with the arrow navigation.

The following workaround did the trick in my case, using the Dropdown component and a combination of selectOnBlur={ false } and resetting the selection onOpen.

  onOpen={ () => { !this.state.value && this.setState({ value: undefined }) }}
  selectOnBlur={ false }

Entire Dropdown example:

<Dropdown
  placeholder='Placeholder'
  options={ options }
  value={ value }
  onOpen={ () => { !this.state.value && this.setState({ value: undefined }) }}
  selectOnBlur={ false }
  floating
  button
  className='icon'
/>

Note the !this.state.value && ... bit in onOpen. This is needed because otherwise the value is reset even if one had been selected by the user. In the latter case, you don't want to reset the current value. I only want the value to be reset if no value had been selected yet, thus overriding the automatic selection by the component on open.

I forked @Brqqq's sandbox and made a new one that also contains the Dashboard component:
https://codesandbox.io/embed/semantic-ui-react-form-and-dropdown-yrjex

@dennnis0204
Copy link

My solution to this issue, placeholder property must be there for styling.

import _ from 'lodash';

const [option, setOption] = useState({});

<Dropdown
  onChange={(e, { value }) => {
    setOption(_.find(options, { value }));
  }}
  selectOnBlur={false}
  options={options}
  placeholder="Choose an option"
  selection
  text={option.text || 'Choose an option'}
  value={option.value}
/>;

@geoadamo
Copy link

One possible work around would be to add a dummy option as the first element in your options array and set it to display none in your css

const options = [
 { key: 'dummy-option', text: '', value: '' },
  { key: 1, text: 'Choice 1', value: 1 },
  { key: 2, text: 'Choice 2', value: 2 },
  { key: 3, text: 'Choice 3', value: 3 },
]

//in your css
#dummy-option{
display:none
}



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants