diff --git a/packages/autocomplete-js/src/__tests__/render.test.ts b/packages/autocomplete-js/src/__tests__/render.test.ts index ceeab1214..1ea8b21b2 100644 --- a/packages/autocomplete-js/src/__tests__/render.test.ts +++ b/packages/autocomplete-js/src/__tests__/render.test.ts @@ -9,6 +9,9 @@ import { import { autocomplete } from '../autocomplete'; describe('render', () => { + const sourceId1 = 'testSource1'; + const sourceId2 = 'testSource2'; + beforeEach(() => { document.body.innerHTML = ''; }); @@ -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'); @@ -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'); @@ -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('.aa-Input'); + + fireEvent.input(input, { target: { value: 'a' } }); + + await waitFor(() => { + expect( + panelContainer.querySelector('.aa-Panel') + ).toBeInTheDocument(); + + expect( + panelContainer.querySelector( + `[data-autocomplete-source-id="${sourceId1}"]` + ) + ).not.toBeInTheDocument(); + expect( + panelContainer.querySelector( + `[data-autocomplete-source-id="${sourceId2}"]` + ) + ).toBeInTheDocument(); + }); + }); }); diff --git a/packages/autocomplete-js/src/render.tsx b/packages/autocomplete-js/src/render.tsx index 0e21cea1c..82eda51b5 100644 --- a/packages/autocomplete-js/src/render.tsx +++ b/packages/autocomplete-js/src/render.tsx @@ -90,87 +90,91 @@ export function renderPanel( dom.panel.classList.toggle('aa-Panel--stalled', state.status === 'stalled'); - const sections = state.collections.map(({ source, items }, sourceIndex) => ( -
- {source.templates.header && ( -
- {source.templates.header({ - components, - createElement, - Fragment, - items, - source, - state, - })} -
- )} + const sections = state.collections + .filter( + ({ source, items }) => source.templates.noResults || items.length !== 0 + ) + .map(({ source, items }, sourceIndex) => ( +
+ {source.templates.header && ( +
+ {source.templates.header({ + components, + createElement, + Fragment, + items, + source, + state, + })} +
+ )} - {source.templates.noResults && items.length === 0 ? ( -
- {source.templates.noResults({ - components, - createElement, - Fragment, - source, - state, - })} -
- ) : ( -
    - {items.map((item) => { - const itemProps = autocomplete.getItemProps({ - item, + {source.templates.noResults && items.length === 0 ? ( +
    + {source.templates.noResults({ + components, + createElement, + Fragment, source, - }); + state, + })} +
    + ) : ( +
      + {items.map((item) => { + const itemProps = autocomplete.getItemProps({ + item, + source, + }); - return ( -
    • - {source.templates.item({ - components, - createElement, - Fragment, - item, - state, - })} -
    • - ); - })} -
    - )} + return ( +
  • + {source.templates.item({ + components, + createElement, + Fragment, + item, + state, + })} +
  • + ); + })} +
+ )} - {source.templates.footer && ( -
- {source.templates.footer({ - components, - createElement, - Fragment, - items, - source, - state, - })} -
- )} -
- )); + {source.templates.footer && ( +
+ {source.templates.footer({ + components, + createElement, + Fragment, + items, + source, + state, + })} +
+ )} +
+ )); const children = (