Created
January 13, 2019 22:07
-
-
Save converge/2c737c87ac2e6a81c0c11b39f45ef6f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
import deburr from 'lodash/deburr'; | |
import Downshift from 'downshift'; | |
import { withStyles } from '@material-ui/core/styles'; | |
import TextField from '@material-ui/core/TextField'; | |
import Paper from '@material-ui/core/Paper'; | |
import MenuItem from '@material-ui/core/MenuItem'; | |
import Grid from '@material-ui/core/Grid'; | |
import { compose } from 'redux'; | |
import { connect } from 'react-redux' | |
import { firestoreConnect } from 'react-redux-firebase' | |
const suggestions = [ | |
{ name: 'Afghanistan' }, | |
{ name: 'Aland Islands' }, | |
{ name: 'Albania' }, | |
{ name: 'Algeria' }, | |
{ name: 'American Samoa' }, | |
{ name: 'Andorra' }, | |
{ name: 'Angola' }, | |
{ name: 'Anguilla' }, | |
{ name: 'Antarctica' }, | |
{ name: 'Antigua and Barbuda' }, | |
{ name: 'Argentina' }, | |
{ name: 'Armenia' }, | |
{ name: 'Aruba' }, | |
{ name: 'Australia' }, | |
{ name: 'Austria' }, | |
{ name: 'Azerbaijan' }, | |
{ name: 'Bahamas' }, | |
{ name: 'Bahrain' }, | |
{ name: 'Bangladesh' }, | |
{ name: 'Barbados' }, | |
{ name: 'Belarus' }, | |
{ name: 'Belgium' }, | |
{ name: 'Belize' }, | |
{ name: 'Benin' }, | |
{ name: 'Bermuda' }, | |
{ name: 'Bhutan' }, | |
{ name: 'Bolivia, Plurinational State of' }, | |
{ name: 'Bonaire, Sint Eustatius and Saba' }, | |
{ name: 'Bosnia and Herzegovina' }, | |
{ name: 'Botswana' }, | |
{ name: 'Bouvet Island' }, | |
{ name: 'Brazil' }, | |
{ name: 'British Indian Ocean Territory' }, | |
{ name: 'Brunei Darussalam' }, | |
]; | |
function renderInput(inputProps) { | |
const { InputProps, classes, ref, ...other } = inputProps; | |
return ( | |
<TextField | |
InputProps={{ | |
inputRef: ref, | |
classes: { | |
root: classes.inputRoot, | |
input: classes.inputInput, | |
}, | |
...InputProps, | |
}} | |
{...other} | |
/> | |
); | |
} | |
function renderSuggestion({ suggestion, index, itemProps, highlightedIndex, selectedItem }) { | |
const isHighlighted = highlightedIndex === index; | |
const isSelected = (selectedItem || '').indexOf(suggestion.name) > -1; | |
return ( | |
<MenuItem | |
{...itemProps} | |
key={suggestion.name} | |
selected={isHighlighted} | |
component="div" | |
style={{ | |
fontWeight: isSelected ? 500 : 400, | |
}} | |
> | |
{suggestion.name} | |
</MenuItem> | |
); | |
} | |
renderSuggestion.propTypes = { | |
highlightedIndex: PropTypes.number, | |
index: PropTypes.number, | |
itemProps: PropTypes.object, | |
selectedItem: PropTypes.string, | |
suggestion: PropTypes.shape({ name: PropTypes.string }).isRequired, | |
}; | |
function getSuggestions(value) { | |
const inputValue = deburr(value.trim()).toLowerCase(); | |
const inputLength = inputValue.length; | |
let count = 0; | |
return inputLength === 0 | |
? [] | |
: suggestions.filter(suggestion => { | |
const keep = count < 5 && suggestion.name.slice(0, inputLength).toLowerCase() === inputValue; | |
if (keep) { | |
count += 1; | |
} | |
return keep; | |
}); | |
} | |
const styles = theme => ({ | |
root: { | |
flexGrow: 0, | |
width: 500, | |
height: 125, | |
align: 'center', | |
justify: 'center', | |
paddingTop: 90, | |
}, | |
container: { | |
flexGrow: 1, | |
position: 'relative', | |
}, | |
paper: { | |
position: 'absolute', | |
zIndex: 1, | |
marginTop: theme.spacing.unit, | |
left: 0, | |
right: 0, | |
}, | |
chip: { | |
margin: `${theme.spacing.unit / 2}px ${theme.spacing.unit / 4}px`, | |
}, | |
inputRoot: { | |
flexWrap: 'wrap', | |
}, | |
inputInput: { | |
width: 'auto', | |
flexGrow: 1, | |
}, | |
divider: { | |
height: theme.spacing.unit * 2, | |
}, | |
}); | |
function Search(props) { | |
const { classes } = props | |
const { books } = props | |
console.log('books', books) | |
return ( | |
<Grid | |
container | |
direction="row" | |
justify="center" | |
alignItems="center" | |
> | |
<div className={classes.root}> | |
<Downshift id="downshift-simple"> | |
{({ | |
getInputProps, | |
getItemProps, | |
getMenuProps, | |
highlightedIndex, | |
inputValue, | |
isOpen, | |
selectedItem, | |
}) => ( | |
<div className={classes.container}> | |
<label>Book Search</label> | |
{renderInput({ | |
fullWidth: true, | |
classes, | |
InputProps: getInputProps({ | |
placeholder: 'title...', | |
}), | |
})} | |
<div {...getMenuProps()}> | |
{isOpen ? ( | |
<Paper className={classes.paper} square> | |
{getSuggestions(inputValue).map((suggestion, index) => | |
renderSuggestion({ | |
suggestion, | |
index, | |
itemProps: getItemProps({ item: suggestion.name }), | |
highlightedIndex, | |
selectedItem, | |
}), | |
)} | |
</Paper> | |
) : null} | |
</div> | |
</div> | |
)} | |
</Downshift> | |
<div className={classes.divider} /> | |
</div> | |
</Grid> | |
); | |
} | |
Search.propTypes = { | |
classes: PropTypes.object.isRequired, | |
}; | |
const mapStateToProps = (state) => { | |
return { | |
books: state.firestore.ordered.books | |
} | |
} | |
export default compose( | |
withStyles(styles), | |
connect(mapStateToProps), | |
firestoreConnect([ | |
{ collection: 'books' } | |
]) | |
)(Search) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment