I have a Dropdown component from semantic-ui React (https://react.semantic-ui.com/modules/dropdown/).
Currently, the Dropdown populates with data from an API call that returns:
[
{href: 'https://blah blah.com',
text: 'text to display in the dropdown'}
]
There is text that is displayed as items in the Dropdown that are links to external webpages. I am able to type into the search, click on the item in the list, and I am taken to the link for that particular dropdown item. What I would like to do is be able to search for the item and click the Enter key to select the item and go to the link for that item. Does anyone know how I can do this? Currently, pressing Enter will just select the item and display it in the dropdown search, without actually triggering the link to the item.
Here is my code:
import React, { Component } from "react";
import { Dropdown } from "semantic-ui-react";
const snippets = [];
class SearchableDropdown extends Component {
state = {
success: false,
textValue: "",
topic: this.props.topic,
data: [],
};
async componentDidMount() {
let topics = window.location.pathname;
const res = await fetch(`http://localhost:5000/api${topics}`);
const jsonData = await res.json();
const info = jsonData;
const data = Object.keys(info).map((key) => {
return {
topic: info[key].topic,
card_title: info[key].card_title,
card_subtitle: info[key].card_subtitle,
card_gif: info[key].card_gif,
card_link: info[key].card_link,
};
});
this.setState({ data });
let topicInfo = this.state.data;
topicInfo.forEach((item) =>
snippets.push({
text: `${item.card_title} | ${item.card_subtitle}`,
value: `${item.card_title} | ${item.card_subtitle}`,
href: `${item.card_link}`,
})
);
}
}
render() {
let topic = this.props.topic;
return (
<Dropdown
placeholder="Search Snippets"
fluid
search
selection
options={snippets}
onChange={this.handleChange}
style={{
backgroundColor: "whitesmoke",
border: "1px solid black",
display: "block",
margin: "auto",
width: "50%",
marginTop: "30px",
}}
/>
);
}
}
export default SearchableDropdown;