-
-
Notifications
You must be signed in to change notification settings - Fork 241
feat(Tabs): support showSearch in more dropdown #992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EmilyyyLiu
wants to merge
9
commits into
react-component:master
Choose a base branch
from
EmilyyyLiu:feat/add-showSearch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+405
−10
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f22edac
feat(Tabs): support showSearch in more dropdown
186feeb
feat(Tabs): move ControlledDemo component to the top for better reada…
da9a598
feat(Tabs): add input support and keyboard navigation for search func…
20934ca
feat(Tabs): add tests for search value persistence and clearing on dr…
0187104
fix(OperationNode): remove click event propagation on dropdown close
d8ebc66
feat(README): update dropdown configuration descriptions and improve …
191e236
fix(Tabs.Overflow): adjust scrollIntoView implementation and update s…
644da59
feat(OperationNode): enhance search functionality with custom filter …
b2ba0b6
feat(OperationNode): add search input focus handling and refactor sea…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| title: Search Dropdown | ||
| nav: | ||
| title: Demo | ||
| path: /demo | ||
| --- | ||
|
|
||
| <code src="../examples/search-dropdown.tsx"></code> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import React, { useState } from 'react'; | ||
| import '../../assets/index.less'; | ||
| import Tabs from '../../src'; | ||
|
|
||
| // Controlled mode example | ||
| const ControlledDemo = ({ items }: { items: any[] }) => { | ||
| const [searchValue, setSearchValue] = useState(''); | ||
|
|
||
| return ( | ||
| <Tabs | ||
| activeKey="1" | ||
| onChange={() => {}} | ||
| items={items} | ||
| more={{ | ||
| showSearch: { | ||
| placeholder: 'Controlled search...', | ||
| searchValue, | ||
| onSearch: setSearchValue, | ||
| }, | ||
| }} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default () => { | ||
| const [activeKey, setActiveKey] = useState('1'); | ||
|
|
||
| // Generate many tabs to trigger the "more" button | ||
| const items = Array.from({ length: 30 }, (_, i) => ({ | ||
| key: String(i + 1), | ||
| label: `Tab ${i + 1}`, | ||
| children: `Content of Tab ${i + 1}`, | ||
| })); | ||
|
|
||
| return ( | ||
| <div> | ||
| <h3>Basic Usage</h3> | ||
| <Tabs | ||
| activeKey={activeKey} | ||
| onChange={setActiveKey} | ||
| items={items} | ||
| more={{ | ||
| showSearch: { | ||
| placeholder: 'Search...', | ||
| }, | ||
| }} | ||
| /> | ||
|
|
||
| <h3>Controlled Mode</h3> | ||
| <ControlledDemo items={items} /> | ||
|
|
||
| <h3>Keep Search Value on Close</h3> | ||
| <Tabs | ||
| activeKey={activeKey} | ||
| onChange={setActiveKey} | ||
| items={items} | ||
| more={{ | ||
| showSearch: { | ||
| placeholder: 'Keep search value', | ||
| autoClearSearchValue: false, | ||
| }, | ||
| }} | ||
| /> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import Dropdown from '@rc-component/dropdown'; | |
| import Menu, { MenuItem } from '@rc-component/menu'; | ||
| import { KeyCode } from '@rc-component/util'; | ||
| import * as React from 'react'; | ||
| import { useEffect, useState } from 'react'; | ||
| import { useEffect, useRef, useState } from 'react'; | ||
| import type { EditableConfig, Tab, TabsLocale, MoreProps } from '../interface'; | ||
| import { getRemovable } from '../util'; | ||
| import AddButton from './AddButton'; | ||
|
|
@@ -39,6 +39,7 @@ const OperationNode = React.forwardRef<HTMLDivElement, OperationNodeProps>((prop | |
| tabs, | ||
| locale, | ||
| mobile, | ||
| activeKey, | ||
| more: moreProps = {}, | ||
| style, | ||
| className, | ||
|
|
@@ -56,8 +57,36 @@ const OperationNode = React.forwardRef<HTMLDivElement, OperationNodeProps>((prop | |
| // ======================== Dropdown ======================== | ||
| const [open, setOpen] = useState(false); | ||
| const [selectedKey, setSelectedKey] = useState<string>(null); | ||
| const [searchValue, setSearchValue] = useState(''); | ||
| const searchInputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| const { icon: moreIcon = 'More' } = moreProps; | ||
| const { icon: moreIcon = 'More', showSearch } = moreProps; | ||
|
|
||
| const isSearchable = !!showSearch; | ||
| const showSearchConfig = typeof showSearch === 'object' ? showSearch : {}; | ||
| const { | ||
| placeholder = 'Search', | ||
| onSearch, | ||
| searchValue: controlledSearchValue, | ||
| autoClearSearchValue = true, | ||
| filter: filterOption, | ||
| } = showSearchConfig; | ||
|
|
||
| const mergedSearchValue = | ||
| controlledSearchValue !== undefined ? controlledSearchValue : searchValue; | ||
| const setSearchValueFn = controlledSearchValue !== undefined ? () => {} : setSearchValue; | ||
|
|
||
| const filteredTabs = mergedSearchValue | ||
| ? tabs.filter(tab => { | ||
| if (filterOption) { | ||
| return filterOption(tab, mergedSearchValue); | ||
| } | ||
| if (typeof tab.label === 'string') { | ||
| return tab.label.toLowerCase().includes(mergedSearchValue.toLowerCase()); | ||
| } | ||
| return false; | ||
| }) | ||
| : tabs; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| const popupId = `${id}-more-popup`; | ||
| const dropdownPrefix = `${prefixCls}-dropdown`; | ||
|
|
@@ -85,7 +114,7 @@ const OperationNode = React.forwardRef<HTMLDivElement, OperationNodeProps>((prop | |
| selectedKeys={[selectedKey]} | ||
| aria-label={dropdownAriaLabel !== undefined ? dropdownAriaLabel : 'expanded dropdown'} | ||
| > | ||
| {tabs.map<React.ReactNode>(tab => { | ||
| {filteredTabs.map<React.ReactNode>(tab => { | ||
| const { closable, disabled, closeIcon, key, label } = tab; | ||
| const removable = getRemovable(closable, closeIcon, editable, disabled); | ||
| return ( | ||
|
|
@@ -120,10 +149,12 @@ const OperationNode = React.forwardRef<HTMLDivElement, OperationNodeProps>((prop | |
| ); | ||
|
|
||
| function selectOffset(offset: -1 | 1) { | ||
| const enabledTabs = tabs.filter(tab => !tab.disabled); | ||
| const enabledTabs = filteredTabs.filter(tab => !tab.disabled); | ||
| let selectedIndex = enabledTabs.findIndex(tab => tab.key === selectedKey) || 0; | ||
| const len = enabledTabs.length; | ||
|
|
||
| if (len === 0) return; | ||
|
|
||
| for (let i = 0; i < len; i += 1) { | ||
| selectedIndex = (selectedIndex + offset + len) % len; | ||
| const tab = enabledTabs[selectedIndex]; | ||
|
|
@@ -166,20 +197,63 @@ const OperationNode = React.forwardRef<HTMLDivElement, OperationNodeProps>((prop | |
| } | ||
| } | ||
|
|
||
| // 搜索框 | ||
| const searchInput = isSearchable ? ( | ||
| <div className={`${dropdownPrefix}-search`}> | ||
| <input | ||
| ref={searchInputRef} | ||
| type="text" | ||
| placeholder={placeholder} | ||
| value={mergedSearchValue} | ||
| onChange={e => { | ||
| const value = e.target.value; | ||
| setSearchValueFn(value); | ||
| onSearch?.(value); | ||
| }} | ||
| onKeyDown={e => { | ||
| if (e.key === 'ArrowDown') { | ||
| e.preventDefault(); | ||
| selectOffset(1); | ||
| } else if (e.key === 'ArrowUp') { | ||
| e.preventDefault(); | ||
| selectOffset(-1); | ||
| } else if (e.key === 'Enter' && selectedKey) { | ||
| e.preventDefault(); | ||
| onTabClick(selectedKey, e); | ||
| setOpen(false); | ||
| } | ||
| }} | ||
| /> | ||
| </div> | ||
| ) : null; | ||
|
Comment on lines
+201
to
+228
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为了提升用户体验和无障碍支持(Accessibility),建议在搜索框中加入以下改进:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 自动聚焦已经完成,esc 本身支持 |
||
|
|
||
| // ========================= Effect ========================= | ||
| useEffect(() => { | ||
| // We use query element here to avoid React strict warning | ||
| const ele = document.getElementById(selectedItemId); | ||
| if (ele?.scrollIntoView) { | ||
| ele.scrollIntoView(false); | ||
| ele.scrollIntoView({ block: 'center', behavior: 'smooth' }); | ||
| } | ||
| }, [selectedItemId, selectedKey]); | ||
|
|
||
| useEffect(() => { | ||
| if (!open) { | ||
| if (open) { | ||
| if (!selectedKey && activeKey) { | ||
| setSelectedKey(activeKey); | ||
| } | ||
|
|
||
| if (isSearchable) { | ||
| requestAnimationFrame(() => { | ||
| searchInputRef.current?.focus(); | ||
| }); | ||
| } | ||
| } else { | ||
| setSelectedKey(null); | ||
| if (autoClearSearchValue && controlledSearchValue === undefined) { | ||
| setSearchValue(''); | ||
| } | ||
| } | ||
| }, [open]); | ||
| }, [open, activeKey, isSearchable, autoClearSearchValue, controlledSearchValue]); | ||
|
|
||
| // ========================= Render ========================= | ||
| const moreStyle: React.CSSProperties = { | ||
|
|
@@ -193,18 +267,29 @@ const OperationNode = React.forwardRef<HTMLDivElement, OperationNodeProps>((prop | |
|
|
||
| const overlayClassName = clsx(popupClassName, { [`${dropdownPrefix}-rtl`]: rtl }); | ||
|
|
||
| const dropdownContent = isSearchable ? ( | ||
| <div className={`${dropdownPrefix}-container`}> | ||
| {searchInput} | ||
| {menu} | ||
| </div> | ||
| ) : ( | ||
| menu | ||
| ); | ||
|
|
||
| const { showSearch: _s, ...dropdownProps } = moreProps; | ||
|
|
||
| const moreNode: React.ReactNode = mobile ? null : ( | ||
| <Dropdown | ||
| prefixCls={dropdownPrefix} | ||
| overlay={menu} | ||
| overlay={dropdownContent} | ||
| visible={tabs.length ? open : false} | ||
| onVisibleChange={setOpen} | ||
| overlayClassName={overlayClassName} | ||
| overlayStyle={popupStyle} | ||
| mouseEnterDelay={0.1} | ||
| mouseLeaveDelay={0.1} | ||
| getPopupContainer={getPopupContainer} | ||
| {...moreProps} | ||
| {...dropdownProps} | ||
| > | ||
| <button | ||
| type="button" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在
rc-tabs中,tab.label的类型是React.ReactNode。如果用户传入了非字符串的 React 节点(例如包含图标或 HTML 标签的<span>Tab 1</span>),直接使用String(tab.label)会得到"[object Object]",导致搜索过滤失效,甚至在搜索 "object" 时错误地匹配到所有这类 Tab。建议实现一个简单的文本提取函数,或者在过滤时安全地处理非字符串类型的
label。