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

Adding onInputChange handler that returns the current input value. #425

Merged
merged 3 commits into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var Select = React.createClass({
onBlur: React.PropTypes.func, // onBlur handler: function(event) {}
onChange: React.PropTypes.func, // onChange handler: function(newValue) {}
onFocus: React.PropTypes.func, // onFocus handler: function(event) {}
onInputChange: React.PropTypes.func, // onInputChange handler: function(inputValue) {}
onOptionLabelClick: React.PropTypes.func, // onCLick handler for value labels: function (value, event) {}
optionComponent: React.PropTypes.func, // option component to render in dropdown
optionRenderer: React.PropTypes.func, // optionRenderer: function(option) {}
Expand Down Expand Up @@ -77,6 +78,7 @@ var Select = React.createClass({
newOptionCreator: undefined,
noResultsText: 'No results found',
onChange: undefined,
onInputChange: undefined,
onOptionLabelClick: undefined,
optionComponent: Option,
options: undefined,
Expand Down Expand Up @@ -486,6 +488,10 @@ var Select = React.createClass({
// the latest value before setState() has completed.
this._optionsFilterString = event.target.value;

if (this.props.onInputChange) {
this.props.onInputChange(event.target.value);
}

if (this.props.asyncOptions) {
this.setState({
isLoading: true,
Expand Down
16 changes: 15 additions & 1 deletion test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ class PropsWrapper extends React.Component {
}

describe('Select', function() {
var options, onChange;
var options, onChange, onInputChange;
var instance, wrapper;
var searchInputNode;

function getSelectControl(instance) {
return React.findDOMNode(instance).querySelector('.Select-control');
}

function enterSingleCharacter() {
TestUtils.Simulate.keyDown(searchInputNode, { keyCode: 65, key: 'a' });
}

function pressEnterToAccept() {
TestUtils.Simulate.keyDown(searchInputNode, { keyCode: 13, key: 'Enter' });
}
Expand Down Expand Up @@ -107,10 +111,12 @@ describe('Select', function() {
var createControl = function(props) {

onChange = sinon.spy();
onInputChange = sinon.spy();
// Render an instance of the component
instance = TestUtils.renderIntoDocument(
<Select
onChange={onChange}
onInputChange={onInputChange}
{...props}
/>
);
Expand All @@ -122,11 +128,13 @@ describe('Select', function() {

var createControlWithWrapper = function (props) {
onChange = sinon.spy();
onInputChange = sinon.spy();

wrapper = TestUtils.renderIntoDocument(
<PropsWrapper
childComponent={Select}
onChange={onChange}
onInputChange={onInputChange}
{...props}
/>
);
Expand Down Expand Up @@ -193,6 +201,12 @@ describe('Select', function() {
expect(node, 'queried for', '.Select-option', 'to have length', 2);
});

it('should pass input value when entering text', function () {
typeSearchText('a');
enterSingleCharacter('a');
expect(onInputChange, 'was called with', 'a');
});

it('should filter case insensitively', function () {

typeSearchText('t');
Expand Down