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

feat: more searchBox work #27

Merged
merged 9 commits into from
Aug 4, 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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ npm test # test and lint
npm run test:watch # developer mode, test only
npm run test:coverage
```

## Available widgets

### searchBox

```html
<div id="search-box"></div>
```

```js
instant.addWidget(
instantsearch.widgets.searchBox({
container: '#search-box',
placeholder: 'Search for products',
// cssClass: 'form-control'
})
);
```
23 changes: 9 additions & 14 deletions components/SearchBox/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
'use strict';

var React = require('react');
var bem = require('../BemHelper')('algolia-magic-search-box');
var bem = require('../BemHelper')('as-search-box');
var cx = require('classnames');

var AlgoliasearchHelper = require('algoliasearch-helper/src/algoliasearch.helper');
var SearchResults = require('algoliasearch-helper/src/SearchResults');

var SearchBox = React.createClass({
propTypes: {
helper: React.PropTypes.instanceOf(AlgoliasearchHelper),
results: React.PropTypes.instanceOf(SearchResults),
onFocus: React.PropTypes.func,
placeholder: React.PropTypes.string,
inputClass: React.PropTypes.string
inputClass: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.array
]),
setQuery: React.PropTypes.func,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I removed onFocus without any specific commit. This was done to clean the widget API before we decide we do need an onFocus prop and expose it in the widget.

search: React.PropTypes.func
},
render: function() {
var onFocus = this.props.onFocus;
var classNames = cx(bem('input'), this.props.inputClass);

return (
Expand All @@ -28,15 +26,12 @@ var SearchBox = React.createClass({
autoComplete="off"
autoFocus="autofocus"
onChange={this.change}
onFocus={onFocus}
role="textbox" />
);
},
change: function(e) {
var value = e.target.value;
var helper = this.props.helper;

helper.setQuery(value).search();
this.props.setQuery(e.target.value);
this.props.search();
}
});

Expand Down
25 changes: 15 additions & 10 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ var instant = new instantsearch.InstantSearch(
'bestbuy'
);

instant.addWidget(instantsearch.widgets.searchbox({
container: '#search-box',
placeholder: 'Search for products in France...'
}));
instant.addWidget(
instantsearch.widgets.searchBox({
container: '#search-box',
placeholder: 'Search for products',
cssClass: 'form-control'
})
);

instant.addWidget(instantsearch.widgets.results({
instant.addWidget(
instantsearch.widgets.results({
container: '#hits',
templates: {
noResults: require('./templates/no-results.html'),
hit: require('./templates/hit.html')
}
}));
templates: {
noResults: require('./templates/no-results.html'),
hit: require('./templates/hit.html')
}
})
);

instant.start();
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = {
InstantSearch: require('./lib/InstantSearch'),
widgets: {
searchbox: require('./widgets/searchbox'),
results: require('./widgets/hits')
searchBox: require('./widgets/search-box/'),
results: require('./widgets/hits/')
}
};
25 changes: 25 additions & 0 deletions widgets/search-box/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

var React = require('react');

var utils = require('../../lib/widgetUtils');
var bind = require('lodash/function/bind');

function searchbox(params) {
var SearchBox = require('../../components/SearchBox');
var container = utils.getContainerNode(params.container);

return {
init: function(initialState, helper) {
React.render(
<SearchBox
setQuery={bind(helper.setQuery, helper)}
search={bind(helper.search, helper)}
placeholder={params.placeholder}
inputClass={params.cssClass} />, container
);
}
};
}

module.exports = searchbox;
18 changes: 0 additions & 18 deletions widgets/searchbox/index.js

This file was deleted.