overleaf/services/web/frontend/js/shared/components/controlled-dropdown.js
Alexandre Bourdin a0fabee3b4 Merge pull request #9245 from overleaf/integration-project-dashboard-react-migration
[Integration branch] Project Dashboard React Migration

GitOrigin-RevId: 3c3db39109a8137c57995f5f7c0ff8c800f04c4e
2022-09-14 08:04:03 +00:00

36 lines
958 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,
id: PropTypes.string,
}