Skip to content

Commit 564da51

Browse files
committed
feat(bem): Add BEM to the index-selector widget
This one does not have any default styles (so no `css` file with it). I still allow the styling of individual `option`, even if I'm unsure this is even useful. BREAKING CHANGE: We now use `cssClasses.select` and `cssClasses.option` instead of `cssClass` for the index-selector widget.
1 parent 6b42e34 commit 564da51

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ you'll need several indices. This widget lets you easily change it.
326326
* @param {Array} options.indices Array of objects defining the different indices to choose from.
327327
* @param {String} options.indices[0].name Name of the index to target
328328
* @param {String} options.indices[0].label Label displayed in the dropdown
329-
* @param {String|String[]} [options.cssClass] Class name(s) to be added to the generated select element
329+
* @param {Object} [options.cssClasses] CSS classes to be added
330+
* @param {String} [options.cssClasses.select] CSS classes added to the parent <select>
331+
* @param {String} [options.cssClasses.option] CSS classes added to each <option>
330332
* @param {boolean} [hideWhenNoResults=false] Hide the container when no results match
331333
* @return {Object}
332334
*/
@@ -347,7 +349,9 @@ search.addWidget(
347349
{name: 'instant_search_price_asc', label: 'Lowest price'},
348350
{name: 'instant_search_price_desc', label: 'Highest price'}
349351
],
350-
cssClass: 'form-control'
352+
cssClasses: {
353+
select: 'form-control'
354+
}
351355
})
352356
);
353357
```

components/IndexSelector.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
11
var React = require('react');
22

3+
var bem = require('../lib/utils').bemHelper('ais-index-selector');
4+
var cx = require('classnames');
5+
36
class IndexSelector extends React.Component {
47
handleChange(event) {
5-
this.props.setIndex(event.target.value).search();
8+
this.props.setIndex(event.target.value);
69
}
710

811
render() {
9-
var currentIndex = this.props.currentIndex;
10-
var indices = this.props.indices;
11-
var cssClass = this.props.cssClass;
12-
var selectId = this.props.containerId + '-select';
12+
var {currentIndex, indices} = this.props;
13+
14+
var selectClass = cx(bem('select'), this.props.cssClasses.select);
15+
var optionClass = cx(bem('option'), this.props.cssClasses.option);
16+
17+
var handleChange = this.handleChange.bind(this);
1318

1419
return (
1520
<select
16-
id={selectId}
17-
className={cssClass}
18-
onChange={this.handleChange.bind(this)}
21+
className={selectClass}
22+
onChange={handleChange}
1923
value={currentIndex}
2024
>
2125
{indices.map(function(index) {
22-
return <option key={index.name} value={index.name}>{index.label}</option>;
26+
return <option className={optionClass} key={index.name} value={index.name}>{index.label}</option>;
2327
})}
2428
</select>
2529
);
2630
}
2731
}
2832

2933
IndexSelector.propTypes = {
30-
containerId: React.PropTypes.string,
31-
cssClass: React.PropTypes.string,
34+
cssClasses: React.PropTypes.shape({
35+
select: React.PropTypes.string,
36+
option: React.PropTypes.string
37+
}),
3238
currentIndex: React.PropTypes.string,
3339
indices: React.PropTypes.array,
34-
setIndex: React.PropTypes.func
40+
setIndex: React.PropTypes.func.isRequired
3541
};
3642

3743
module.exports = IndexSelector;

example/app.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ search.addWidget(
3535
{name: 'instant_search_price_asc', label: 'Lowest price'},
3636
{name: 'instant_search_price_desc', label: 'Highest price'}
3737
],
38-
cssClass: 'form-control'
38+
cssClasses: {
39+
select: 'form-control'
40+
}
3941
})
4042
);
4143

widgets/index-selector.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,22 @@ var IndexSelector = autoHide(require('../components/IndexSelector'));
1111
* @param {Array} options.indices Array of objects defining the different indices to choose from.
1212
* @param {String} options.indices[0].name Name of the index to target
1313
* @param {String} options.indices[0].label Label displayed in the dropdown
14-
* @param {String|String[]} [options.cssClass] Class name(s) to be added to the generated select element
14+
* @param {Object} [options.cssClasses] CSS classes to be added
15+
* @param {String} [options.cssClasses.select] CSS classes added to the parent <select>
16+
* @param {String} [options.cssClasses.option] CSS classes added to each <option>
1517
* @param {boolean} [hideWhenNoResults=false] Hide the container when no results match
1618
* @return {Object}
1719
*/
1820
function indexSelector({
19-
container = null,
20-
indices = null,
21-
cssClass,
21+
container,
22+
indices,
23+
cssClasses = {},
2224
hideWhenNoResults = false
2325
}) {
2426
var containerNode = utils.getContainerNode(container);
2527

26-
var usage = 'Usage: indexSelector({container, indices[, cssClass]})';
27-
if (container === null || indices === null) {
28+
var usage = 'Usage: indexSelector({container, indices[, cssClasses.{select,option}, hideWhenNoResults]})';
29+
if (!container || !indices) {
2830
throw new Error(usage);
2931
}
3032

@@ -37,17 +39,22 @@ function indexSelector({
3739
}
3840
},
3941

42+
setIndex: function(helper, indexName) {
43+
helper.setIndex(indexName).search();
44+
},
45+
4046
render: function({helper, results}) {
41-
var containerId = containerNode.id;
47+
let currentIndex = helper.getIndex();
48+
let hasResults = results.hits.length > 0;
49+
let setIndex = this.setIndex.bind(this, helper);
4250
React.render(
4351
<IndexSelector
44-
containerId={containerId}
45-
cssClass={cssClass}
46-
currentIndex={helper.getIndex()}
47-
indices={indices}
52+
cssClasses={cssClasses}
53+
currentIndex={currentIndex}
54+
hasResults={hasResults}
4855
hideWhenNoResults={hideWhenNoResults}
49-
hasResults={results.hits.length > 0}
50-
setIndex={helper.setIndex.bind(helper)}
56+
indices={indices}
57+
setIndex={setIndex}
5158
/>,
5259
containerNode
5360
);

0 commit comments

Comments
 (0)