Skip to content

Commit

Permalink
fix(react): fix CompositePanel
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Oct 14, 2021
1 parent 646e68e commit a364532
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions packages/react/src/panels/CompositePanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import { isValid } from '@designable/shared'
import cls from 'classnames'
import { IconWidget, TextWidget } from '../widgets'
Expand Down Expand Up @@ -46,26 +46,34 @@ const findItem = (
}
}

const getDefaultKey = (children: React.ReactNode) => {
const items = parseItems(children)
return items?.[0].key
}

export const CompositePanel: React.FC<ICompositePanelProps> & {
Item?: React.FC<ICompositePanelItemProps>
} = (props) => {
const prefix = usePrefix('composite-panel')
const [activeKey, setActiveKey] = useState<string | number>(
props.defaultActiveKey ?? 0
props.defaultActiveKey ?? getDefaultKey(props.children)
)
const activeKeyRef = useRef(null)
const [pinning, setPinning] = useState(props.defaultPinning ?? false)
const [visible, setVisible] = useState(props.defaultOpen ?? true)
const items = parseItems(props.children)
const currentItem = findItem(items, activeKey)
const content = currentItem?.children

activeKeyRef.current = activeKey

useEffect(() => {
if (isValid(props.activeKey)) {
if (props.activeKey !== activeKey) {
if (props.activeKey !== activeKeyRef.current) {
setActiveKey(props.activeKey)
}
}
}, [props.activeKey, activeKey])
}, [props.activeKey])

const renderContent = () => {
if (!content || !visible) return
Expand Down Expand Up @@ -147,22 +155,21 @@ export const CompositePanel: React.FC<ICompositePanelProps> & {
return (
<Comp
className={cls(prefix + '-tabs-pane', {
active: activeKey === index || activeKey === item.key,
active: activeKey === item.key,
})}
key={index}
href={item.href}
onClick={(e: any) => {
const key = item.key ?? index
if (shape === 'tab') {
if (activeKey === index || activeKey === item.key) {
if (activeKey === item.key) {
setVisible(!visible)
} else {
setVisible(true)
}
setActiveKey(key)
setActiveKey(item.key)
}
item.onClick?.(e)
props.onChange?.(key)
props.onChange?.(item.key)
}}
>
{takeTab()}
Expand Down

0 comments on commit a364532

Please sign in to comment.