How to add submit button for SearchField #6495
-
I have implemented SearchField to replace an existing form in an app, and I cannot work out how to add a submit button. All the SearchField handlers work correctly and I can press enter to search. When wrapping inside Form, and adding a button, the Form onSubmit handler does not return the value in the search field. I have found one example in the docs which has the Button and the SearchField in a Form, but there is no indication of how to handle the onSubmit Button event correctly. The example in StoryBook wraps the SearchField and Button in an element . Also no help. :-( |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I think this will address your issue when it's released #6404 |
Beta Was this translation helpful? Give feedback.
-
Thanks @snowystinger. I am not sure. I had my SearchForm wrapped like this (irrelevant bit omitted): import { Button, Form, Input, Label, SearchField } from 'react-aria-components';
<Form onSubmit={onFormSubmit}>
<SearchField>
<Label className="hidden">{label}</Label>
<Input
placeholder={placeholder}
/>
<Button
type="submit"
>
<span className="hidden">Search</span>
<SearchIcon className="w-5 h-5" />
</Button>
</SearchField>
</Form> All that does is fire the onSubmit handler on the the |
Beta Was this translation helpful? Give feedback.
-
Thanks. Putting an outline of the code here for others. I have done this: <Form onSubmit={onFormSubmit}>
<SearchField>
<Label className="hidden">{label}</Label>
<Input
placeholder={placeholder}
/>
</SearchField>
<Button
type="submit"
>
<span className="hidden">Search</span>
<SearchIcon className="w-5 h-5" />
</Button>
</Form> In that case I can attach a separate event handler to the Form, for the key press, and process the data that way. Interestingly the docs say the event is I was then able to handle the event this way: const onFormSubmit = (event: SyntheticBaseEvent) => {
event.preventDefault();
console.log(event.target[0].value);
}; |
Beta Was this translation helpful? Give feedback.
The button you've passed to the SearchField will act as a clear button. https://react-spectrum.adobe.com/react-aria/SearchField.html#anatomy
You could put the submit button outside the SearchField and style the two together to look like a single field?