Skip to content

Commit 660ee2f

Browse files
author
vvo
committed
fix(searchBox): fixes cssClasses option
We were trying to call classList.add('a list of css classes'); while it's only callable like this: classList.add('a', 'list', 'of'..); fixes #775 This is a hotfix
1 parent 5acd554 commit 660ee2f

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/widgets/search-box/__tests__/search-box-test.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ describe('search-box()', () => {
4949
});
5050

5151
context('targeting a div', () => {
52+
let opts;
53+
5254
beforeEach(() => {
5355
container = document.createElement('div');
54-
widget = searchBox({container});
55-
});
56-
57-
it('configures nothing', () => {
58-
expect(widget.getConfiguration).toEqual(undefined);
56+
opts = {container};
5957
});
6058

6159
it('adds an input inside the div', () => {
60+
widget = searchBox(opts);
6261
widget.init({state, helper});
6362
let inputs = container.getElementsByTagName('input');
6463
expect(inputs.length).toEqual(1);
6564
});
6665

6766
it('sets default HTML attribute to the input', () => {
67+
widget = searchBox(opts);
6868
widget.init({state, helper});
6969
let input = container.getElementsByTagName('input')[0];
7070
expect(input.getAttribute('autocapitalize')).toEqual('off');
@@ -76,6 +76,23 @@ describe('search-box()', () => {
7676
expect(input.getAttribute('spellcheck')).toEqual('false');
7777
expect(input.getAttribute('type')).toEqual('text');
7878
});
79+
80+
it('supports cssClasses option', () => {
81+
opts.cssClasses = {
82+
root: 'root-class',
83+
input: 'input-class'
84+
};
85+
86+
widget = searchBox(opts);
87+
widget.init({state, helper});
88+
let actualRootClasses = container.querySelector('input').parentNode.getAttribute('class');
89+
let actualInputClasses = container.querySelector('input').getAttribute('class');
90+
let expectedRootClasses = 'ais-search-box root-class';
91+
let expectedInputClasses = 'ais-search-box--input input-class';
92+
93+
expect(actualRootClasses).toEqual(expectedRootClasses);
94+
expect(actualInputClasses).toEqual(expectedInputClasses);
95+
});
7996
});
8097

8198
context('targeting an input', () => {
@@ -102,6 +119,20 @@ describe('search-box()', () => {
102119
expect(container.getAttribute('class')).toEqual('my-class ais-search-box--input');
103120
expect(container.getAttribute('placeholder')).toEqual('Search');
104121
});
122+
123+
it('supports cssClasses', () => {
124+
container = createHTMLNodeFromString('<input class="my-class" />');
125+
widget = searchBox({container, cssClasses: {root: 'root-class', input: 'input-class'}});
126+
widget.init({state, helper});
127+
128+
let actualRootClasses = container.parentNode.getAttribute('class');
129+
let actualInputClasses = container.getAttribute('class');
130+
let expectedRootClasses = 'ais-search-box root-class';
131+
let expectedInputClasses = 'my-class ais-search-box--input input-class';
132+
133+
expect(actualRootClasses).toEqual(expectedRootClasses);
134+
expect(actualInputClasses).toEqual(expectedInputClasses);
135+
});
105136
});
106137

107138
context('wraps the input in a div', () => {

src/widgets/search-box/search-box.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const KEY_SUPPRESS = 8;
1919
* @param {boolean} [options.searchOnEnterKeyPressOnly=false] If set, trigger the search
2020
* once `<Enter>` is pressed only
2121
* @param {Object} [options.cssClasses] CSS classes to add
22-
* @param {string} [options.cssClasses.root] CSS class to add to the
22+
* @param {string|string[]} [options.cssClasses.root] CSS class to add to the
2323
* wrapping div (if `wrapInput` set to `true`)
24-
* @param {string} [options.cssClasses.input] CSS class to add to the input
25-
* @param {string} [options.cssClasses.poweredBy] CSS class to add to the poweredBy element
24+
* @param {string|string[]} [options.cssClasses.input] CSS class to add to the input
25+
* @param {string|string[]} [options.cssClasses.poweredBy] CSS class to add to the poweredBy element
2626
* @return {Object}
2727
*/
2828
const usage = `Usage:
@@ -66,7 +66,8 @@ function searchBox({
6666
wrapInput: function(input) {
6767
// Wrap input in a .ais-search-box div
6868
let wrapper = document.createElement('div');
69-
wrapper.classList.add(cx(bem(null), cssClasses.root));
69+
let CSSClassesToAdd = cx(bem(null), cssClasses.root).split(' ');
70+
wrapper.classList.add.apply(wrapper.classList, CSSClassesToAdd);
7071
wrapper.appendChild(input);
7172
return wrapper;
7273
},
@@ -91,7 +92,8 @@ function searchBox({
9192
});
9293

9394
// Add classes
94-
input.classList.add(cx(bem('input'), cssClasses.input));
95+
let CSSClassesToAdd = cx(bem('input'), cssClasses.input).split(' ');
96+
input.classList.add.apply(input.classList, CSSClassesToAdd);
9597
},
9698
addPoweredBy: function(input) {
9799
let PoweredBy = require('../../components/PoweredBy/PoweredBy.js');

0 commit comments

Comments
 (0)