Skip to content

Commit 92eeb3e

Browse files
authored
fix(js): ignore empty template with no query and openOnFocus (#407)
1 parent 9651481 commit 92eeb3e

2 files changed

Lines changed: 88 additions & 14 deletions

File tree

packages/autocomplete-js/src/__tests__/autocomplete.test.ts

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ describe('autocomplete-js', () => {
166166
autocomplete<{ label: string }>({
167167
container,
168168
panelContainer,
169+
openOnFocus: true,
169170
getSources() {
170171
return [
171172
{
@@ -192,12 +193,86 @@ describe('autocomplete-js', () => {
192193
await waitFor(() => {
193194
expect(
194195
panelContainer.querySelector<HTMLElement>('.aa-Panel')
195-
).toBeInTheDocument();
196+
).toHaveTextContent('No results template');
196197
});
198+
});
197199

198-
expect(
199-
panelContainer.querySelector<HTMLElement>('.aa-Panel')
200-
).toHaveTextContent('No results template');
200+
test("doesn't render empty template on no query when openOnFocus is false", async () => {
201+
const container = document.createElement('div');
202+
const panelContainer = document.createElement('div');
203+
204+
document.body.appendChild(panelContainer);
205+
autocomplete<{ label: string }>({
206+
container,
207+
panelContainer,
208+
openOnFocus: false,
209+
getSources() {
210+
return [
211+
{
212+
getItems() {
213+
return [];
214+
},
215+
templates: {
216+
item({ item }) {
217+
return item.label;
218+
},
219+
empty() {
220+
return 'No results template';
221+
},
222+
},
223+
},
224+
];
225+
},
226+
});
227+
228+
const input = container.querySelector<HTMLInputElement>('.aa-Input');
229+
230+
fireEvent.input(input, { target: { value: '' } });
231+
232+
await waitFor(() => {
233+
expect(
234+
panelContainer.querySelector<HTMLElement>('.aa-Panel')
235+
).not.toBeInTheDocument();
236+
});
237+
});
238+
239+
test('render empty template on query when openOnFocus is false', async () => {
240+
const container = document.createElement('div');
241+
const panelContainer = document.createElement('div');
242+
243+
document.body.appendChild(panelContainer);
244+
autocomplete<{ label: string }>({
245+
container,
246+
panelContainer,
247+
openOnFocus: true,
248+
getSources() {
249+
return [
250+
{
251+
getItems() {
252+
return [];
253+
},
254+
templates: {
255+
item({ item }) {
256+
return item.label;
257+
},
258+
empty() {
259+
return 'No results template';
260+
},
261+
},
262+
},
263+
];
264+
},
265+
});
266+
267+
const input = container.querySelector<HTMLInputElement>('.aa-Input');
268+
269+
fireEvent.input(input, { target: { value: 'Query' } });
270+
271+
await waitFor(() => {
272+
expect(
273+
panelContainer.querySelector<HTMLElement>('.aa-Panel')
274+
).toHaveTextContent('No results template');
275+
});
201276
});
202277

203278
test('calls renderEmpty without empty template on no results', async () => {
@@ -214,6 +289,7 @@ describe('autocomplete-js', () => {
214289
autocomplete<{ label: string }>({
215290
container,
216291
panelContainer,
292+
openOnFocus: true,
217293
getSources() {
218294
return [
219295
{
@@ -238,7 +314,7 @@ describe('autocomplete-js', () => {
238314
await waitFor(() => {
239315
expect(
240316
panelContainer.querySelector<HTMLElement>('.aa-Panel')
241-
).toBeInTheDocument();
317+
).toHaveTextContent('No results render');
242318
});
243319

244320
expect(renderEmpty).toHaveBeenCalledWith(
@@ -251,10 +327,6 @@ describe('autocomplete-js', () => {
251327
},
252328
expect.any(HTMLElement)
253329
);
254-
255-
expect(
256-
panelContainer.querySelector<HTMLElement>('.aa-Panel')
257-
).toHaveTextContent('No results render');
258330
});
259331

260332
test('renders empty template over renderEmpty method on no results', async () => {
@@ -265,6 +337,7 @@ describe('autocomplete-js', () => {
265337
autocomplete<{ label: string }>({
266338
container,
267339
panelContainer,
340+
openOnFocus: true,
268341
getSources() {
269342
return [
270343
{
@@ -297,12 +370,8 @@ describe('autocomplete-js', () => {
297370
await waitFor(() => {
298371
expect(
299372
panelContainer.querySelector<HTMLElement>('.aa-Panel')
300-
).toBeInTheDocument();
373+
).toHaveTextContent('No results template');
301374
});
302-
303-
expect(
304-
panelContainer.querySelector<HTMLElement>('.aa-Panel')
305-
).toHaveTextContent('No results template');
306375
});
307376

308377
test('allows user-provided shouldPanelShow', () => {

packages/autocomplete-js/src/autocomplete.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ export function autocomplete<TItem extends BaseItem>(
5252
optionsRef.current.shouldPanelShow ||
5353
(({ state }) => {
5454
const hasItems = getItemsCount(state) > 0;
55+
56+
if (!props.value.core.openOnFocus && !state.query) {
57+
return hasItems;
58+
}
59+
5560
const hasEmptyTemplate = Boolean(
5661
hasEmptySourceTemplateRef.current ||
5762
props.value.renderer.renderEmpty

0 commit comments

Comments
 (0)