Skip to content

Commit

Permalink
feat(js): provide setters and refresh to render API (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour authored May 31, 2021
1 parent dfa503d commit 3e78566
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
42 changes: 42 additions & 0 deletions packages/autocomplete-js/src/__tests__/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,48 @@ describe('render', () => {
});
});

test('provides the scoped API', () => {
const container = document.createElement('div');
const panelContainer = document.createElement('div');

document.body.appendChild(panelContainer);
autocomplete<{ label: string }>({
container,
panelContainer,
initialState: {
isOpen: true,
},
getSources() {
return [
{
sourceId: 'testSource',
getItems() {
return [{ label: '1' }];
},
templates: {
item({ item }) {
return item.label;
},
},
},
];
},
render(params) {
expect(params).toEqual(
expect.objectContaining({
refresh: expect.any(Function),
setActiveItemId: expect.any(Function),
setCollections: expect.any(Function),
setContext: expect.any(Function),
setIsOpen: expect.any(Function),
setQuery: expect.any(Function),
setStatus: expect.any(Function),
})
);
},
});
});

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');
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export function renderPanel<TItem extends BaseItem>(
createElement,
Fragment,
components,
...autocompleteScopeApi,
},
dom.panel
);
Expand Down
4 changes: 2 additions & 2 deletions packages/autocomplete-js/src/types/AutocompleteRender.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BaseItem } from '@algolia/autocomplete-core';
import { AutocompleteScopeApi, BaseItem } from '@algolia/autocomplete-core';

import { AutocompleteComponents } from './AutocompleteComponents';
import { Pragma, PragmaFrag, VNode } from './AutocompleteRenderer';
import { AutocompleteState } from './AutocompleteState';

export type AutocompleteRender<TItem extends BaseItem> = (
params: {
params: AutocompleteScopeApi<TItem> & {
children: VNode;
state: AutocompleteState<TItem>;
sections: VNode[];
Expand Down

0 comments on commit 3e78566

Please sign in to comment.