Skip to content

Commit

Permalink
feat(react-search): Add a stateless SearchInput component.
Browse files Browse the repository at this point in the history
[#84034250]
  • Loading branch information
Paul Meskers authored and Paul Meskers committed Dec 18, 2014
1 parent 836594f commit 3103c38
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/pivotal-ui/components/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1251,3 +1251,23 @@ Add the focus-input directive to an element that should be focused if the focus-
```
*/

/*doc
---
title: React Search Inputs
name: form_search_input_react
categories:
- Beta
---
```html_example
<div id='react-search-example'></div>
```
```jsx_example
React.render(
<SearchInput placeholder="Search..."/>,
document.getElementById('react-search-example')
);
```
*/
27 changes: 27 additions & 0 deletions src/pivotal-ui/javascripts/inputs.jsx
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
};
2 changes: 2 additions & 0 deletions src/pivotal-ui/javascripts/pivotal-ui-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ global.DangerButton = require('./buttons.jsx').DangerButton;
global.HighlightButton = require('./buttons.jsx').HighlightButton;
global.HighlightAltButton = require('./buttons.jsx').HighlightAltButton;

global.SearchInput = require('./inputs.jsx').SearchInput;

global.Divider = require('./dividers.jsx').Divider;
global.DividerInverse = require('./dividers.jsx').DividerInverse;

Expand Down
81 changes: 81 additions & 0 deletions test/spec/javascripts/input_spec.js
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();
});
});
});

0 comments on commit 3103c38

Please sign in to comment.