Skip to content

Commit b9847cf

Browse files
author
Alexandre Stanislawski
committed
feat(connector): sort by selector connector
1 parent c8fcf4e commit b9847cf

File tree

3 files changed

+108
-69
lines changed

3 files changed

+108
-69
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import findIndex from 'lodash/findIndex';
2+
import map from 'lodash/map';
3+
import {
4+
bemHelper,
5+
getContainerNode,
6+
} from '../../lib/utils.js';
7+
import cx from 'classnames';
8+
9+
const bem = bemHelper('ais-sort-by-selector');
10+
/**
11+
* Instantiate a dropdown element to choose the current targeted index
12+
* @function sortBySelector
13+
* @param {string|DOMElement} options.container CSS Selector or DOMElement to insert the widget
14+
* @param {Array} options.indices Array of objects defining the different indices to choose from.
15+
* @param {string} options.indices[0].name Name of the index to target
16+
* @param {string} options.indices[0].label Label displayed in the dropdown
17+
* @param {boolean} [options.autoHideContainer=false] Hide the container when no results match
18+
* @param {Object} [options.cssClasses] CSS classes to be added
19+
* @param {string|string[]} [options.cssClasses.root] CSS classes added to the parent <select>
20+
* @param {string|string[]} [options.cssClasses.item] CSS classes added to each <option>
21+
* @return {Object}
22+
*/
23+
const usage = `Usage:
24+
sortBySelector({
25+
container,
26+
indices,
27+
[cssClasses.{root,item}={}],
28+
[autoHideContainer=false]
29+
})`;
30+
const connectSortBySelector = sortBySelectorRendering => ({
31+
container,
32+
indices,
33+
cssClasses: userCssClasses = {},
34+
autoHideContainer = false,
35+
} = {}) => {
36+
if (!container || !indices) {
37+
throw new Error(usage);
38+
}
39+
40+
const containerNode = getContainerNode(container);
41+
42+
const selectorOptions = map(
43+
indices,
44+
index => ({label: index.label, value: index.name})
45+
);
46+
47+
const cssClasses = {
48+
root: cx(bem(null), userCssClasses.root),
49+
item: cx(bem('item'), userCssClasses.item),
50+
};
51+
52+
return {
53+
init({helper}) {
54+
const currentIndex = helper.getIndex();
55+
const isIndexInList = findIndex(indices, {name: currentIndex}) !== -1;
56+
if (!isIndexInList) {
57+
throw new Error(`[sortBySelector]: Index ${currentIndex} not present in \`indices\``);
58+
}
59+
this.setIndex = indexName => helper
60+
.setIndex(indexName)
61+
.search();
62+
63+
sortBySelectorRendering({
64+
cssClasses,
65+
currentValue: helper.getIndex(),
66+
options: selectorOptions,
67+
setValue: this.setIndex,
68+
shouldAutoHideContainer: autoHideContainer,
69+
containerNode,
70+
}, true);
71+
},
72+
73+
render({helper, results}) {
74+
sortBySelectorRendering({
75+
cssClasses,
76+
currentValue: helper.getIndex(),
77+
options: selectorOptions,
78+
setValue: this.setIndex,
79+
shouldAutoHideContainer: autoHideContainer && results.nbHits === 0,
80+
containerNode,
81+
}, false);
82+
},
83+
};
84+
};
85+
86+
export default connectSortBySelector;

src/widgets/sort-by-selector/__tests__/sort-by-selector-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ describe('sortBySelector()', () => {
7676
item: 'ais-sort-by-selector--item custom-item',
7777
},
7878
currentValue: 'index-a',
79-
shouldAutoHideContainer: true,
8079
options: [
8180
{value: 'index-a', label: 'Index A'},
8281
{value: 'index-b', label: 'Index B'},
Lines changed: 22 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import findIndex from 'lodash/findIndex';
4-
import map from 'lodash/map';
5-
import {
6-
bemHelper,
7-
getContainerNode,
8-
} from '../../lib/utils.js';
9-
import cx from 'classnames';
10-
import autoHideContainerHOC from '../../decorators/autoHideContainer.js';
11-
import SelectorComponent from '../../components/Selector.js';
123

13-
const bem = bemHelper('ais-sort-by-selector');
4+
import Selector from '../../components/Selector.js';
5+
import connectSortBySelector from '../../connectors/sort-by-selector/connectSortBySelector.js';
6+
147
/**
158
* Instantiate a dropdown element to choose the current targeted index
169
* @function sortBySelector
@@ -24,64 +17,25 @@ const bem = bemHelper('ais-sort-by-selector');
2417
* @param {string|string[]} [options.cssClasses.item] CSS classes added to each <option>
2518
* @return {Object}
2619
*/
27-
const usage = `Usage:
28-
sortBySelector({
29-
container,
30-
indices,
31-
[cssClasses.{root,item}={}],
32-
[autoHideContainer=false]
33-
})`;
34-
function sortBySelector({
35-
container,
36-
indices,
37-
cssClasses: userCssClasses = {},
38-
autoHideContainer = false,
39-
} = {}) {
40-
if (!container || !indices) {
41-
throw new Error(usage);
42-
}
43-
44-
const containerNode = getContainerNode(container);
45-
let Selector = SelectorComponent;
46-
if (autoHideContainer === true) {
47-
Selector = autoHideContainerHOC(Selector);
48-
}
4920

50-
const selectorOptions = map(
51-
indices,
52-
index => ({label: index.label, value: index.name})
21+
export default connectSortBySelector(defaultRendering);
22+
function defaultRendering({
23+
cssClasses,
24+
currentValue,
25+
options,
26+
setValue,
27+
shouldAutoHideContainer,
28+
containerNode,
29+
}, isFirstRendering) {
30+
if (isFirstRendering) return;
31+
ReactDOM.render(
32+
<Selector
33+
cssClasses={cssClasses}
34+
currentValue={currentValue}
35+
options={options}
36+
setValue={setValue}
37+
shouldAutoHideContainer={shouldAutoHideContainer}
38+
/>,
39+
containerNode
5340
);
54-
55-
const cssClasses = {
56-
root: cx(bem(null), userCssClasses.root),
57-
item: cx(bem('item'), userCssClasses.item),
58-
};
59-
60-
return {
61-
init({helper}) {
62-
const currentIndex = helper.getIndex();
63-
const isIndexInList = findIndex(indices, {name: currentIndex}) !== -1;
64-
if (!isIndexInList) {
65-
throw new Error(`[sortBySelector]: Index ${currentIndex} not present in \`indices\``);
66-
}
67-
this.setIndex = indexName => helper
68-
.setIndex(indexName)
69-
.search();
70-
},
71-
72-
render({helper, results}) {
73-
ReactDOM.render(
74-
<Selector
75-
cssClasses={cssClasses}
76-
currentValue={helper.getIndex()}
77-
options={selectorOptions}
78-
setValue={this.setIndex}
79-
shouldAutoHideContainer={results.nbHits === 0}
80-
/>,
81-
containerNode
82-
);
83-
},
84-
};
8541
}
86-
87-
export default sortBySelector;

0 commit comments

Comments
 (0)