Skip to content
This repository has been archived by the owner on Jun 11, 2021. It is now read-only.

Commit

Permalink
feat(onInput): support onInput prop for controlled mode
Browse files Browse the repository at this point in the history
  • Loading branch information
francoischalifour committed Feb 21, 2020
1 parent 3cbedc5 commit 7345eb9
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,13 @@ autocomplete({

</details>

#### `onInput` 🚧
#### `onInput`

> `(params: { query: string, state: AutocompleteState, ...setters }) => void | Promise <void | { state: AutocompleteState }>`
> `(params: { query: string, props: AutocompleteOptions, state: AutocompleteState, ...setters }) => void | Promise <any>`
Called when the input changes.

This turns experience is "controlled" mode. You'll be in charge of updating the state with the [top-level API](#autocomplete).
This turns experience in "controlled" mode. You'll be in charge of updating the state.

#### `shouldDropdownShow`

Expand Down
23 changes: 19 additions & 4 deletions packages/autocomplete-core/src/onInput.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
AutocompleteStore,
AutocompleteSetters,
AutocompleteOptions,
AutocompleteSetters,
AutocompleteStore,
AutocompleteState,
} from './types';

let lastStalledId: number | null = null;

interface OnInputOptions<TItem> extends AutocompleteSetters<TItem> {
interface OnInputParams<TItem> extends AutocompleteSetters<TItem> {
query: string;
store: AutocompleteStore<TItem>;
props: AutocompleteOptions<TItem>;
Expand All @@ -32,7 +32,22 @@ export function onInput<TItem>({
setStatus,
setContext,
nextState = {},
}: OnInputOptions<TItem>): Promise<void> {
}: OnInputParams<TItem>): Promise<void> {
if (props.onInput) {
return Promise.resolve(
props.onInput({
query,
state: store.getState(),
setHighlightedIndex,
setQuery,
setSuggestions,
setIsOpen,
setStatus,
setContext,
})
);
}

if (lastStalledId) {
clearTimeout(lastStalledId);
}
Expand Down
13 changes: 13 additions & 0 deletions packages/autocomplete-core/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ interface OnSubmitParams<TItem> extends AutocompleteSetters<TItem> {
event: Event;
}

interface OnInputParams<TItem> extends AutocompleteSetters<TItem> {
query: string;
state: AutocompleteState<TItem>;
}

export interface PublicAutocompleteSource<TItem> {
/**
* Get the string value of the suggestion. The value is used to fill the search box.
Expand Down Expand Up @@ -209,6 +214,13 @@ export interface PublicAutocompleteOptions<TItem> {
* The function called when the autocomplete form is submitted.
*/
onSubmit?(params: OnSubmitParams<TItem>): void;
/**
* The function called when the input changes.
*
* This turns the experience in controlled mode, leaving you in charge of
* updating the state.
*/
onInput?(params: OnInputParams<TItem>): void | Promise<any>;
}

// Props manipulated internally with default values.
Expand All @@ -227,4 +239,5 @@ export interface AutocompleteOptions<TItem> {
navigator: Navigator<TItem>;
shouldDropdownShow(params: { state: AutocompleteState<TItem> }): boolean;
onSubmit(params: OnSubmitParams<TItem>): void;
onInput?(params: OnInputParams<TItem>): void | Promise<any>;
}
70 changes: 70 additions & 0 deletions stories/react.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,76 @@ storiesOf('React', module)
container
);

return container;
})
)
.add(
'Controlled mode',
withPlayground(({ container, dropdownContainer }) => {
render(
<Autocomplete
placeholder="Search items…"
dropdownContainer={dropdownContainer}
onInput={({
query,
setHighlightedIndex,
setQuery,
setSuggestions,
setIsOpen,
}) => {
setQuery(query);

if (query.length < 1) {
setHighlightedIndex(null);
setIsOpen(false);
setSuggestions([
{
source: {
getInputValue: ({ suggestion }) => suggestion.query,
getSuggestionUrl: () => undefined,
getSuggestions: () => [],
onSelect: () => {},
onHighlight: () => {},
},
items: [],
},
]);

return;
}

getAlgoliaHits({
searchClient,
queries: [
{
indexName: 'instant_search_demo_query_suggestions',
query,
},
],
}).then(items => {
setHighlightedIndex(null);
setIsOpen(items.length > 0);
setSuggestions([
{
source: {
getInputValue: ({ suggestion }) => suggestion.query,
getSuggestionUrl: () => undefined,
getSuggestions: () => items,
onSelect: () => {},
onHighlight: () => {},
},
items,
},
]);
});
}}
getSources={() => {
return [];
}}
/>,
container
);

return container;
})
);

0 comments on commit 7345eb9

Please sign in to comment.