Skip to content

Commit 9e0772c

Browse files
committed
Merge pull request react-bootstrap#336 from heydiplo/multiple_select
getValue() should return all selected options for multiple select
2 parents a72f724 + c9a1217 commit 9e0772c

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

docs/src/ComponentsPage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ var ComponentsPage = React.createClass({
517517
The helper method <code>getInputDOMNode()</code> returns the internal input element.</p>
518518
<ReactPlayground codeText={fs.readFileSync(__dirname + '/../examples/Input.js', 'utf8')} />
519519
<h2 id="input-types">Types</h2>
520-
<p>Supports <code>select</code>, <code>textarea</code>, <code>static</code> as well as standard HTML input types.</p>
520+
<p>Supports <code>select</code>, <code>textarea</code>, <code>static</code> as well as standard HTML input types. <code>getValue()</code> returns an array for multiple select.</p>
521521
<ReactPlayground codeText={fs.readFileSync(__dirname + '/../examples/InputTypes.js', 'utf8')} />
522522
<h2 id="input-addons">Add-ons</h2>
523523
<p>Use <code>addonBefore</code> and <code>addonAfter</code> for normal addons, <code>buttonBefore</code> and <code>buttonAfter</code> for button addons.

src/Input.jsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ var Input = React.createClass({
3737
return this.props.value;
3838
}
3939
else if (this.props.type) {
40-
return this.getInputDOMNode().value;
40+
if (this.props.type == "select" && this.props.multiple) {
41+
return this.getSelectedOptions();
42+
} else {
43+
return this.getInputDOMNode().value;
44+
}
4145
}
4246
else {
4347
throw Error('Cannot use getValue without specifying input type.');
@@ -48,6 +52,23 @@ var Input = React.createClass({
4852
return this.getInputDOMNode().checked;
4953
},
5054

55+
getSelectedOptions: function () {
56+
var values = [];
57+
58+
Array.prototype.forEach.call(
59+
this.getInputDOMNode().getElementsByTagName('option'),
60+
function (option) {
61+
if (option.selected) {
62+
var value = option.getAttribute('value') || option.innerHTML;
63+
64+
values.push(value);
65+
}
66+
}
67+
);
68+
69+
return values;
70+
},
71+
5172
isCheckboxOrRadio: function () {
5273
return this.props.type === 'radio' || this.props.type === 'checkbox';
5374
},

test/InputSpec.jsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,30 @@ describe('Input', function () {
214214
assert.equal(instance.getChecked(), true);
215215
});
216216

217+
it('returns the only selected option for select', function () {
218+
var instance = ReactTestUtils.renderIntoDocument(
219+
<Input type="select" value={'one'}>
220+
<option value="one">one</option>
221+
<option value="two">two</option>
222+
<option value="three">three</option>
223+
</Input>
224+
);
225+
226+
assert.equal(instance.getValue(), 'one');
227+
})
228+
229+
it('returns all selected options for multiple select', function () {
230+
var instance = ReactTestUtils.renderIntoDocument(
231+
<Input type="select" multiple value={['one','two']}>
232+
<option value="one">one</option>
233+
<option value="two">two</option>
234+
<option value="three">three</option>
235+
</Input>
236+
);
237+
238+
assert.deepEqual(instance.getValue(), ['one', 'two']);
239+
})
240+
217241
it('renders a disabled input correctly', function () {
218242
var instance = ReactTestUtils.renderIntoDocument(
219243
<Input type="text" disabled={true} />

0 commit comments

Comments
 (0)