Skip to content

Commit

Permalink
fix(queryRuleCustomData): add default template (#3650)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Apr 5, 2019
1 parent e16ad57 commit 83e9eaa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Expand Up @@ -99,8 +99,22 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/query-rule-
const { templates } = render.mock.calls[0][0].props;

expect(templates).toEqual({
default: '',
default: expect.any(Function),
});
expect(
templates.default({
items: [{ banner: '1.jpg' }, { banner: '2.jpg' }],
})
).toMatchInlineSnapshot(`
"[
{
\\"banner\\": \\"1.jpg\\"
},
{
\\"banner\\": \\"2.jpg\\"
}
]"
`);
});

test('applies custom template', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/widgets/query-rule-custom-data/query-rule-custom-data.tsx
Expand Up @@ -16,7 +16,7 @@ export type QueryRuleCustomDataCSSClasses = {
};

export type QueryRuleCustomDataTemplates = {
default: string | ((items: object[]) => string);
default: string | (({ items }: { items: object[] }) => string);
};

type QueryRuleCustomDataWidgetParams = {
Expand Down Expand Up @@ -69,7 +69,9 @@ const queryRuleCustomData: QueryRuleCustomData = (
root: cx(suit(), userCssClasses.root),
};

const defaultTemplates = { default: '' };
const defaultTemplates = {
default: ({ items }) => JSON.stringify(items, null, 2),
};
const templates: QueryRuleCustomDataTemplates = {
...defaultTemplates,
...userTemplates,
Expand Down
17 changes: 17 additions & 0 deletions stories/query-rule-custom-data.stories.ts
Expand Up @@ -134,4 +134,21 @@ storiesOf('QueryRuleCustomData', module)
})
);
}, searchOptions)
)
.add(
'without template',
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,
})
);
}, searchOptions)
);

0 comments on commit 83e9eaa

Please sign in to comment.