Created
March 18, 2018 05:50
-
-
Save cauldyclark15/adcde8ed02cd729d6372a66b9dc62954 to your computer and use it in GitHub Desktop.
This file contains 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 Downshift from 'downshift'; | |
import { | |
InputWrapper, | |
InputMainWrapper, | |
ClearButtonWrapper, | |
} from './styles/fiddleStyles'; | |
import CategoriesDropdown from './CategoriesDropdown'; | |
import XIcon from './partials/XIcon'; | |
const propTypes = { | |
items: PropTypes.array, | |
}; | |
const defaultProps = { | |
items: [], | |
}; | |
const Autocomplete = ({ items, onInputChange, input, ...rest }) => { | |
const { onChange, onDragStart, onDrop, onFocus } = input; | |
return ( | |
<Downshift onChange={onInputChange} {...rest}> | |
{({ | |
getInputProps, | |
getItemProps, | |
getRootProps, | |
inputValue, | |
selectedItem, | |
highlightedIndex, | |
isOpen, | |
clearSelection, | |
}) => { | |
return ( | |
<div className="input-wrapper"> | |
<InputMainWrapper | |
{...getInputProps({ | |
placeholder: 'What are you looking for?', | |
type: 'search', | |
required: true, | |
id: 'downshift-input', | |
onChange, | |
onDragStart, | |
onDrop, | |
onFocus, | |
})} | |
/> | |
{isOpen ? ( | |
<CategoriesDropdown | |
{...{ | |
items, | |
inputValue, | |
selectedItem, | |
highlightedIndex, | |
getItemProps, | |
}} | |
/> | |
) : null} | |
{selectedItem && ( | |
<ClearButtonWrapper onClick={() => clearSelection()}> | |
<label> | |
<div> | |
<XIcon /> | |
</div> | |
</label> | |
</ClearButtonWrapper> | |
)} | |
</div> | |
); | |
}} | |
</Downshift> | |
); | |
}; | |
Autocomplete.propTypes = propTypes; | |
Autocomplete.defaultProps = defaultProps; | |
export default Autocomplete; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment