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): ignore empty template with no query and openOnFocus #407

Merged
merged 5 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
89 changes: 89 additions & 0 deletions packages/autocomplete-js/src/__tests__/autocomplete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ describe('autocomplete-js', () => {
autocomplete<{ label: string }>({
container,
panelContainer,
openOnFocus: true,
getSources() {
return [
{
Expand Down Expand Up @@ -200,6 +201,92 @@ describe('autocomplete-js', () => {
).toHaveTextContent('No results template');
});

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

document.body.appendChild(panelContainer);
autocomplete<{ label: string }>({
container,
panelContainer,
openOnFocus: false,
getSources() {
return [
{
getItems() {
return [];
},
templates: {
item({ item }) {
return item.label;
},
empty() {
return 'No results template';
},
},
},
];
},
});

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

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

await waitFor(() => {
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
expect(
panelContainer.querySelector<HTMLElement>('.aa-Panel')
).not.toBeInTheDocument();
});

expect(
panelContainer.querySelector<HTMLElement>('.aa-Panel')
).not.toBeInTheDocument();
});

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

document.body.appendChild(panelContainer);
autocomplete<{ label: string }>({
container,
panelContainer,
openOnFocus: true,
getSources() {
return [
{
getItems() {
return [];
},
templates: {
item({ item }) {
return item.label;
},
empty() {
return 'No results template';
},
},
},
];
},
});

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

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

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

expect(
panelContainer.querySelector<HTMLElement>('.aa-Panel')
).toHaveTextContent('No results template');
shortcuts marked this conversation as resolved.
Show resolved Hide resolved
});

test('calls renderEmpty without empty template on no results', async () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');
Expand All @@ -214,6 +301,7 @@ describe('autocomplete-js', () => {
autocomplete<{ label: string }>({
container,
panelContainer,
openOnFocus: true,
getSources() {
return [
{
Expand Down Expand Up @@ -265,6 +353,7 @@ describe('autocomplete-js', () => {
autocomplete<{ label: string }>({
container,
panelContainer,
openOnFocus: true,
getSources() {
return [
{
Expand Down
5 changes: 5 additions & 0 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export function autocomplete<TItem extends BaseItem>(
optionsRef.current.shouldPanelShow ||
(({ state }) => {
const hasItems = getItemsCount(state) > 0;

if (!props.value.core.openOnFocus && !state.query) {
return hasItems;
}

const hasEmptyTemplate = Boolean(
hasEmptySourceTemplateRef.current ||
props.value.renderer.renderEmpty
Expand Down