Skip to content

Commit 0bfdfa4

Browse files
authored
Added an example on how to use <select multiple>
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 #531, #984.
1 parent 6e79d08 commit 0bfdfa4

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

content/docs/forms.md

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,58 @@ class FlavorForm extends React.Component {
182182

183183
Overall, this makes it so that `<input type="text">`, `<textarea>`, and `<select>` all work very similarly - they all accept a `value` attribute that you can use to implement a controlled component.
184184

185-
> Note
186-
>
187-
> You can pass an array into the `value` attribute, allowing you to select multiple options in a `select` tag:
188-
>
189-
>```js
190-
><select multiple={true} value={['B', 'C']}>
191-
>```
185+
### Selecting multiple options {#selecting-multiple-options}
186+
187+
You can pass an array into the `value` attribute, allowing you to select multiple options in a `select` tag:
188+
189+
```javascript{4,10-15,18,27-30}
190+
class FlavorForm extends React.Component {
191+
constructor(props) {
192+
super(props);
193+
this.state = { values: ['coconut'] };
194+
195+
this.handleChange = this.handleChange.bind(this);
196+
this.handleSubmit = this.handleSubmit.bind(this);
197+
}
198+
199+
handleChange(event) {
200+
const options = Array.from(event.target);
201+
const selectedOptions = options.filter(option => option.selected);
202+
const selectedValues = selectedOptions.map(option => option.value);
203+
this.setState({ values: selectedValues });
204+
}
205+
206+
handleSubmit(event) {
207+
alert('Your favorite flavors are: ' + this.state.values);
208+
event.preventDefault();
209+
}
210+
211+
render() {
212+
return (
213+
<form onSubmit={this.handleSubmit}>
214+
<label>
215+
Pick your favorite flavor:
216+
<select
217+
multiple={true}
218+
value={this.state.values}
219+
onChange={this.handleChange}
220+
>
221+
<option value="grapefruit">Grapefruit</option>
222+
<option value="lime">Lime</option>
223+
<option value="coconut">Coconut</option>
224+
<option value="mango">Mango</option>
225+
</select>
226+
</label>
227+
<input type="submit" value="Submit" />
228+
</form>
229+
);
230+
}
231+
}
232+
```
233+
234+
[**Try it on CodePen**](https://codepen.io/anon/pen/pmxJRd?editors=0011)
235+
236+
Notice that [Array.from()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from) is used to transform `event.target` into an array, so we can use the [filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) functions to obtain the values from the selected options.
192237

193238
## The file input Tag {#the-file-input-tag}
194239

0 commit comments

Comments
 (0)