mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-15 14:43:46 +01:00
Add useDropdown hook (#4228)
GitOrigin-RevId: a16762139049aed1e309b1330602c3b291d41f81
This commit is contained in:
@@ -1,25 +1,13 @@
|
||||
import React, { useCallback, useState } from 'react'
|
||||
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 [open, setOpen] = useState(Boolean(props.defaultOpen))
|
||||
|
||||
const handleClick = useCallback(event => {
|
||||
event.stopPropagation()
|
||||
}, [])
|
||||
|
||||
const handleToggle = useCallback(value => {
|
||||
setOpen(value)
|
||||
}, [])
|
||||
const dropdownProps = useDropdown(Boolean(props.defaultOpen))
|
||||
|
||||
return (
|
||||
<Dropdown
|
||||
{...props}
|
||||
open={open}
|
||||
onToggle={handleToggle}
|
||||
onClick={handleClick}
|
||||
>
|
||||
<Dropdown {...props} {...dropdownProps}>
|
||||
{React.Children.map(props.children, child => {
|
||||
if (!React.isValidElement(child)) {
|
||||
return child
|
||||
@@ -27,12 +15,12 @@ export default function ControlledDropdown(props) {
|
||||
|
||||
// Dropdown.Menu
|
||||
if ('open' in child.props) {
|
||||
return React.cloneElement(child, { open })
|
||||
return React.cloneElement(child, { open: dropdownProps.open })
|
||||
}
|
||||
|
||||
// Overlay
|
||||
if ('show' in child.props) {
|
||||
return React.cloneElement(child, { show: open })
|
||||
return React.cloneElement(child, { show: dropdownProps.open })
|
||||
}
|
||||
|
||||
// anything else
|
||||
|
||||
Reference in New Issue
Block a user