Skip to content

Commit 1841ef1

Browse files
committed
feat(theme): Move indexSelector styling to default.css
BREAKING CHANGE: Classes are now named `ais-index-selector` and `ais-index-selector--item` to stay consistent with other widgets. Updated tests as well. Widget is responsible for adding default classes + user-defined ones. Then component simply add them to the markup.
1 parent 6b79703 commit 1841ef1

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed

README.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ you'll need several indices. This widget lets you easily change it.
408408
* @param {String} options.indices[0].name Name of the index to target
409409
* @param {String} options.indices[0].label Label displayed in the dropdown
410410
* @param {Object} [options.cssClasses] CSS classes to be added
411-
* @param {String} [options.cssClasses.select] CSS classes added to the parent <select>
412-
* @param {String} [options.cssClasses.option] CSS classes added to each <option>
411+
* @param {String} [options.cssClasses.root] CSS classes added to the parent <select>
412+
* @param {String} [options.cssClasses.item] CSS classes added to each <option>
413413
* @param {boolean} [hideWhenNoResults=false] Hide the container when no results match
414414
* @return {Object}
415415
*/
@@ -431,7 +431,7 @@ search.addWidget(
431431
{name: 'instant_search_price_desc', label: 'Highest price'}
432432
],
433433
cssClasses: {
434-
select: 'form-control'
434+
root: 'form-control'
435435
}
436436
})
437437
);
@@ -440,15 +440,18 @@ search.addWidget(
440440
#### Styling
441441

442442
```html
443-
<select class="ais-index-selector--select">
444-
<option class="ais-index-selector--option">Most relevant</option>
445-
<option class="ais-index-selector--option">Lowest price</option>
446-
<option class="ais-index-selector--option">Highest price</option>
443+
<select class="ais-index-selector">
444+
<option class="ais-index-selector--item">Most relevant</option>
445+
<option class="ais-index-selector--item">Lowest price</option>
446+
<option class="ais-index-selector--item">Highest price</option>
447447
</select>
448448
```
449449

450450
```css
451-
/* No default styling applied */
451+
.ais-index-selector {
452+
}
453+
.ais-index-selector--item {
454+
}
452455
```
453456

454457
### pagination

components/IndexSelector.js

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

3-
var bem = require('../lib/utils').bemHelper('ais-index-selector');
4-
var cx = require('classnames');
53

64
class IndexSelector extends React.Component {
75
handleChange(event) {
@@ -11,29 +9,32 @@ class IndexSelector extends React.Component {
119
render() {
1210
var {currentIndex, indices} = this.props;
1311

14-
var selectClass = cx(bem('select'), this.props.cssClasses.select);
15-
var optionClass = cx(bem('option'), this.props.cssClasses.option);
16-
1712
var handleChange = this.handleChange.bind(this);
1813

1914
return (
2015
<select
21-
className={selectClass}
16+
className={this.props.cssClasses.root}
2217
onChange={handleChange}
2318
value={currentIndex}
2419
>
25-
{indices.map(function(index) {
26-
return <option className={optionClass} key={index.name} value={index.name}>{index.label}</option>;
27-
})}
20+
{indices.map((index) => {
21+
return <option className={this.props.cssClasses.item} key={index.name} value={index.name}>{index.label}</option>;
22+
})}
2823
</select>
2924
);
3025
}
3126
}
3227

3328
IndexSelector.propTypes = {
3429
cssClasses: React.PropTypes.shape({
35-
select: React.PropTypes.string,
36-
option: React.PropTypes.string
30+
root: React.PropTypes.oneOfType([
31+
React.PropTypes.string,
32+
React.PropTypes.arrayOf(React.PropTypes.string)
33+
]),
34+
item: React.PropTypes.oneOfType([
35+
React.PropTypes.string,
36+
React.PropTypes.arrayOf(React.PropTypes.string)
37+
])
3738
}),
3839
currentIndex: React.PropTypes.string,
3940
indices: React.PropTypes.array,

components/__tests__/IndexSelector-test.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import expect from 'expect';
55
import TestUtils from 'react-addons-test-utils';
66
import IndexSelector from '../IndexSelector';
77

8-
var bem = require('../../lib/utils').bemHelper('ais-index-selector');
9-
var cx = require('classnames');
10-
118
describe('IndexSelector', () => {
129
var renderer;
1310

@@ -18,15 +15,21 @@ describe('IndexSelector', () => {
1815

1916

2017
it('should render <IndexSelector/>', () => {
21-
var out = render({currentIndex: 'index-a'});
18+
var out = render({
19+
currentIndex: 'index-a',
20+
cssClasses: {
21+
root: 'custom-root',
22+
item: 'custom-item'
23+
}
24+
});
2225
expect(out).toEqualJSX(
2326
<select
24-
className={cx(bem('select'))}
27+
className="custom-root"
2528
onChange={() => {}}
2629
value="index-a"
2730
>
28-
<option className={cx(bem('option'))} value="index-a">Index A</option>
29-
<option className={cx(bem('option'))} value="index-b">Index B</option>
31+
<option className="custom-item" value="index-a">Index A</option>
32+
<option className="custom-item" value="index-b">Index B</option>
3033
</select>
3134
);
3235
});

themes/default/default.css

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
/* STATS */
2828

2929
/* INDEX SELECTOR */
30+
.ais-index-selector {
31+
}
32+
.ais-index-selector--item {
33+
}
3034

3135
/* HITS */
3236

widgets/index-selector/__tests__/index-selector-test.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe('indexSelector()', () => {
1717
var ReactDOM;
1818
var container;
1919
var indices;
20+
var cssClasses;
2021
var widget;
2122
var props;
2223
var helper;
@@ -32,7 +33,11 @@ describe('indexSelector()', () => {
3233

3334
container = document.createElement('div');
3435
indices = ['index-a', 'index-b'];
35-
widget = indexSelector({container, indices});
36+
cssClasses = {
37+
root: 'custom-root',
38+
item: 'custom-item'
39+
};
40+
widget = indexSelector({container, indices, cssClasses});
3641
helper = {
3742
getIndex: sinon.stub().returns('index-a'),
3843
setIndex: sinon.spy(),
@@ -50,7 +55,10 @@ describe('indexSelector()', () => {
5055
it('calls ReactDOM.render(<IndexSelector props />, container)', () => {
5156
widget.render({helper, results});
5257
props = {
53-
cssClasses: {},
58+
cssClasses: {
59+
root: 'ais-index-selector custom-root',
60+
item: 'ais-index-selector--item custom-item'
61+
},
5462
currentIndex: 'index-a',
5563
hasResults: false,
5664
hideWhenNoResults: false,

widgets/index-selector/index-selector.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var ReactDOM = require('react-dom');
33

44
var findIndex = require('lodash/array/findIndex');
55
var utils = require('../../lib/utils.js');
6+
var bem = utils.bemHelper('ais-index-selector');
7+
var cx = require('classnames');
68
var autoHide = require('../../decorators/autoHide');
79

810
/**
@@ -12,8 +14,8 @@ var autoHide = require('../../decorators/autoHide');
1214
* @param {String} options.indices[0].name Name of the index to target
1315
* @param {String} options.indices[0].label Label displayed in the dropdown
1416
* @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>
17+
* @param {String} [options.cssClasses.root] CSS classes added to the parent <select>
18+
* @param {String} [options.cssClasses.item] CSS classes added to each <option>
1719
* @param {boolean} [hideWhenNoResults=false] Hide the container when no results match
1820
* @return {Object}
1921
*/
@@ -49,6 +51,11 @@ function indexSelector({
4951
let hasResults = results.hits.length > 0;
5052
let setIndex = this.setIndex.bind(this, helper);
5153
var IndexSelector = autoHide(require('../../components/IndexSelector'));
54+
55+
cssClasses = {
56+
root: cx(bem(null), cssClasses.root),
57+
item: cx(bem('item'), cssClasses.item)
58+
};
5259
ReactDOM.render(
5360
<IndexSelector
5461
cssClasses={cssClasses}

0 commit comments

Comments
 (0)