Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
feat(SearchBox): introduce autoFocus prop (#3599)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan authored Aug 22, 2022
1 parent 56adfad commit 99121b9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/hooks/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function App() {
]}
/>

<SearchBox placeholder="Search" />
<SearchBox placeholder="Search" autoFocus />

<div className="Search-header">
<PoweredBy />
Expand Down
7 changes: 6 additions & 1 deletion packages/react-instantsearch-hooks-web/src/ui/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export type SearchBoxProps = Omit<
> &
Pick<React.ComponentProps<'form'>, 'onSubmit'> &
Required<Pick<React.ComponentProps<'form'>, 'onReset'>> &
Pick<React.ComponentProps<'input'>, 'placeholder' | 'onChange'> & {
Pick<
React.ComponentProps<'input'>,
'placeholder' | 'onChange' | 'autoFocus'
> & {
inputRef: React.RefObject<HTMLInputElement>;
isSearchStalled: boolean;
value: string;
Expand Down Expand Up @@ -135,6 +138,7 @@ export function SearchBox({
onSubmit,
placeholder,
value,
autoFocus,
resetIconComponent: ResetIcon = DefaultResetIcon,
submitIconComponent: SubmitIcon = DefaultSubmitIcon,
loadingIconComponent: LoadingIcon = DefaultLoadingIcon,
Expand Down Expand Up @@ -190,6 +194,7 @@ export function SearchBox({
type="search"
value={value}
onChange={onChange}
autoFocus={autoFocus}
/>
<button
className={cx('ais-SearchBox-submit', classNames.submit)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/react';
import { render, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React, { createRef } from 'react';

Expand All @@ -20,6 +20,7 @@ describe('SearchBox', () => {
onSubmit,
placeholder: '',
value: '',
autoFocus: false,
translations: {
submitTitle: 'Submit the search query.',
resetTitle: 'Clear the search query.',
Expand Down Expand Up @@ -132,6 +133,8 @@ describe('SearchBox', () => {
</div>
</div>
`);

expect(within(container).getByRole('searchbox')).not.toHaveFocus();
});

test('renders with translations', () => {
Expand Down Expand Up @@ -322,6 +325,14 @@ describe('SearchBox', () => {
);
});

test('forwards the `autoFocus` prop to the input', () => {
const props = createProps({ autoFocus: true });

const { container } = render(<SearchBox {...props} />);

expect(within(container).getByRole('searchbox')).toHaveFocus();
});

test('with input value shows the reset button', () => {
const props = createProps({ value: 'query' });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ type UiProps = Pick<
| 'onReset'
| 'onSubmit'
| 'value'
| 'autoFocus'
| 'translations'
>;

export type SearchBoxProps = Omit<
SearchBoxUiComponentProps,
Exclude<keyof UiProps, 'onSubmit'>
Exclude<keyof UiProps, 'onSubmit' | 'autoFocus'>
> &
UseSearchBoxProps & {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, render, waitFor } from '@testing-library/react';
import { act, render, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';

Expand Down Expand Up @@ -113,6 +113,8 @@ describe('SearchBox', () => {
</div>
`);
});

expect(within(container).getByRole('searchbox')).not.toHaveFocus();
});

test('forwards placeholder prop', async () => {
Expand All @@ -130,6 +132,18 @@ describe('SearchBox', () => {
});
});

test('forwards `autoFocus` prop', async () => {
const { container } = render(
<InstantSearchHooksTestWrapper>
<SearchBox autoFocus />
</InstantSearchHooksTestWrapper>
);

await waitFor(() => {
expect(within(container).getByRole('searchbox')).toHaveFocus();
});
});

test('sets query on change event', async () => {
let lastUiState: UiState = {};

Expand Down

0 comments on commit 99121b9

Please sign in to comment.