Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): render noResults template when openOnFocus is true #573

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions packages/autocomplete-js/src/__tests__/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ describe('autocomplete-js', () => {
});
});

test("doesn't render noResults template on no query when openOnFocus is false", async () => {
test("doesn't render noResults template on empty query when openOnFocus is false", async () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');

Expand Down Expand Up @@ -309,7 +309,7 @@ describe('autocomplete-js', () => {

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

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

await waitFor(() => {
expect(
Expand All @@ -318,7 +318,47 @@ describe('autocomplete-js', () => {
});
});

test('render noResults template on query when openOnFocus is false', async () => {
test('renders noResults template on empty query when openOnFocus is true', 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: 'testSource',
getItems() {
return [];
},
templates: {
item({ item }) {
return item.label;
},
noResults() {
return 'No results template';
},
},
},
];
},
});

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

fireEvent.focus(input);

await waitFor(() => {
expect(
panelContainer.querySelector<HTMLElement>('.aa-Panel')
).toHaveTextContent('No results template');
});
});

test('renders noResults template on query when openOnFocus is false', async () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');

Expand Down
4 changes: 3 additions & 1 deletion packages/autocomplete-js/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export function renderPanel<TItem extends BaseItem>(
</div>
)}

{items.length === 0 && source.templates.noResults && state.query ? (
{!items.length &&
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
source.templates.noResults &&
(state.query || state.isOpen) ? (
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
<div className={classNames.sourceNoResults}>
{source.templates.noResults({
components,
Expand Down