Skip to content

Commit db8bd60

Browse files
committed
feat(searchbox): Make the searchBox BEMish
I've also updated the `poweredBy` as well. I've changed the logic of adding inline `style` to using a specific class (defined in the default `css` file). I've switched the display/hide logic. We no longer need to define a `display=true` in the props to show it, but a `hidden=true` to hide it instead. BREAKING CHANGE: The `searchBox` widget now expect a `cssClasses.{input, poweredBy}`
1 parent da1bf86 commit db8bd60

File tree

6 files changed

+92
-51
lines changed

6 files changed

+92
-51
lines changed

README.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,36 @@ search.addWidget(
250250
);
251251
```
252252

253+
#### Styling
254+
255+
```html
256+
<input class="ais-search-box--input">
257+
<!-- With poweredBy: true -->
258+
<div class="ais-search-box--powered-by ais-powered-by">
259+
Powered by
260+
<a class="ais-powered-by--link">
261+
<img src="ais-powered-by--image" />
262+
</a>
263+
</div>
264+
```
265+
266+
```css
267+
/* With poweredBy; true */
268+
.ais-powered-by {
269+
display: block;
270+
font-size: 12px;
271+
line-height: 18px;
272+
text-align: right;
273+
}
274+
.ais-powered-by--image {
275+
height: 16px;
276+
margin-bottom: 2px;
277+
}
278+
```
253279
### stats
254280

281+
![Example of the stats widget][stats]
282+
255283
#### API
256284

257285
```js
@@ -276,9 +304,7 @@ search.addWidget(
276304
*/
277305
```
278306

279-
#### Usage
280307

281-
![Example of the stats widget][stats]
282308

283309
#### Usage
284310

components/PoweredBy/PoweredBy.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var React = require('react');
2+
var bem = require('../../lib/utils').bemHelper('ais-powered-by');
3+
var cx = require('classnames');
4+
var logo = require('url?limit=10000!./algolia_logo.png');
5+
6+
require('style?prepend!raw!./powered-by.css');
7+
8+
class PoweredBy extends React.Component {
9+
render() {
10+
var cssClasses = {
11+
root: cx(this.props.className, bem(null)),
12+
link: bem('link'),
13+
image: bem('image')
14+
};
15+
16+
return (
17+
<div className={cssClasses.root}>
18+
Powered by
19+
<a className={cssClasses.link} href="https://www.algolia.com/">
20+
<img className={cssClasses.image} src={logo} />
21+
</a>
22+
</div>
23+
);
24+
}
25+
}
26+
27+
PoweredBy.propTypes = {
28+
className: React.PropTypes.string
29+
};
30+
31+
module.exports = PoweredBy;

components/PoweredBy/index.css

-10
This file was deleted.

components/PoweredBy/index.js

-28
This file was deleted.

components/PoweredBy/powered-by.css

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.ais-powered-by {
2+
font-size: 0.8em;
3+
text-align: right;
4+
}
5+
.ais-powered-by--image {
6+
height: 16px;
7+
margin-bottom: 2px;
8+
}

widgets/search-box.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
var utils = require('../lib/utils.js');
22
var forEach = require('lodash/collection/forEach');
3+
var bem = require('../lib/utils').bemHelper('ais-search-box');
4+
var cx = require('classnames');
35

46
/**
57
* Instantiate a searchbox
68
* @param {String|DOMElement} options.container CSS Selector or DOMElement to insert the widget
7-
* @param {String} [options.placeholder='Search here'] Input's placeholder
8-
* @param {Object} [options.cssClass] CSS classes to add to the input
9+
* @param {String} [options.placeholder] Input's placeholder
10+
* @param {Object} [options.cssClasses] CSS classes to add
11+
* @param {String} [options.cssClasses.input] CSS class to add to the input
12+
* @param {String} [options.cssClasses.poweredBy] CSS class to add to the poweredBy element
913
* @param {boolean} [poweredBy=false] Show a powered by Algolia link below the input
1014
* @param {boolean|string} [autofocus='auto'] autofocus on the input
1115
* @return {Object}
1216
*/
13-
function searchbox({
17+
function searchBox({
1418
container,
1519
placeholder,
16-
cssClass,
20+
cssClasses = {},
1721
poweredBy = false,
1822
autofocus = 'auto'
1923
}) {
20-
// Hook on an existing input, or add one if none targeted
2124
var input = utils.getContainerNode(container);
25+
26+
if (!input) {
27+
throw new Error('Usage: searchBox({container[, placeholder, cssClasses.{input,poweredBy}, poweredBy, autofocus]})');
28+
}
29+
30+
// Hook on an existing input, or add one if none targeted
2231
if (input.tagName !== 'INPUT') {
2332
input = input.appendChild(document.createElement('input'));
2433
}
@@ -34,7 +43,6 @@ function searchbox({
3443
autocapitalize: 'off',
3544
autocomplete: 'off',
3645
autocorrect: 'off',
37-
className: cssClass,
3846
placeholder: placeholder,
3947
role: 'textbox',
4048
spellcheck: 'false',
@@ -50,8 +58,8 @@ function searchbox({
5058
input.setAttribute(key, value);
5159
});
5260

53-
// Always add our own classes
54-
input.classList.add('as-search-box__input');
61+
// Add classes
62+
input.classList.add(bem('input'), cssClasses.input);
5563

5664
input.addEventListener('keyup', () => {
5765
helper.setQuery(input.value).search();
@@ -60,10 +68,16 @@ function searchbox({
6068
// Optional "powered by Algolia" widget
6169
if (poweredBy) {
6270
var React = require('react');
63-
var PoweredBy = require('../components/PoweredBy');
71+
var PoweredBy = require('../components/PoweredBy/PoweredBy.js');
6472
var poweredByContainer = document.createElement('div');
6573
input.parentNode.appendChild(poweredByContainer);
66-
React.render(<PoweredBy />, poweredByContainer);
74+
var poweredByClassName = cx(bem('powered-by'), cssClasses.poweredBy);
75+
React.render(
76+
<PoweredBy
77+
className={poweredByClassName}
78+
/>,
79+
poweredByContainer
80+
);
6781
}
6882

6983
helper.on('change', function(state) {
@@ -80,4 +94,4 @@ function searchbox({
8094
};
8195
}
8296

83-
module.exports = searchbox;
97+
module.exports = searchBox;

0 commit comments

Comments
 (0)