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

Documentation for handling multiple options with <select> #531

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

nupurgrover
Copy link

@nupurgrover nupurgrover commented Jan 17, 2018

Closes #524

Screenshot:
image

@facebook-github-bot
Copy link
Collaborator

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@nupurgrover nupurgrover changed the title Added documentation for handling multiple options with <select>. Documentation for handling multiple options with <select> Jan 17, 2018
@reactjs-bot
Copy link

reactjs-bot commented Jan 17, 2018

Deploy preview for reactjs ready!

Built with commit cf3fcd6

https://deploy-preview-531--reactjs.netlify.com

@facebook-github-bot
Copy link
Collaborator

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

The `handleChange` method from the above example would then change to get the selected values from `event.target.selectedOptions`.

```javascript{4,10-12,24}
handleChange(event) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is inconsistent for the rest of this page, which is 2 spaces for both HTML and JS. Do fix your markup accordingly (not sure if Prettier does that for you).

Also, you can simplify handleChange to:

const selectedFlavors = Array.from(event.target.selectedOptions).map(option => option.value);
this.setState({value: selectedFlavors});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yangshun Done. I was not sure if using arrow functions was OK for the docs or not, that is why decided to write it using function

The `<select>` inside the render would also change to pass an array to the `value` attribute:

```javascript{4,10-12,24}
<select value={this.state.value} onChange={this.handleChange} multiple={true}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 spaces indentation.

@nupurgrover
Copy link
Author

nupurgrover commented Jan 25, 2018

@yangshun I made changes to correct the indentation. Do let me know if there's anything else I need to change, thanks! :)

@yangshun
Copy link
Contributor

I'm not from the React core team. Just a random commenter passing by 😄

@gdennie
Copy link

gdennie commented Sep 26, 2018

The documentation has not been revised. Spent a while reinventing this solution. :(

@nupurgrover
Copy link
Author

I actually forgot I wrote this. Have just updated the branch. Will comment on the original issue and see if anyone from the core team would be willing to merge.

@gaearon
Copy link
Member

gaearon commented Sep 26, 2018

Sorry — we're a bit overwhelmed and haven't been looking at docs PRs closely.

```

[Try it on CodePen.](https://codepen.io/nupgrover/pen/RxeqLQ?editors=0010)

> Note
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note below now looks confusing because we've just discussed it. Maybe move it above, and then expand on how to change it dynamically?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I'll make the change

@gaearon
Copy link
Member

gaearon commented Sep 26, 2018

Seems like selectedOptions isn't supported in IE? Could be confusing.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions

Any other ideas?

@nupurgrover
Copy link
Author

@gaearon Oh, completely missed that! No, I don't have any ideas (except mentioning it explicitly in the docs) but I'll try and find a workaround and update this PR.

0xnoob added a commit to 0xnoob/reactjs.org that referenced this pull request May 29, 2019
The short info segment that is currently provided is insufficient to use an `<select multiple={true}>` element.  

This is especially confusing and frustraring with the sentence **"this makes it so that `<input type="text">`, `<textarea>`, and `<select>` all work very similarly"** right before it: In the case of using the `multiple` attribute, you *do* need to change your code a little bit more.

The linked anonymous codepen is from me, feel free to copy/paste/modify it as you like; afaik all codepen example are owned by @gaearon?  

There is an [alternative codepen example](https://codepen.io/anon/pen/VOEezB?editors=0011) which also deals with writing a more suitable alert message, depending on how many options are selected, but in my opinion it adds unnecessary noice.

This issue has also been addressed in reactjs#531, reactjs#984.
BetterZxx pushed a commit to BetterZxx/react.dev that referenced this pull request Mar 21, 2023
Bumps [url-parse](https://github.com/unshiftio/url-parse) from 1.4.7 to 1.5.1.
- [Release notes](https://github.com/unshiftio/url-parse/releases)
- [Commits](unshiftio/url-parse@1.4.7...1.5.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants