2021-06-23 04:09:46 -04:00
|
|
|
import React from 'react'
|
2021-06-18 05:29:06 -04:00
|
|
|
import { Dropdown } from 'react-bootstrap'
|
|
|
|
import PropTypes from 'prop-types'
|
2021-06-23 04:09:46 -04:00
|
|
|
import useDropdown from '../hooks/use-dropdown'
|
2021-06-18 05:29:06 -04:00
|
|
|
|
|
|
|
export default function ControlledDropdown(props) {
|
2021-06-23 04:09:46 -04:00
|
|
|
const dropdownProps = useDropdown(Boolean(props.defaultOpen))
|
2021-06-18 05:29:06 -04:00
|
|
|
|
|
|
|
return (
|
2021-06-23 04:09:46 -04:00
|
|
|
<Dropdown {...props} {...dropdownProps}>
|
2021-06-18 05:29:06 -04:00
|
|
|
{React.Children.map(props.children, child => {
|
|
|
|
if (!React.isValidElement(child)) {
|
|
|
|
return child
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dropdown.Menu
|
|
|
|
if ('open' in child.props) {
|
2021-06-23 04:09:46 -04:00
|
|
|
return React.cloneElement(child, { open: dropdownProps.open })
|
2021-06-18 05:29:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overlay
|
|
|
|
if ('show' in child.props) {
|
2021-06-23 04:09:46 -04:00
|
|
|
return React.cloneElement(child, { show: dropdownProps.open })
|
2021-06-18 05:29:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// anything else
|
|
|
|
return React.cloneElement(child)
|
|
|
|
})}
|
|
|
|
</Dropdown>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
ControlledDropdown.propTypes = {
|
|
|
|
children: PropTypes.any,
|
|
|
|
defaultOpen: PropTypes.bool,
|
2022-09-13 09:57:47 -04:00
|
|
|
id: PropTypes.string,
|
2021-06-18 05:29:06 -04:00
|
|
|
}
|