plugins.Header.SimpleHeader.Menu.NavItems.Dropdown.Dropdown.tsx Maven / Gradle / Ivy
The newest version!
import React, { useCallback, useContext, useState } from 'react'
import {
Dropdown as DropdownParent,
DropdownMenu,
DropdownItem,
DropdownToggle,
} from 'reactstrap'
import classNames from 'classnames'
import { LinkBody } from '../Links/LinkBody'
import { Dropdown as DropdownProps } from '../../Item'
import { FactoryComponent } from '../../../../../CommonMenuTypes'
import { ITEM_SRC } from '../../../../../constants'
import { FactoryContext } from '../../../../../../core/factory/context'
import { FactoryLevels } from '../../../../../../core/factory/factoryLevels'
import { ICON_POSITIONS } from '../../../../../../components/snippets/IconContainer/IconContainer'
export function Dropdown({
items,
active,
title,
className,
nested,
icon,
imageSrc,
imageShape = 'square',
direction = 'down',
recursiveClose = true,
onItemClick = () => {},
level = 0,
iconPosition = ICON_POSITIONS.LEFT,
}: DropdownProps) {
const [isOpen, setOpen] = useState(false)
const toggle = useCallback(() => setOpen(!isOpen), [isOpen])
const forceParentClose = useCallback(() => setOpen(false), [setOpen])
const { getComponent } = useContext(FactoryContext)
if (!items.length) { return null }
return (
0 })} caret>
{items.map((item) => {
const { items: nestedItems, title, src } = item
if (nestedItems) {
const onItemClick = () => {
if (recursiveClose) {
forceParentClose()
}
}
return (
)
}
const FactoryComponent: FactoryComponent = getComponent(src, FactoryLevels.HEADER_ITEMS)
if (!FactoryComponent) {
return null
}
return (
)
})
}
)
}