import React from 'react'; import PropTypes from 'prop-types'; import Autosuggest from 'react-autosuggest'; import match from 'autosuggest-highlight/match'; import parse from 'autosuggest-highlight/parse'; import { withStyles } from '@material-ui/core/styles'; import { TextField, Paper, MenuItem } from '@material-ui/core'; const suggestions = [ { label: 'Afghanistan' }, { label: 'Aland Islands' }, { label: 'Albania' }, { label: 'Algeria' }, { label: 'American Samoa' }, { label: 'Andorra' }, { label: 'Angola' }, { label: 'Anguilla' }, { label: 'Antarctica' }, { label: 'Antigua and Barbuda' }, { label: 'Argentina' }, { label: 'Armenia' }, { label: 'Aruba' }, { label: 'Australia' }, { label: 'Austria' }, { label: 'Azerbaijan' }, { label: 'Bahamas' }, { label: 'Bahrain' }, { label: 'Bangladesh' }, { label: 'Barbados' }, { label: 'Belarus' }, { label: 'Belgium' }, { label: 'Belize' }, { label: 'Benin' }, { label: 'Bermuda' }, { label: 'Bhutan' }, { label: 'Bolivia, Plurinational State of' }, { label: 'Bonaire, Sint Eustatius and Saba' }, { label: 'Bosnia and Herzegovina' }, { label: 'Botswana' }, { label: 'Bouvet Island' }, { label: 'Brazil' }, { label: 'British Indian Ocean Territory' }, { label: 'Brunei Darussalam' }, ]; function renderInput(inputProps) { const { classes, ref, ...other } = inputProps; return ( ); } function renderSuggestion(suggestion, { query, isHighlighted }) { const matches = match(suggestion.label, query); const parts = parse(suggestion.label, matches); return ( {parts.map((part, index) => ( part.highlight ? ( {part.text} ) : ( {part.text} ) ))} ); } function renderSuggestionsContainer(options) { const { containerProps, children } = options; return ( {children} ); } function getSuggestionValue(suggestion) { return suggestion.label; } function getSuggestions(value) { const inputValue = value.trim().toLowerCase(); const inputLength = inputValue.length; let count = 0; return inputLength === 0 ? [] : suggestions.filter(suggestion => { const keep = count < 5 && suggestion.label.toLowerCase().slice(0, inputLength) === inputValue; if (keep) { count += 1; } return keep; }); } const styles = theme => ({ container: { flexGrow: 1, position: 'relative', height: 250, }, suggestionsContainerOpen: { position: 'absolute', zIndex: 1, marginTop: theme.spacing(1), left: 0, right: 0, }, suggestion: { display: 'block', }, suggestionsList: { margin: 0, padding: 0, listStyleType: 'none', }, }); class HighlightSuggest extends React.Component { state = { value: '', suggestions: [], }; handleSuggestionsFetchRequested = ({ value }) => { this.setState({ suggestions: getSuggestions(value), }); }; handleSuggestionsClearRequested = () => { this.setState({ suggestions: [], }); }; handleChange = (event, { newValue }) => { this.setState({ value: newValue, }); }; render() { const { classes } = this.props; return ( ); } } HighlightSuggest.propTypes = { classes: PropTypes.object.isRequired, }; export default withStyles(styles)(HighlightSuggest);