|
| 1 | +import React, { useState, useMemo } from 'react'; |
| 2 | +import Tabs from '../../src'; |
| 3 | +import type { TabsProps, PopupRender } from '../../src'; |
| 4 | +import Menu, { MenuItem as MenuItemNode } from '@rc-component/menu'; |
| 5 | + |
| 6 | +const items: TabsProps['items'] = []; |
| 7 | + |
| 8 | +for (let i = 0; i < 15; i += 1) { |
| 9 | + items.push({ key: String(i), label: `Tab ${i + 1}`, children: `Content of ${i}` }); |
| 10 | +} |
| 11 | + |
| 12 | +export default () => { |
| 13 | + const [activeKey, setActiveKey] = useState('0'); |
| 14 | + const [searchValue, setSearchValue] = useState(''); |
| 15 | + |
| 16 | + // popupRender 回调:使用 info.tabs 重新渲染菜单 |
| 17 | + const popupRender: PopupRender = (menu, { tabs, activeKey: selectedKey, onClose }) => { |
| 18 | + // 过滤 tabs |
| 19 | + const filteredTabs = useMemo(() => { |
| 20 | + if (!searchValue) return tabs; |
| 21 | + return tabs.filter(tab => |
| 22 | + String(tab.label).toLowerCase().includes(searchValue.toLowerCase()), |
| 23 | + ); |
| 24 | + }, [tabs, searchValue]); |
| 25 | + |
| 26 | + // 检查当前 activeKey 是否在过滤后的列表中 |
| 27 | + const isActiveKeyInFiltered = filteredTabs.some(t => t.key === selectedKey); |
| 28 | + const selectedKeys = isActiveKeyInFiltered ? [selectedKey] : []; |
| 29 | + |
| 30 | + return ( |
| 31 | + <div style={{ padding: '8px' }}> |
| 32 | + <input |
| 33 | + placeholder="搜索 Tab..." |
| 34 | + value={searchValue} |
| 35 | + onChange={e => setSearchValue(e.target.value)} |
| 36 | + style={{ |
| 37 | + width: '100%', |
| 38 | + padding: '4px 8px', |
| 39 | + marginBottom: '8px', |
| 40 | + border: '1px solid #d9d9d9', |
| 41 | + borderRadius: '4px', |
| 42 | + outline: 'none', |
| 43 | + boxSizing: 'border-box', |
| 44 | + }} |
| 45 | + onClick={e => e.stopPropagation()} |
| 46 | + /> |
| 47 | + <Menu |
| 48 | + selectedKeys={selectedKeys} |
| 49 | + onClick={({ key }) => { |
| 50 | + setActiveKey(key); |
| 51 | + onClose(); |
| 52 | + }} |
| 53 | + > |
| 54 | + {filteredTabs.map(tab => ( |
| 55 | + <MenuItemNode key={tab.key} disabled={tab.disabled}> |
| 56 | + {tab.label} |
| 57 | + </MenuItemNode> |
| 58 | + ))} |
| 59 | + </Menu> |
| 60 | + </div> |
| 61 | + ); |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <div style={{ maxWidth: 550 }}> |
| 66 | + <Tabs |
| 67 | + activeKey={activeKey} |
| 68 | + onChange={setActiveKey} |
| 69 | + items={items} |
| 70 | + more={{ |
| 71 | + popupRender, |
| 72 | + }} |
| 73 | + /> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +}; |
0 commit comments