Skip to content

Commit

Permalink
fix(js): do not render empty sections (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored May 25, 2021
1 parent ca396ad commit 527670e
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 127 deletions.
6 changes: 1 addition & 5 deletions cypress/test-apps/js/categoriesPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ export function createCategoriesPlugin({
});
},
templates: {
header({ items }) {
if (items.length === 0) {
return null;
}

header() {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">Categories</span>
Expand Down
10 changes: 3 additions & 7 deletions examples/instantsearch/src/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,7 @@ const querySuggestionsPluginInCategory = createQuerySuggestionsPlugin({
},
templates: {
...source.templates,
header({ items }) {
if (items.length === 0) {
return null;
}

header() {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">In {currentCategory}</span>
Expand Down Expand Up @@ -232,8 +228,8 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
},
templates: {
...source.templates,
header({ items }) {
if (!currentCategory || items.length === 0) {
header() {
if (!currentCategory) {
return null;
}

Expand Down
12 changes: 2 additions & 10 deletions examples/multiple-datasets-with-headers/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
...source,
templates: {
...source.templates,
header({ items }) {
if (items.length === 0) {
return null;
}

header() {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">Your searches</span>
Expand All @@ -49,11 +45,7 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
...source,
templates: {
...source.templates,
header({ items, state }) {
if (items.length === 0) {
return null;
}

header({ state }) {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">
Expand Down
6 changes: 1 addition & 5 deletions examples/multiple-datasets-with-headers/categoriesPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ export function createCategoriesPlugin({
});
},
templates: {
header({ items }) {
if (items.length === 0) {
return null;
}

header() {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">Categories</span>
Expand Down
6 changes: 1 addition & 5 deletions examples/playground/categoriesPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ export function createCategoriesPlugin({
});
},
templates: {
header({ items }) {
if (items.length === 0) {
return null;
}

header() {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">Categories</span>
Expand Down
6 changes: 1 addition & 5 deletions examples/preview-panel-in-modal/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ autocomplete({
refresh();
},
templates: {
header({ items, Fragment }) {
if (items.length === 0) {
return null;
}

header({ Fragment }) {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">
Expand Down
8 changes: 4 additions & 4 deletions examples/query-suggestions-with-recent-searches/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const recentSearchesPlugin = createLocalStorageRecentSearchesPlugin({
...source,
templates: {
...source.templates,
header({ items, state }) {
if (Boolean(state.query) || items.length === 0) {
header({ state }) {
if (state.query) {
return null;
}

Expand Down Expand Up @@ -48,8 +48,8 @@ const querySuggestionsPlugin = createQuerySuggestionsPlugin({
...source,
templates: {
...source.templates,
header({ items, state }) {
if (Boolean(state.query) || items.length === 0) {
header({ state }) {
if (state.query) {
return null;
}

Expand Down
6 changes: 1 addition & 5 deletions examples/recently-viewed-items/recentlyViewedItemsPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ export function createLocalStorageRecentlyViewedItems<
},
templates: {
...transformedSource.templates,
header({ items }) {
if (items.length === 0) {
return null;
}

header() {
return (
<Fragment>
<span className="aa-SourceHeaderTitle">Recently viewed</span>
Expand Down
78 changes: 74 additions & 4 deletions packages/autocomplete-js/src/__tests__/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
import { autocomplete } from '../autocomplete';

describe('render', () => {
const sourceId1 = 'testSource1';
const sourceId2 = 'testSource2';

beforeEach(() => {
document.body.innerHTML = '';
});
Expand Down Expand Up @@ -181,8 +184,6 @@ describe('render', () => {
});

test('provides the elements', async () => {
const sourceId1 = 'testSource1';
const sourceId2 = 'testSource2';
const container = document.createElement('div');
const panelContainer = document.createElement('div');

Expand Down Expand Up @@ -253,8 +254,6 @@ describe('render', () => {
});

test('provides the sections', async () => {
const sourceId1 = 'testSource1';
const sourceId2 = 'testSource2';
const container = document.createElement('div');
const panelContainer = document.createElement('div');

Expand Down Expand Up @@ -536,4 +535,75 @@ describe('render', () => {
},
});
});

test('does not render the sections without results and noResults template on multi sources', async () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');

document.body.appendChild(panelContainer);
autocomplete<{ label: string }>({
container,
panelContainer,
openOnFocus: true,
getSources() {
return [
{
sourceId: sourceId1,
getItems() {
return [];
},
templates: {
header() {
return sourceId1;
},
item({ item }) {
return item.label;
},
footer() {
return sourceId1;
},
},
},
{
sourceId: sourceId2,
getItems() {
return [{ label: '2' }];
},
templates: {
header() {
return sourceId2;
},
item({ item }) {
return item.label;
},
footer() {
return sourceId2;
},
},
},
];
},
});

const input = container.querySelector<HTMLInputElement>('.aa-Input');

fireEvent.input(input, { target: { value: 'a' } });

await waitFor(() => {
expect(
panelContainer.querySelector<HTMLElement>('.aa-Panel')
).toBeInTheDocument();

expect(
panelContainer.querySelector(
`[data-autocomplete-source-id="${sourceId1}"]`
)
).not.toBeInTheDocument();
expect(
panelContainer.querySelector(
`[data-autocomplete-source-id="${sourceId2}"]`
)
).toBeInTheDocument();
});
});
});
Loading

0 comments on commit 527670e

Please sign in to comment.