Skip to content

Commit 1b04fd0

Browse files
refactor(ui): enhance CustomCollapse and ToolsCallingIcon components
- Refactored CustomCollapse to utilize Accordion and AccordionItem from HeroUI, simplifying props and improving functionality. - Updated ToolsCallingIcon to accept TooltipProps for better customization. - Revised stories for CustomCollapse to reflect new prop structure and added examples for various use cases. - Cleaned up unnecessary props and improved documentation in story files.
1 parent 76b3ba5 commit 1b04fd0

File tree

6 files changed

+204
-265
lines changed

6 files changed

+204
-265
lines changed

packages/ui/src/components/base/CustomCollapse/index.tsx

Lines changed: 22 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,42 @@
1-
import { Accordion, AccordionItem } from '@heroui/react'
1+
import { Accordion, AccordionItem, type AccordionItemProps, type AccordionProps } from '@heroui/react'
22
import type { FC } from 'react'
3-
import { memo, useEffect, useState } from 'react'
3+
import { memo } from 'react'
44

55
// 重新导出 HeroUI 的组件,方便直接使用
66
export { Accordion, AccordionItem } from '@heroui/react'
77

88
interface CustomCollapseProps {
9-
label: React.ReactNode
10-
extra?: React.ReactNode
119
children: React.ReactNode
12-
destroyInactivePanel?: boolean
13-
defaultActiveKey?: string[]
14-
activeKey?: string[]
15-
collapsible?: 'header' | 'icon' | 'disabled'
16-
onChange?: (activeKeys: string | string[]) => void
17-
style?: React.CSSProperties
18-
classNames?: {
19-
trigger?: string
20-
content?: string
21-
}
22-
className?: string
23-
variant?: 'light' | 'shadow' | 'bordered' | 'splitted'
10+
accordionProps?: Omit<AccordionProps, 'children'>
11+
accordionItemProps?: Omit<AccordionItemProps, 'children'>
2412
}
2513

26-
const CustomCollapse: FC<CustomCollapseProps> = ({
27-
label,
28-
extra,
29-
children,
30-
defaultActiveKey = ['1'],
31-
activeKey,
32-
collapsible,
33-
onChange,
34-
style,
35-
classNames,
36-
className = '',
37-
variant = 'bordered'
38-
}) => {
39-
const [expandedKeys, setExpandedKeys] = useState<Set<string>>(() => {
40-
if (activeKey !== undefined) {
41-
return new Set(activeKey)
42-
}
43-
return new Set(defaultActiveKey)
44-
})
14+
const CustomCollapse: FC<CustomCollapseProps> = ({ children, accordionProps = {}, accordionItemProps = {} }) => {
15+
// 解构 Accordion 的 props
16+
const {
17+
defaultExpandedKeys = ['1'],
18+
variant = 'bordered',
19+
className = '',
20+
isDisabled = false,
21+
...restAccordionProps
22+
} = accordionProps
4523

46-
useEffect(() => {
47-
if (activeKey !== undefined) {
48-
setExpandedKeys(new Set(activeKey))
49-
}
50-
}, [activeKey])
51-
52-
const handleSelectionChange = (keys: 'all' | Set<React.Key>) => {
53-
if (keys === 'all') return
54-
55-
const stringKeys = Array.from(keys).map((key) => String(key))
56-
const newExpandedKeys = new Set(stringKeys)
57-
58-
if (activeKey === undefined) {
59-
setExpandedKeys(newExpandedKeys)
60-
}
61-
62-
onChange?.(stringKeys.length === 1 ? stringKeys[0] : stringKeys)
63-
}
64-
65-
const isDisabled = collapsible === 'disabled'
24+
// 解构 AccordionItem 的 props
25+
const { title = 'Collapse Panel', ...restAccordionItemProps } = accordionItemProps
6626

6727
return (
6828
<Accordion
69-
className={className}
70-
style={style}
29+
defaultExpandedKeys={defaultExpandedKeys}
7130
variant={variant}
72-
defaultExpandedKeys={activeKey === undefined ? defaultActiveKey : undefined}
73-
selectedKeys={activeKey !== undefined ? expandedKeys : undefined}
74-
onSelectionChange={handleSelectionChange}
31+
className={className}
7532
isDisabled={isDisabled}
76-
selectionMode="multiple">
33+
selectionMode="multiple"
34+
{...restAccordionProps}>
7735
<AccordionItem
7836
key="1"
79-
aria-label={typeof label === 'string' ? label : 'collapse-item'}
80-
title={label}
81-
startContent={extra}
82-
classNames={{
83-
trigger: classNames?.trigger ?? '',
84-
content: classNames?.content ?? ''
85-
}}>
37+
aria-label={typeof title === 'string' ? title : 'collapse-item'}
38+
title={title}
39+
{...restAccordionItemProps}>
8640
{children}
8741
</AccordionItem>
8842
</Accordion>

packages/ui/src/components/icons/ToolsCallingIcon/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
// Original: src/renderer/src/components/Icons/ToolsCallingIcon.tsx
2-
import { Tooltip } from '@heroui/react'
2+
import { Tooltip, type TooltipProps } from '@heroui/react'
33
import { Wrench } from 'lucide-react'
44
import React from 'react'
5-
import { useTranslation } from 'react-i18next'
65

76
import { cn } from '../../../utils'
87

98
interface ToolsCallingIconProps extends React.HTMLAttributes<HTMLDivElement> {
109
className?: string
1110
iconClassName?: string
11+
TooltipProps?: TooltipProps
1212
}
1313

14-
const ToolsCallingIcon = ({ className, iconClassName, ...props }: ToolsCallingIconProps) => {
15-
const { t } = useTranslation()
16-
14+
const ToolsCallingIcon = ({ className, iconClassName, TooltipProps, ...props }: ToolsCallingIconProps) => {
1715
return (
1816
<div className={cn('flex justify-center items-center', className)} {...props}>
19-
<Tooltip content={t('models.function_calling')} placement="top">
17+
<Tooltip placement="top" {...TooltipProps}>
2018
<Wrench className={cn('w-4 h-4 mr-1.5 text-[#00b96b]', iconClassName)} />
2119
</Tooltip>
2220
</div>

0 commit comments

Comments
 (0)