-
Notifications
You must be signed in to change notification settings - Fork 498
/
index-selector.js
55 lines (49 loc) · 1.77 KB
/
index-selector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var React = require('react');
var findIndex = require('lodash/array/findIndex');
var utils = require('../lib/utils.js');
/**
* Instantiate a dropdown element to choose the current targeted index
* @param {String|DOMElement} options.container Valid CSS Selector as a string or DOMElement
* @param {Array} options.indices Array of objects defining the different indices to choose from. Each object must contain a `name` and `label` key.
* @param {String} [options.cssClass] Class name(s) to be added to the generated select element
* @param {boolean} [hideIfEmpty=false] Hide the container when no results match
* @return {Object}
*/
function indexSelector({
container = null,
indices = null,
cssClass,
hideIfEmpty = false
}) {
var IndexSelector = require('../components/IndexSelector');
var containerNode = utils.getContainerNode(container);
var usage = 'Usage: indexSelector({container, indices[, cssClass]})';
if (container === null || indices === null) {
throw new Error(usage);
}
return {
init: function(state, helper) {
var currentIndex = helper.getIndex();
var isIndexInList = findIndex(indices, {name: currentIndex}) !== -1;
if (!isIndexInList) {
throw new Error('[stats]: Index ' + currentIndex + ' not present in `indices`');
}
},
render: function({helper, results}) {
var containerId = containerNode.id;
React.render(
<IndexSelector
containerId={containerId}
cssClass={cssClass}
currentIndex={helper.getIndex()}
indices={indices}
hideIfEmpty={hideIfEmpty}
hasResults={results.hits.length > 0}
setIndex={helper.setIndex.bind(helper)}
/>,
containerNode
);
}
};
}
module.exports = indexSelector;