Skip to content

Commit

Permalink
fix(QueryRuleCustomData): pass data as object to templates (#3647)
Browse files Browse the repository at this point in the history
* fix(queryRuleCustomData): pass object to template

* chore(stories): update queryRuleCustomData stories
  • Loading branch information
francoischalifour committed Apr 3, 2019
1 parent 20053a4 commit b8f8b4e
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/components/QueryRuleCustomData/QueryRuleCustomData.tsx
Expand Up @@ -20,7 +20,7 @@ const QueryRuleCustomData = ({
templateKey="default"
templates={templates}
rootProps={{ className: cssClasses.root }}
data={items}
data={{ items }}
/>
);

Expand Down
Expand Up @@ -17,7 +17,7 @@ describe('QueryRuleCustomData', () => {

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

expect(wrapper.props().data).toEqual(items);
expect(wrapper.props().data).toEqual({ items });
expect(wrapper).toMatchSnapshot();
});

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

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

expect(wrapper.props().data).toEqual(items);
expect(wrapper.props().data).toEqual({ items });
expect(wrapper).toMatchSnapshot();
});
});
Expand Up @@ -2,7 +2,11 @@

exports[`QueryRuleCustomData renders with empty items 1`] = `
<Template
data={Array []}
data={
Object {
"items": Array [],
}
}
rootProps={
Object {
"className": "root",
Expand All @@ -23,14 +27,16 @@ exports[`QueryRuleCustomData renders with empty items 1`] = `
exports[`QueryRuleCustomData renders with items 1`] = `
<Template
data={
Array [
Object {
"banner": "image-1.png",
},
Object {
"banner": "image-2.png",
},
]
Object {
"items": Array [
Object {
"banner": "image-1.png",
},
Object {
"banner": "image-2.png",
},
],
}
}
rootProps={
Object {
Expand Down
5 changes: 1 addition & 4 deletions src/components/Template/Template.js
Expand Up @@ -45,10 +45,7 @@ class Template extends Component {
}

Template.propTypes = {
data: PropTypes.oneOfType([
PropTypes.object,
PropTypes.arrayOf(PropTypes.object),
]),
data: PropTypes.object,
rootProps: PropTypes.object,
rootTagName: PropTypes.string,
templateKey: PropTypes.string,
Expand Down
56 changes: 32 additions & 24 deletions stories/query-rule-context.stories.ts
Expand Up @@ -41,21 +41,26 @@ storiesOf('QueryRuleContext', module)
search.addWidget(
instantsearch.widgets.queryRuleCustomData({
container: widgetContainer,
transformItems: (items: CustomDataItem[]) => items[0],
transformItems(items: CustomDataItem[]) {
return items.filter(item => typeof item.banner !== 'undefined');
},
templates: {
default({ title, banner, link }: CustomDataItem) {
if (!banner) {
return '';
}
default: ({ items }: { items: CustomDataItem[] }) =>
items
.map(item => {
const { title, banner, link } = item;

return `
<h2>${title}</h2>
return `
<section>
<h2>${title}</h2>
<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
`;
},
<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
</section>
`;
})
.join(''),
},
})
);
Expand Down Expand Up @@ -94,21 +99,24 @@ storiesOf('QueryRuleContext', module)
search.addWidget(
instantsearch.widgets.queryRuleCustomData({
container: widgetContainer,
transformItems: (items: CustomDataItem[]) => items[0],
transformItems(items: CustomDataItem[]) {
return items.filter(item => typeof item.banner !== 'undefined');
},
templates: {
default({ title, banner, link }: CustomDataItem) {
if (!banner) {
return '';
}
default: ({ items }: { items: CustomDataItem[] }) =>
items.map(item => {
const { title, banner, link } = item;

return `
<h2>${title}</h2>
return `
<section>
<h2>${title}</h2>
<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
`;
},
<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
</section>
`;
}),
},
})
);
Expand Down
86 changes: 64 additions & 22 deletions stories/query-rule-custom-data.stories.ts
Expand Up @@ -29,28 +29,65 @@ storiesOf('QueryRuleCustomData', module)
search.addWidget(
instantsearch.widgets.queryRuleCustomData({
container: widgetContainer,
transformItems: (items: CustomDataItem[]) => items[0],
templates: {
default({ title, banner, link }: CustomDataItem) {
if (!banner) {
return '';
}
default: ({ items }: { items: CustomDataItem[] }) =>
items
.map(item => {
const { title, banner, link } = item;

return `
<h2>${title}</h2>
if (!banner) {
return;
}

<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
`;
},
return `
<section>
<h2>${title}</h2>
<a href="${link}">
<img src="${banner}" alt="${title}">
</a>
</section>
`;
})
.join(''),
},
})
);
}, searchOptions)
)
.add(
'with default banner',
'with Hogan',
withHits(({ search, container, instantsearch }) => {
const widgetContainer = document.createElement('div');
const description = document.createElement('p');
description.innerHTML = 'Type <q>music</q> and a banner will appear.';

container.appendChild(description);
container.appendChild(widgetContainer);

search.addWidget(
instantsearch.widgets.queryRuleCustomData({
container: widgetContainer,
templates: {
default: `
{{#items}}
{{#banner}}
<section>
<h2>{{title}}</h2>
<a href="{{link}}">
<img src="{{banner}}" alt="{{title}}">
</a>
</section>
{{/banner}}
{{/items}}`,
},
})
);
}, searchOptions)
)
.add(
'with default and single banner',
withHits(({ search, container, instantsearch }) => {
const widgetContainer = document.createElement('div');
const description = document.createElement('p');
Expand All @@ -65,21 +102,26 @@ storiesOf('QueryRuleCustomData', module)
container: widgetContainer,
transformItems: (items: CustomDataItem[]) => {
if (items.length > 0) {
return items[0];
return items.filter(item => typeof item.banner !== 'undefined');
}

return {
title: 'Kill Bill',
banner: 'http://static.bobatv.net/IMovie/mv_2352/poster_2352.jpg',
link: 'https://www.netflix.com/title/60031236',
};
return [
{
title: 'Kill Bill',
banner:
'http://static.bobatv.net/IMovie/mv_2352/poster_2352.jpg',
link: 'https://www.netflix.com/title/60031236',
},
];
},
templates: {
default({ title, banner, link }: CustomDataItem) {
if (!banner) {
return '';
default(items: CustomDataItem[]) {
if (items.length === 0) {
return;
}

const { title, banner, link } = items[0];

return `
<h2>${title}</h2>
Expand Down

0 comments on commit b8f8b4e

Please sign in to comment.