Created
August 31, 2017 03:14
-
-
Save ericchernuka/88cfe9c071cccf2225b847d1b14fbe8c to your computer and use it in GitHub Desktop.
Downshift Boostrap
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 Downshift from 'downshift' | |
export default class Dropdown extends React.Component { | |
render() { | |
const { children, onChange } = this.props | |
return ( | |
<Downshift onChange={onChange}> | |
{({ | |
getInputProps, | |
getButtonProps, | |
getItemProps, | |
isOpen, | |
inputValue, | |
selectedItem, | |
highlightedIndex | |
}) => ( | |
<div className="dropdown"> | |
<button | |
className="btn btn-secondary dropdown-toggle" | |
type="button" | |
aria-haspopup="true" | |
aria-expanded={isOpen} | |
id="dropdownMenuLink" | |
{...getButtonProps()} | |
> | |
Dropdown button | |
</button> | |
{isOpen ? ( | |
<div className="dropdown-menu show" aria-labelledby="dropdownMenuLink"> | |
{React.Children.map(children, (child, index) => { | |
const { props: childProps } = child | |
const item = childProps.value | |
return ( | |
React.cloneElement(child, { | |
...getItemProps({ | |
key: item, | |
isFocused: highlightedIndex === index, | |
index, | |
item, | |
}), | |
}) | |
) | |
})} | |
</div> | |
) : null} | |
</div> | |
)} | |
</Downshift> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment