-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react-search): Add a stateless SearchInput component.
[#84034250]
- Loading branch information
Paul Meskers
authored and
Paul Meskers
committed
Dec 18, 2014
1 parent
836594f
commit 3103c38
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
var React = require('react'); | ||
var _ = require('lodash'); | ||
|
||
var SearchInput = React.createClass({ | ||
render: function () { | ||
var inputClasses = ['form-control']; | ||
|
||
if (this.props.className) { | ||
inputClasses.push(this.props.className); | ||
} | ||
|
||
inputClasses = inputClasses.join(" "); | ||
|
||
return( | ||
<div className='form-group form-group-search'> | ||
<input {...this.props} type='text' className={inputClasses} placeholder={this.props.placeholder}/> | ||
<i className='fa fa-search' /> | ||
</div> | ||
); | ||
} | ||
}); | ||
|
||
module.exports = { | ||
SearchInput: SearchInput | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
'use strict'; | ||
|
||
var $ = require('jquery'); | ||
var React = require('react/addons'); | ||
|
||
var SearchInput = React.createFactory(require('../../../src/pivotal-ui/javascripts/inputs.jsx').SearchInput); | ||
var TestUtils = React.addons.TestUtils; | ||
|
||
describe('SearchInput', function() { | ||
beforeEach(function() { | ||
this.node = $('<div id="container"></div>').appendTo('body').get(0); | ||
|
||
React.render( | ||
SearchInput({}), | ||
this.node | ||
); | ||
}); | ||
|
||
afterEach(function() { | ||
React.unmountComponentAtNode(this.node); | ||
document.body.removeChild(this.node); | ||
}); | ||
|
||
it("renders a form group with the search classes", function() { | ||
expect($('#container .form-group')).toHaveClass('form-group-search'); | ||
|
||
expect($('#container .form-group input')).toHaveClass('form-control'); | ||
|
||
expect($('#container .form-group i')).toHaveClass('fa'); | ||
expect($('#container .form-group i')).toHaveClass('fa-search'); | ||
}); | ||
|
||
describe("when a placeholder is provided", function() { | ||
beforeEach(function() { | ||
React.render( | ||
SearchInput({ | ||
placeholder: 'Search here...' | ||
}), | ||
this.node | ||
); | ||
}); | ||
|
||
it("renders the input with a placeholder", function() { | ||
expect($('#container .form-group input').attr('placeholder')).toEqual('Search here...'); | ||
}); | ||
}); | ||
|
||
describe("when a className is provided", function() { | ||
beforeEach(function() { | ||
React.render( | ||
SearchInput({ | ||
className: 'foo myClass' | ||
}), | ||
this.node | ||
); | ||
}); | ||
|
||
it("adds the classes to the input", function() { | ||
expect($('#container .form-group input')).toHaveClass('foo'); | ||
expect($('#container .form-group input')).toHaveClass('myClass'); | ||
}); | ||
}); | ||
|
||
describe("when event handlers are provided", function() { | ||
beforeEach(function() { | ||
this.changeFn = jasmine.createSpy(); | ||
|
||
React.render( | ||
SearchInput({ | ||
onChange: this.changeFn | ||
}), | ||
this.node | ||
); | ||
}); | ||
|
||
it("adds the handlers to the search input", function() { | ||
TestUtils.Simulate.change($('#container input').get(0)); | ||
expect(this.changeFn).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |