Skip to content

Commit

Permalink
feat(core): remove onInput option (#437)
Browse files Browse the repository at this point in the history
The `onInput` user param was originally introduced to enable controlled mode, but we've managed to create all the search experiences we wanted without this option. We can therefore remove it before public release.

BREAKING CHANGE
  • Loading branch information
francoischalifour authored Feb 9, 2021
1 parent 7cfdd55 commit 3827605
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 44 deletions.
40 changes: 22 additions & 18 deletions packages/autocomplete-core/src/__tests__/getInputProps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,14 @@ describe('getInputProps', () => {
});
});

test('calls onInput and onSelect without item URL', async () => {
test('calls getSources and onSelect without item URL', async () => {
const onSelect = jest.fn();
const onInput = jest.fn();
const getSources = jest.fn(() => [
createSource({
onSelect,
getItems: () => [{ label: '1' }, { label: '2' }],
}),
]);
const navigator = createNavigator();
const {
inputElement,
Expand All @@ -1082,30 +1087,26 @@ describe('getInputProps', () => {
setActiveItemId,
setStatus,
} = createPlayground(createAutocomplete, {
onInput,
navigator,
defaultActiveItemId: 0,
initialState: {
isOpen: true,
collections: [
createCollection({
source: { onSelect },
items: [{ label: '1' }, { label: '2' }],
}),
],
},
getSources,
});

inputElement.focus();
userEvent.type(inputElement, '{enter}');
userEvent.type(inputElement, 'a');
await runAllMicroTasks();

expect(getSources).toHaveBeenCalledTimes(1);

expect(onInput).toHaveBeenCalledTimes(1);
userEvent.type(inputElement, '{enter}');
await runAllMicroTasks();

expect(getSources).toHaveBeenCalledTimes(2);
expect(onSelect).toHaveBeenCalledTimes(1);
expect(onSelect).toHaveBeenCalledWith({
event: expect.any(KeyboardEvent),
item: { label: '1' },
itemInputValue: '',
item: expect.objectContaining({ label: '1' }),
itemInputValue: 'a',
itemUrl: undefined,
refresh,
setCollections,
Expand All @@ -1118,14 +1119,17 @@ describe('getInputProps', () => {
state: {
collections: [
{
items: [{ label: '1' }, { label: '2' }],
items: [
expect.objectContaining({ label: '1' }),
expect.objectContaining({ label: '2' }),
],
source: expect.any(Object),
},
],
completion: null,
context: {},
isOpen: false,
query: '',
query: 'a',
activeItemId: 0,
status: 'idle',
},
Expand Down
11 changes: 0 additions & 11 deletions packages/autocomplete-core/src/onInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ export function onInput<TItem extends BaseItem>({
store,
...setters
}: OnInputParams<TItem>): Promise<void> {
if (props.onInput) {
return Promise.resolve(
props.onInput({
query,
refresh,
state: store.getState(),
...setters,
})
);
}

if (lastStalledId) {
props.environment.clearTimeout(lastStalledId);
}
Expand Down
7 changes: 0 additions & 7 deletions packages/autocomplete-core/src/types/AutocompleteOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,6 @@ export interface AutocompleteOptions<TItem extends BaseItem> {
* The function called when the Autocomplete form is reset.
*/
onReset?(params: OnResetParams<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;
/**
* The array of plugins.
*/
Expand Down
8 changes: 0 additions & 8 deletions packages/website/docs/partials/createAutocomplete-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ The function called when the Autocomplete form is submitted.
The function called when the Autocomplete form is reset.

### `onInput`

> `(params: { query: string, state: AutocompleteState, ...setters }) => void`
The function called when the input changes.

This turns the experience in controlled mode, leaving you in charge of updating the state.

### `debug`

> `boolean` | defaults to `false`
Expand Down

0 comments on commit 3827605

Please sign in to comment.