Skip to content

Commit

Permalink
feat(js): pass elements record to render (#490)
Browse files Browse the repository at this point in the history
This adds an API to easily retrieve generated UI elements to manipulate the panel UI.

```
import { render } from 'preact';

autocomplete({
  // ...
  plugins: [recentSearchesPlugin, querySuggestionsPlugin],
  getSources({ query }) {
    return [
      {
        sourceId: 'products',
        // ...
      },
    ];
  },
  render({ elements }, root) {
    const { recentSearchesPlugin, querySuggestionsPlugin, products } = elements;

    render(
      <div className="aa-PanelLayout">
        <div>
          {recentSearchesPlugin}
          {querySuggestionsPlugin}
        </div>
        <div>{products}</div>
      </div>,
      root
    );
  },
});
```
  • Loading branch information
francoischalifour committed Mar 8, 2021
1 parent 4c97375 commit a50712e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ describe('autocomplete-js', () => {
state: expect.anything(),
children: expect.anything(),
sections: expect.any(Array),
elements: expect.any(Object),
createElement: expect.anything(),
Fragment: expect.anything(),
},
Expand Down
9 changes: 8 additions & 1 deletion packages/autocomplete-js/src/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ export function renderPanel<TItem extends BaseItem>(
const children = (
<div className="aa-PanelLayout aa-Panel--Scrollable">{sections}</div>
);
const elements = sections.reduce((acc, current) => {
acc[current.props['data-autocomplete-source-id']] = current;
return acc;
}, {});

render({ children, state, sections, createElement, Fragment }, dom.panel);
render(
{ children, state, sections, elements, createElement, Fragment },
dom.panel
);
}
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/types/AutocompleteRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type AutocompleteRender<TItem extends BaseItem> = (
children: VNode;
state: AutocompleteState<TItem>;
sections: VNode[];
elements: Record<string, VNode>;
createElement: Pragma;
Fragment: PragmaFrag;
},
Expand Down

0 comments on commit a50712e

Please sign in to comment.