Skip to content

Commit 66ff929

Browse files
[CDX-223] Add search button functionality to focus input (#235)
1 parent 3a9f902 commit 66ff929

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

spec/Components/CioAutocomplete/CioAutocomplete.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,32 @@ describe('CioAutocomplete Client-Side Rendering', () => {
225225
});
226226
});
227227
});
228+
229+
describe('Search button behavior', () => {
230+
it('Focuses the input when search button is clicked with empty input', () => {
231+
render(<CioAutocomplete apiKey={DEMO_API_KEY} onSubmit={() => {}} />);
232+
233+
const searchInput = screen.getByRole('combobox') as HTMLInputElement;
234+
const searchButton = screen.getByTestId('cio-submit-btn');
235+
236+
expect(searchInput).not.toHaveFocus();
237+
238+
fireEvent.click(searchButton);
239+
240+
expect(searchInput).toHaveFocus();
241+
});
242+
243+
it('Submits the form when search button is clicked with non-empty input', () => {
244+
const mockOnSubmit = jest.fn();
245+
render(<CioAutocomplete apiKey={DEMO_API_KEY} onSubmit={mockOnSubmit} />);
246+
247+
const searchInput = screen.getByRole('combobox');
248+
const searchButton = screen.getByTestId('cio-submit-btn');
249+
250+
fireEvent.change(searchInput, { target: { value: 'test query' } });
251+
fireEvent.click(searchButton);
252+
253+
expect(mockOnSubmit).toHaveBeenCalled();
254+
});
255+
});
228256
});

src/components/Autocomplete/SearchInput/SearchInput.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ function DefaultRenderInput({ getFormProps, getInputProps, getLabelProps, setQue
4242
<button
4343
className='cio-submit-btn'
4444
data-testid='cio-submit-btn'
45-
disabled={!inputProps.value}
4645
data-cnstrc-search-submit-btn
46+
onClick={(e) => {
47+
if (!inputProps.value) {
48+
e.preventDefault();
49+
if (inputProps.id) {
50+
document.getElementById(inputProps.id)?.focus();
51+
}
52+
}
53+
}}
4754
type='submit'
4855
aria-label='Submit Search'>
4956
<div className='cio-icon'>

src/styles.css

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@
3131
cursor: pointer;
3232
}
3333

34-
.cio-autocomplete button:disabled {
35-
cursor: not-allowed;
36-
}
3734

3835
.cio-autocomplete .cio-submit-btn {
3936
right: -21px;

0 commit comments

Comments
 (0)