mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
bb55fc2e32
GitOrigin-RevId: a16762139049aed1e309b1330602c3b291d41f81
35 lines
934 B
JavaScript
35 lines
934 B
JavaScript
import React from 'react'
|
|
import { Dropdown } from 'react-bootstrap'
|
|
import PropTypes from 'prop-types'
|
|
import useDropdown from '../hooks/use-dropdown'
|
|
|
|
export default function ControlledDropdown(props) {
|
|
const dropdownProps = useDropdown(Boolean(props.defaultOpen))
|
|
|
|
return (
|
|
<Dropdown {...props} {...dropdownProps}>
|
|
{React.Children.map(props.children, child => {
|
|
if (!React.isValidElement(child)) {
|
|
return child
|
|
}
|
|
|
|
// Dropdown.Menu
|
|
if ('open' in child.props) {
|
|
return React.cloneElement(child, { open: dropdownProps.open })
|
|
}
|
|
|
|
// Overlay
|
|
if ('show' in child.props) {
|
|
return React.cloneElement(child, { show: dropdownProps.open })
|
|
}
|
|
|
|
// anything else
|
|
return React.cloneElement(child)
|
|
})}
|
|
</Dropdown>
|
|
)
|
|
}
|
|
ControlledDropdown.propTypes = {
|
|
children: PropTypes.any,
|
|
defaultOpen: PropTypes.bool,
|
|
}
|