11var  utils  =  require ( '../lib/utils.js' ) ; 
22var  forEach  =  require ( 'lodash/collection/forEach' ) ; 
33
4+ var  VALID_AUTOFOCUS_VALUES  =  { 
5+   true : true , 
6+   false : true , 
7+   auto : true 
8+ } ; 
9+ 
410/** 
511 * Instantiate a searchbox 
612 * @param   {String|DOMElement } options.container CSS Selector or DOMElement to insert the widget 
713 * @param   {String } [options.placeholder='Search here'] Input's placeholder 
814 * @param   {Object } [options.cssClass] CSS classes to add to the input 
915 * @param   {boolean } [poweredBy=false] Show a powered by Algolia link below the input 
16+  * @param   {boolean|string } [autofocus='auto'] autofocus on the input 
1017 * @return  {Object } 
1118 */ 
1219function  searchbox ( params )  { 
@@ -16,13 +23,16 @@ function searchbox(params) {
1623    input  =  input . appendChild ( document . createElement ( 'input' ) ) ; 
1724  } 
1825
26+   var  autofocus  =  ( typeof  params . autofocus  ===  'boolean'  ||  VALID_AUTOFOCUS_VALUES [ params . autofocus ] )  ?
27+     params . autofocus  :
28+     'auto' ; 
29+ 
1930  return  { 
2031    init : function ( initialState ,  helper )  { 
2132      var  defaultAttributes  =  { 
2233        autocapitalize : 'off' , 
2334        autocomplete : 'off' , 
2435        autocorrect : 'off' , 
25-         autofocus : 'autofocus' , 
2636        className : params . cssClass , 
2737        placeholder : params . placeholder , 
2838        role : 'textbox' , 
@@ -60,6 +70,12 @@ function searchbox(params) {
6070          input . value  =  state . query ; 
6171        } 
6272      } ) ; 
73+ 
74+       if  ( autofocus  ===  'true'  || 
75+           autofocus  ===  true  || 
76+           autofocus  ===  'auto'  &&  helper . state . query  ===  '' )  { 
77+         input . focus ( ) ; 
78+       } 
6379    } 
6480  } ; 
6581} 
0 commit comments