1
1
var utils = require ( '../lib/utils.js' ) ;
2
2
var forEach = require ( 'lodash/collection/forEach' ) ;
3
+ var bem = require ( '../lib/utils' ) . bemHelper ( 'ais-search-box' ) ;
4
+ var cx = require ( 'classnames' ) ;
3
5
4
6
/**
5
7
* Instantiate a searchbox
6
8
* @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
9
13
* @param {boolean } [poweredBy=false] Show a powered by Algolia link below the input
10
14
* @param {boolean|string } [autofocus='auto'] autofocus on the input
11
15
* @return {Object }
12
16
*/
13
- function searchbox ( {
17
+ function searchBox ( {
14
18
container,
15
19
placeholder,
16
- cssClass ,
20
+ cssClasses = { } ,
17
21
poweredBy = false ,
18
22
autofocus = 'auto'
19
23
} ) {
20
- // Hook on an existing input, or add one if none targeted
21
24
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
22
31
if ( input . tagName !== 'INPUT' ) {
23
32
input = input . appendChild ( document . createElement ( 'input' ) ) ;
24
33
}
@@ -34,7 +43,6 @@ function searchbox({
34
43
autocapitalize : 'off' ,
35
44
autocomplete : 'off' ,
36
45
autocorrect : 'off' ,
37
- className : cssClass ,
38
46
placeholder : placeholder ,
39
47
role : 'textbox' ,
40
48
spellcheck : 'false' ,
@@ -50,8 +58,8 @@ function searchbox({
50
58
input . setAttribute ( key , value ) ;
51
59
} ) ;
52
60
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 ) ;
55
63
56
64
input . addEventListener ( 'keyup' , ( ) => {
57
65
helper . setQuery ( input . value ) . search ( ) ;
@@ -60,10 +68,16 @@ function searchbox({
60
68
// Optional "powered by Algolia" widget
61
69
if ( poweredBy ) {
62
70
var React = require ( 'react' ) ;
63
- var PoweredBy = require ( '../components/PoweredBy' ) ;
71
+ var PoweredBy = require ( '../components/PoweredBy/PoweredBy.js ' ) ;
64
72
var poweredByContainer = document . createElement ( 'div' ) ;
65
73
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
+ ) ;
67
81
}
68
82
69
83
helper . on ( 'change' , function ( state ) {
@@ -80,4 +94,4 @@ function searchbox({
80
94
} ;
81
95
}
82
96
83
- module . exports = searchbox ;
97
+ module . exports = searchBox ;
0 commit comments