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: add forceSelection prop #2299

Closed
darewreck54 opened this issue Nov 8, 2017 · 13 comments
Closed

Dropdown: add forceSelection prop #2299

darewreck54 opened this issue Nov 8, 2017 · 13 comments

Comments

@darewreck54
Copy link

I'm not sure if this was by design, so I'm posted this question. Sorry, if i posted this in the wrong section.

Steps

  1. https://codepen.io/anon/pen/eeBYKy?editors=1111
  2. Notice by default, you see the placeholder for the dropdown
  3. click on the drop down and don't select any item. Click away from the drop down. This will auto populated the first item in the drop down list. OnChange Event that is triggered, it doesn't give you any context if a specific item in the list is selected vs. a user clicking anywhere on the screen

Expected Result

  1. User clicks away and does not select an item in the drop down. Expect the placeholder to still be displayed.

Actual Result

  1. Always selects the first item in the dropdown list

Version

0.76.0

Testcase

https://codepen.io/anon/pen/eeBYKy?editors=1111

If this is by design, is there anyway around this? Goal: if user accidently clicks away and not on a list item, I want nothing to be selected. The way i determine if something is selected is onChange and examining the data.value field. This value is always set to the first element on the list even if you don't select the item.

Thanks,
Derek

@layershifter layershifter changed the title Dropdown auto selects even when you don't pick an item Dropdown: add forceSelection prop Nov 8, 2017
@layershifter
Copy link
Member

SUI has an options for this, it's called forceSelection and defaults to true. We should also impliment it. Contributions are welcome.

@darewreck54
Copy link
Author

@layershifter I'll take a stab at it. I think i have the solution, but I'm not sure how to debug the unit test. I see that some of the test are failing. How do you debug the unit test? Currently i'm using vscode, but not sure how to configure or how the test infrastructure is setup for the project.

@darewreck54
Copy link
Author

@layershifter Here is the PR i created. #2308

I still need to add test and verify they all work (having a little trouble getting started with the test infastructure). Please take a look and let me if I took the right approach.

@jjjkkklll
Copy link

I would like to add, that when clicking outside of the DD, onChange is triggered. But there is no change happening!? So I think it shouldn't be triggered? Or if it is triggered, then we need a notification if clicked outside or on an item.

@darewreck54
Copy link
Author

darewreck54 commented Nov 15, 2017 via email

@jjjkkklll
Copy link

I agree, but still, this feels like a workaround. Because the word "onChange" suggests that it is triggered by an actual change of the DD. This is absolutely not critical, but still maybe worth a look while reviewing this part of the code. On the other hand, I am not sure if there is a trigger for clicking outside, which would have to be implemented then. Changing the docs is probably the more easy way.

@darewreck54
Copy link
Author

@layershifter can onChange be removed from being triggered? Wil lthat break anything else?

@darewreck54
Copy link
Author

@jjjkkklll @layershifter check out https://github.com/darewreck54/Semantic-UI-React/blob/07009b052888c7b854faef6aebf3cc49a50a03a5/src/modules/Dropdown/Dropdown.js#L589

You will notice that the code trigger the setSelectetIndex and right after calls handleChange. handleChange only takes in the new value. There is a comment that you cannot rely on props.value because its controlled component. So no real comparison is made. I'm guessing the the caller has to be responsible to determine if handleChange needs to be made or not based on diffing the old vs. new value. You could also put the logic into handleChange, but then you would have to pass in a new and old value. Not sure what controller component refers to and how props value would not be accurate.

@layershifter
Copy link
Member

I've tested proposed changes in #2308. BTW, @darewreck54 thanks for your activity in this question.

My idea that we don't need to touch something in current bevahiour of onChange, especially in #2308. There are numerous problems and RPCs that affects Dropdown, we will solve them systematically. After solving issues with refs and transitions, I will do serious refactoring there, 1.4K LOC make Dropdown very compilicated and untestable.

@levithomason
Copy link
Member

Per the expected result:

User clicks away and does not select an item in the drop down. Expect the placeholder to still be displayed.

We have the selectOnBlur prop, which when set to false will not select the item when the user blurs (clicks away) from the Dropdown.

https://codepen.io/levithomason/pen/MOBKpj

http://g.recordit.co/Md1y0TxFPX.gif

Sadly, it looks like we're missing an example for this in the docs. PRs welcome there, too.

I'll close this since that prop solves the OP issue. Will reopen if I've misunderstood or overlooked another aspect of this.

@darewreck54
Copy link
Author

@levithomason Interesting.....does that mean that the added property forceSelection I added in the PR does the same thing or that it was just a side effect of selectOnBlur feature. Or is selectOnBlur and forceSelection the same thing?

@levithomason
Copy link
Member

@darewreck54 I believe they are accomplishing the same thing. Seems it was reported in #2308 (comment) to also work for the use case.

LMK if your use case isn't handled by selectOnBlur and we can reopen this.

@ghost
Copy link

ghost commented Dec 11, 2017

For me selectOnBlur is the way.

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

No branches or pull requests

4 participants