Skip to content

Instantly share code, notes, and snippets.

@ericchernuka
Created August 31, 2017 03:14
Show Gist options
  • Save ericchernuka/88cfe9c071cccf2225b847d1b14fbe8c to your computer and use it in GitHub Desktop.
Save ericchernuka/88cfe9c071cccf2225b847d1b14fbe8c to your computer and use it in GitHub Desktop.
Downshift Boostrap
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