import React from 'react';
import PropTypes from 'prop-types';
import Downshift from 'downshift';
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 {
InputProps,
classes,
ref,
...other
} = inputProps;
return (
);
}
function renderSuggestion({
suggestion,
index,
itemProps,
highlightedIndex,
selectedItem
}) {
const isHighlighted = highlightedIndex === index;
const isSelected = (selectedItem || '').indexOf(suggestion.label) > -1;
return (
);
}
renderSuggestion.propTypes = {
highlightedIndex: PropTypes.number.isRequired,
index: PropTypes.number.isRequired,
itemProps: PropTypes.object.isRequired,
selectedItem: PropTypes.string.isRequired,
suggestion: PropTypes.shape({ label: PropTypes.string }).isRequired,
};
function getSuggestions(inputValue) {
let count = 0;
return suggestions.filter(suggestion => {
const keep = (!inputValue || suggestion.label.toLowerCase().indexOf(inputValue.toLowerCase()) !== -1)
&& count < 5;
if (keep) {
count += 1;
}
return keep;
});
}
const styles = theme => ({
root: {
flexGrow: 1,
height: 100,
},
container: {
flexGrow: 1,
position: 'relative',
},
paper: {
position: 'absolute',
zIndex: 1,
marginTop: theme.spacing(1),
left: 0,
right: 0,
},
chip: {
margin: `${theme.spacing(0.5)}px ${theme.spacing(0.25)}px`,
},
inputRoot: {
flexWrap: 'wrap',
},
});
function AutoSuggest(props) {
const { classes } = props;
return (
{({
getInputProps,
getItemProps,
isOpen,
inputValue,
selectedItem,
highlightedIndex
}) => (
{renderInput({
fullWidth: true,
classes,
InputProps: getInputProps({
placeholder: 'Search a country (start with a)',
id: 'integration-downshift-simple',
}),
})}
{isOpen ? (
{getSuggestions(inputValue).map((suggestion, index) => renderSuggestion({
suggestion,
index,
itemProps: getItemProps({ item: suggestion.label }),
highlightedIndex,
selectedItem,
}),
)}
) : null}
)}
);
}
AutoSuggest.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(AutoSuggest);