Skip to content

Commit

Permalink
feat(js): display reset button only when query
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Dec 2, 2020
1 parent 2e967be commit 1656530
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('autocomplete-js', () => {
</label>
<button
class="aa-ResetButton"
hidden=""
type="reset"
>
<svg
Expand Down
29 changes: 29 additions & 0 deletions packages/autocomplete-js/src/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ export function autocomplete<TItem extends BaseItem>({
};
});

runEffect(() => {
const panelRoot = getHTMLElement(panelContainer);
const state: AutocompleteState<TItem> = {
collections: [],
completion: null,
context: {},
isOpen: false,
query: '',
selectedItemId: null,
status: 'idle',
...props.initialState,
};
render(renderer, {
state,
...autocomplete,
classNames,
panelRoot,
root,
form,
input,
inputWrapper,
label,
panel,
resetButton,
});

return () => {};
});

runEffect(() => {
const panelRoot = getHTMLElement(panelContainer);
const unmountRef = createRef<(() => void) | undefined>(undefined);
Expand Down
4 changes: 3 additions & 1 deletion packages/autocomplete-js/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ export function render<TItem extends BaseItem>(
panelRoot,
root,
input,
resetButton,
panel,
}: RenderProps<TItem>
): () => void {
setPropertiesWithoutEvents(root, getRootProps());
setPropertiesWithoutEvents(root, getRootProps({}));
setPropertiesWithoutEvents(resetButton, { hidden: !state.query });
setPropertiesWithoutEvents(input, getInputProps({ inputElement: input }));

panel.innerHTML = '';
Expand Down
1 change: 1 addition & 0 deletions packages/autocomplete-js/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export interface AutocompleteOptions<TItem extends BaseItem>
* ```
*/
render?: AutocompleteRenderer<TItem>;
initialState?: Partial<AutocompleteState<TItem>>;
}

export interface AutocompleteApi<TItem extends BaseItem>
Expand Down

0 comments on commit 1656530

Please sign in to comment.