Skip to content

Commit

Permalink
fix(search-box): Fix #137 autofocus must be configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Stanislawski committed Oct 1, 2015
1 parent 35825df commit 51f01be
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion widgets/search-box.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
var utils = require('../lib/utils.js');
var forEach = require('lodash/collection/forEach');

var VALID_AUTOFOCUS_VALUES = {
true: true,
false: true,
auto: true
};

/**
* Instantiate a searchbox
* @param {String|DOMElement} options.container CSS Selector or DOMElement to insert the widget
* @param {String} [options.placeholder='Search here'] Input's placeholder
* @param {Object} [options.cssClass] CSS classes to add to the input
* @param {boolean} [poweredBy=false] Show a powered by Algolia link below the input
* @param {boolean|string} [autofocus='auto'] autofocus on the input
* @return {Object}
*/
function searchbox(params) {
Expand All @@ -16,13 +23,16 @@ function searchbox(params) {
input = input.appendChild(document.createElement('input'));
}

var autofocus = (typeof params.autofocus === 'boolean' || VALID_AUTOFOCUS_VALUES[params.autofocus]) ?
params.autofocus :
'auto';

return {
init: function(initialState, helper) {
var defaultAttributes = {
autocapitalize: 'off',
autocomplete: 'off',
autocorrect: 'off',
autofocus: 'autofocus',
className: params.cssClass,
placeholder: params.placeholder,
role: 'textbox',
Expand Down Expand Up @@ -60,6 +70,12 @@ function searchbox(params) {
input.value = state.query;
}
});

if (autofocus === 'true' ||
autofocus === true ||
autofocus === 'auto' && helper.state.query === '') {
input.focus();
}
}
};
}
Expand Down

0 comments on commit 51f01be

Please sign in to comment.