Skip to content

Instantly share code, notes, and snippets.

@cauldyclark15
Created March 18, 2018 05:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cauldyclark15/adcde8ed02cd729d6372a66b9dc62954 to your computer and use it in GitHub Desktop.
Save cauldyclark15/adcde8ed02cd729d6372a66b9dc62954 to your computer and use it in GitHub Desktop.
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