Skip to content

Commit 70f8e1f

Browse files
author
Alexandre Stanislawski
committed
feat(connector): searchbox connector
This one is another interesting case, the implementation is not made using react which leaves us solving lots of stuff by hand. Also the UI code and the business logic were completly entangled which means that we might want to clarify more the API for the rendering.
1 parent bf9a9c0 commit 70f8e1f

File tree

3 files changed

+296
-228
lines changed

3 files changed

+296
-228
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import {
2+
getContainerNode,
3+
} from '../../lib/utils.js';
4+
import defaultTemplates from './defaultTemplates.js';
5+
6+
/**
7+
* Instantiate a searchbox
8+
* @function searchBox
9+
* @param {string|DOMElement} options.container CSS Selector or DOMElement to insert the widget
10+
* @param {string} [options.placeholder] Input's placeholder [*]
11+
* @param {boolean|Object} [options.poweredBy=false] Define if a "powered by Algolia" link should be added near the input
12+
* @param {function|string} [options.poweredBy.template] Template used for displaying the link. Can accept a function or a Hogan string.
13+
* @param {number} [options.poweredBy.cssClasses] CSS classes to add
14+
* @param {string|string[]} [options.poweredBy.cssClasses.root] CSS class to add to the root element
15+
* @param {string|string[]} [options.poweredBy.cssClasses.link] CSS class to add to the link element
16+
* @param {boolean} [options.wrapInput=true] Wrap the input in a `div.ais-search-box`
17+
* @param {boolean|string} [autofocus='auto'] autofocus on the input
18+
* @param {boolean} [options.searchOnEnterKeyPressOnly=false] If set, trigger the search
19+
* once `<Enter>` is pressed only
20+
* @param {Object} [options.cssClasses] CSS classes to add
21+
* @param {string|string[]} [options.cssClasses.root] CSS class to add to the
22+
* wrapping div (if `wrapInput` set to `true`)
23+
* @param {string|string[]} [options.cssClasses.input] CSS class to add to the input
24+
* @param {function} [options.queryHook] A function that will be called every time a new search would be done. You
25+
* will get the query as first parameter and a search(query) function to call as the second parameter.
26+
* This queryHook can be used to debounce the number of searches done from the searchBox.
27+
* @return {Object}
28+
*/
29+
30+
const usage = `Usage:
31+
searchBox({
32+
container,
33+
[ placeholder ],
34+
[ cssClasses.{input,poweredBy} ],
35+
[ poweredBy=false || poweredBy.{template, cssClasses.{root,link}} ],
36+
[ wrapInput ],
37+
[ autofocus ],
38+
[ searchOnEnterKeyPressOnly ],
39+
[ queryHook ]
40+
})`;
41+
const connectSearchBox = searchBoxRendering => ({
42+
container,
43+
placeholder = '',
44+
cssClasses = {},
45+
poweredBy = false,
46+
wrapInput = true,
47+
autofocus = 'auto',
48+
searchOnEnterKeyPressOnly = false,
49+
queryHook,
50+
}) => {
51+
if (!container) {
52+
throw new Error(usage);
53+
}
54+
55+
const containerNode = getContainerNode(container);
56+
57+
// Only possible values are 'auto', true and false
58+
if (typeof autofocus !== 'boolean') {
59+
autofocus = 'auto';
60+
}
61+
62+
// Convert to object if only set to true
63+
if (poweredBy === true) {
64+
poweredBy = {};
65+
}
66+
67+
return {
68+
init({state, helper, onHistoryChange}) {
69+
searchBoxRendering({
70+
query: state.query,
71+
containerNode,
72+
onHistoryChange,
73+
poweredBy,
74+
helper,
75+
wrapInput,
76+
autofocus,
77+
queryHook,
78+
searchOnEnterKeyPressOnly,
79+
placeholder,
80+
cssClasses,
81+
templates: defaultTemplates,
82+
}, true);
83+
},
84+
render({helper}) {
85+
searchBoxRendering({
86+
query: helper.state.query,
87+
containerNode,
88+
onHistoryChange: () => {},
89+
poweredBy,
90+
helper,
91+
wrapInput,
92+
autofocus,
93+
queryHook,
94+
searchOnEnterKeyPressOnly,
95+
placeholder,
96+
cssClasses,
97+
templates: defaultTemplates,
98+
}, false);
99+
},
100+
};
101+
};
102+
103+
export default connectSearchBox;
File renamed without changes.

0 commit comments

Comments
 (0)