Skip to content

Commit b8f8b4e

Browse files
fix(QueryRuleCustomData): pass data as object to templates (#3647)
* fix(queryRuleCustomData): pass object to template * chore(stories): update queryRuleCustomData stories
1 parent 20053a4 commit b8f8b4e

File tree

6 files changed

+115
-62
lines changed

6 files changed

+115
-62
lines changed

src/components/QueryRuleCustomData/QueryRuleCustomData.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const QueryRuleCustomData = ({
2020
templateKey="default"
2121
templates={templates}
2222
rootProps={{ className: cssClasses.root }}
23-
data={items}
23+
data={{ items }}
2424
/>
2525
);
2626

src/components/QueryRuleCustomData/__tests__/QueryRuleCustomData-test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('QueryRuleCustomData', () => {
1717

1818
const wrapper = shallow(<QueryRuleCustomData {...props} />);
1919

20-
expect(wrapper.props().data).toEqual(items);
20+
expect(wrapper.props().data).toEqual({ items });
2121
expect(wrapper).toMatchSnapshot();
2222
});
2323

@@ -35,7 +35,7 @@ describe('QueryRuleCustomData', () => {
3535

3636
const wrapper = shallow(<QueryRuleCustomData {...props} />);
3737

38-
expect(wrapper.props().data).toEqual(items);
38+
expect(wrapper.props().data).toEqual({ items });
3939
expect(wrapper).toMatchSnapshot();
4040
});
4141
});

src/components/QueryRuleCustomData/__tests__/__snapshots__/QueryRuleCustomData-test.tsx.snap

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
exports[`QueryRuleCustomData renders with empty items 1`] = `
44
<Template
5-
data={Array []}
5+
data={
6+
Object {
7+
"items": Array [],
8+
}
9+
}
610
rootProps={
711
Object {
812
"className": "root",
@@ -23,14 +27,16 @@ exports[`QueryRuleCustomData renders with empty items 1`] = `
2327
exports[`QueryRuleCustomData renders with items 1`] = `
2428
<Template
2529
data={
26-
Array [
27-
Object {
28-
"banner": "image-1.png",
29-
},
30-
Object {
31-
"banner": "image-2.png",
32-
},
33-
]
30+
Object {
31+
"items": Array [
32+
Object {
33+
"banner": "image-1.png",
34+
},
35+
Object {
36+
"banner": "image-2.png",
37+
},
38+
],
39+
}
3440
}
3541
rootProps={
3642
Object {

src/components/Template/Template.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ class Template extends Component {
4545
}
4646

4747
Template.propTypes = {
48-
data: PropTypes.oneOfType([
49-
PropTypes.object,
50-
PropTypes.arrayOf(PropTypes.object),
51-
]),
48+
data: PropTypes.object,
5249
rootProps: PropTypes.object,
5350
rootTagName: PropTypes.string,
5451
templateKey: PropTypes.string,

stories/query-rule-context.stories.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,26 @@ storiesOf('QueryRuleContext', module)
4141
search.addWidget(
4242
instantsearch.widgets.queryRuleCustomData({
4343
container: widgetContainer,
44-
transformItems: (items: CustomDataItem[]) => items[0],
44+
transformItems(items: CustomDataItem[]) {
45+
return items.filter(item => typeof item.banner !== 'undefined');
46+
},
4547
templates: {
46-
default({ title, banner, link }: CustomDataItem) {
47-
if (!banner) {
48-
return '';
49-
}
48+
default: ({ items }: { items: CustomDataItem[] }) =>
49+
items
50+
.map(item => {
51+
const { title, banner, link } = item;
5052

51-
return `
52-
<h2>${title}</h2>
53+
return `
54+
<section>
55+
<h2>${title}</h2>
5356
54-
<a href="${link}">
55-
<img src="${banner}" alt="${title}">
56-
</a>
57-
`;
58-
},
57+
<a href="${link}">
58+
<img src="${banner}" alt="${title}">
59+
</a>
60+
</section>
61+
`;
62+
})
63+
.join(''),
5964
},
6065
})
6166
);
@@ -94,21 +99,24 @@ storiesOf('QueryRuleContext', module)
9499
search.addWidget(
95100
instantsearch.widgets.queryRuleCustomData({
96101
container: widgetContainer,
97-
transformItems: (items: CustomDataItem[]) => items[0],
102+
transformItems(items: CustomDataItem[]) {
103+
return items.filter(item => typeof item.banner !== 'undefined');
104+
},
98105
templates: {
99-
default({ title, banner, link }: CustomDataItem) {
100-
if (!banner) {
101-
return '';
102-
}
106+
default: ({ items }: { items: CustomDataItem[] }) =>
107+
items.map(item => {
108+
const { title, banner, link } = item;
103109

104-
return `
105-
<h2>${title}</h2>
110+
return `
111+
<section>
112+
<h2>${title}</h2>
106113
107-
<a href="${link}">
108-
<img src="${banner}" alt="${title}">
109-
</a>
110-
`;
111-
},
114+
<a href="${link}">
115+
<img src="${banner}" alt="${title}">
116+
</a>
117+
</section>
118+
`;
119+
}),
112120
},
113121
})
114122
);

stories/query-rule-custom-data.stories.ts

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,65 @@ storiesOf('QueryRuleCustomData', module)
2929
search.addWidget(
3030
instantsearch.widgets.queryRuleCustomData({
3131
container: widgetContainer,
32-
transformItems: (items: CustomDataItem[]) => items[0],
3332
templates: {
34-
default({ title, banner, link }: CustomDataItem) {
35-
if (!banner) {
36-
return '';
37-
}
33+
default: ({ items }: { items: CustomDataItem[] }) =>
34+
items
35+
.map(item => {
36+
const { title, banner, link } = item;
3837

39-
return `
40-
<h2>${title}</h2>
38+
if (!banner) {
39+
return;
40+
}
4141

42-
<a href="${link}">
43-
<img src="${banner}" alt="${title}">
44-
</a>
45-
`;
46-
},
42+
return `
43+
<section>
44+
<h2>${title}</h2>
45+
46+
<a href="${link}">
47+
<img src="${banner}" alt="${title}">
48+
</a>
49+
</section>
50+
`;
51+
})
52+
.join(''),
4753
},
4854
})
4955
);
5056
}, searchOptions)
5157
)
5258
.add(
53-
'with default banner',
59+
'with Hogan',
60+
withHits(({ search, container, instantsearch }) => {
61+
const widgetContainer = document.createElement('div');
62+
const description = document.createElement('p');
63+
description.innerHTML = 'Type <q>music</q> and a banner will appear.';
64+
65+
container.appendChild(description);
66+
container.appendChild(widgetContainer);
67+
68+
search.addWidget(
69+
instantsearch.widgets.queryRuleCustomData({
70+
container: widgetContainer,
71+
templates: {
72+
default: `
73+
{{#items}}
74+
{{#banner}}
75+
<section>
76+
<h2>{{title}}</h2>
77+
78+
<a href="{{link}}">
79+
<img src="{{banner}}" alt="{{title}}">
80+
</a>
81+
</section>
82+
{{/banner}}
83+
{{/items}}`,
84+
},
85+
})
86+
);
87+
}, searchOptions)
88+
)
89+
.add(
90+
'with default and single banner',
5491
withHits(({ search, container, instantsearch }) => {
5592
const widgetContainer = document.createElement('div');
5693
const description = document.createElement('p');
@@ -65,21 +102,26 @@ storiesOf('QueryRuleCustomData', module)
65102
container: widgetContainer,
66103
transformItems: (items: CustomDataItem[]) => {
67104
if (items.length > 0) {
68-
return items[0];
105+
return items.filter(item => typeof item.banner !== 'undefined');
69106
}
70107

71-
return {
72-
title: 'Kill Bill',
73-
banner: 'http://static.bobatv.net/IMovie/mv_2352/poster_2352.jpg',
74-
link: 'https://www.netflix.com/title/60031236',
75-
};
108+
return [
109+
{
110+
title: 'Kill Bill',
111+
banner:
112+
'http://static.bobatv.net/IMovie/mv_2352/poster_2352.jpg',
113+
link: 'https://www.netflix.com/title/60031236',
114+
},
115+
];
76116
},
77117
templates: {
78-
default({ title, banner, link }: CustomDataItem) {
79-
if (!banner) {
80-
return '';
118+
default(items: CustomDataItem[]) {
119+
if (items.length === 0) {
120+
return;
81121
}
82122

123+
const { title, banner, link } = items[0];
124+
83125
return `
84126
<h2>${title}</h2>
85127

0 commit comments

Comments
 (0)