Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connectHierarchicalMenu): add hasChildren to items #2638

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions dev/app/builtin/stories/hierarchical-menu.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,30 @@ export default () => {
}
)
)
.add(
'with custom item template',
wrapWithHits(container => {
window.search.addWidget(
instantsearch.widgets.hierarchicalMenu({
container,
attributes: [
'hierarchicalCategories.lvl0',
'hierarchicalCategories.lvl1',
'hierarchicalCategories.lvl2',
],
templates: {
item:
`<a class="{{cssClasses.link}}" href="{{url}}">
{{label}}
</a>
{{#hasChildren}}
<span>Has children</span>
{{/hasChildren}}`,
},
})
);
})
)
.add(
'with header',
wrapWithHits(container => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,22 @@ describe('connectHierarchicalMenu', () => {
value: 'Decoration',
count: 880,
isRefined: true,
hasChildren: true,
data: [
{
label: 'Candle holders & candles',
value: 'Decoration > Candle holders & candles',
count: 193,
isRefined: false,
hasChildren: false,
data: null,
},
{
label: 'Frames & pictures',
value: 'Decoration > Frames & pictures',
count: 173,
isRefined: false,
hasChildren: false,
data: null,
},
],
Expand All @@ -251,6 +254,7 @@ describe('connectHierarchicalMenu', () => {
value: 'Outdoor',
count: 47,
isRefined: false,
hasChildren: false,
data: null,
},
]);
Expand Down
7 changes: 6 additions & 1 deletion src/connectors/hierarchical-menu/connectHierarchicalMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,12 @@ export default function connectHierarchicalMenu(renderFn, unmountFn) {
if (Array.isArray(subValue.data)) {
subValue.data = this._prepareFacetValues(subValue.data, state);
}
return { ...subValue, label, value };
return {
...subValue,
label,
value,
hasChildren: Boolean(subValue.data && subValue.data.length > 0),
};
});
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ exports[`hierarchicalMenu() render calls ReactDOM.render 1`] = `
facetValues={
Array [
Object {
"hasChildren": false,
"label": "foo",
"value": undefined,
},
Object {
"hasChildren": false,
"label": "bar",
"value": undefined,
},
Expand Down Expand Up @@ -72,10 +74,12 @@ exports[`hierarchicalMenu() render has a templates option 1`] = `
facetValues={
Array [
Object {
"hasChildren": false,
"label": "foo",
"value": undefined,
},
Object {
"hasChildren": false,
"label": "bar",
"value": undefined,
},
Expand Down Expand Up @@ -203,10 +207,12 @@ exports[`hierarchicalMenu() render understand provided cssClasses 1`] = `
facetValues={
Array [
Object {
"hasChildren": false,
"label": "foo",
"value": undefined,
},
Object {
"hasChildren": false,
"label": "bar",
"value": undefined,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,18 @@ describe('hierarchicalMenu()', () => {

data = { data: firstLevel };
const expectedFacetValues = [
{ label: 'one', value: 'one' },
{ label: 'one', value: 'one', hasChildren: false },
{
label: 'two',
value: 'two',
data: [
{ label: 'six', value: 'six' },
{ label: 'seven', value: 'seven' },
{ label: 'eight', value: 'eight' },
{ label: 'six', value: 'six', hasChildren: false },
{ label: 'seven', value: 'seven', hasChildren: false },
{ label: 'eight', value: 'eight', hasChildren: false },
],
hasChildren: true,
},
{ label: 'three', value: 'three' },
{ label: 'three', value: 'three', hasChildren: false },
];
widget = hierarchicalMenu({ ...options, limit: 3 });
widget.init({ helper, createURL, instantSearchInstance: {} });
Expand Down