react-instantsearch
react-instantsearch copied to clipboard
Validate Algolia InstantSearch field with Formik, React
Hi,
I am integrating Algolia instant search into a form, I have a problem validating the instant search field using formik.
<Formik
initialValues={{
searchFieldValue: '',
}}
onSubmit={handleSubmit}
validationSchema={validationSchema}
setFieldTouched={('selectedDoctor', true, true)}
>
<Form style={{ maxWidth: '250px', margin: '50px' }}>
<Form.Item name="searchFieldValue" hasFeedback={true} showValidateSuccess={true}>
<AlgoliaSearch selectedValue={handleSelectedValue} />
</Form.Item>
</Form>
</Formik
**AlgoliaSearch.js**
const searchClient = algoliasearch('B1G2GM9NG0', 'aadef574be1f9252bb48d4ea09b5cfe5');
class AlgoliaSearch extends Component {
state = {
query: '',
};
onSuggestionSelected = (_, { suggestion }) => {
this.setState({
query: suggestion.name,
});
console.log('Setting suggestion.name: ' + suggestion.name);
this.props.selectedValue(suggestion.name);
};
onSuggestionCleared = () => {
this.setState({
query: '',
});
};
render() {
const { query } = this.state;
return (
<div>
{/* <h1>React InstantSearch - Results page with autocomplete</h1> */}
<InstantSearch indexName="demo_ecommerce" searchClient={searchClient}>
<Configure hitsPerPage={3} />
<Autocomplete
onSuggestionSelected={this.onSuggestionSelected}
onSuggestionCleared={this.onSuggestionCleared}
/>
</InstantSearch>
</div>
);
}
}
function Hit(props) {
return (
<div>
<Highlight attribute="name" hit={props.hit} />
</div>
);
}
Hit.propTypes = {
hit: PropTypes.object.isRequired,
};
export default AlgoliaSearch;
Hey, could you please demonstrate what's going wrong in this sandbox?
Hi! We're doing a round of cleanup before migrating this repository to the new InstantSearch monorepo. This issue seems not to have generated much activity lately, so we're going to close it, feel free to reopen if needed.