Skip to content

Commit

Permalink
chore: remove unnecessary logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aojunhao123 committed Nov 6, 2024
1 parent 5d29cc0 commit 4640c4d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 35 deletions.
8 changes: 8 additions & 0 deletions examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class Demo extends React.Component {
console.log('onPopupScroll:', evt.target);
}}
/>

<h2>single select (just select children)</h2>
<TreeSelect
style={{ width: 300 }}
Expand All @@ -232,6 +233,7 @@ class Demo extends React.Component {
filterTreeNode={false}
onChange={this.onChangeChildren}
/>

<h2>multiple select</h2>
<TreeSelect
style={{ width: 300 }}
Expand All @@ -247,6 +249,7 @@ class Demo extends React.Component {
onSelect={this.onSelect}
allowClear
/>

<h2>check select</h2>
<TreeSelect
open
Expand Down Expand Up @@ -278,6 +281,7 @@ class Demo extends React.Component {
return `${valueList.length} rest...`;
}}
/>

<h2>labelInValue & show path</h2>
<TreeSelect
style={{ width: 500 }}
Expand All @@ -295,6 +299,7 @@ class Demo extends React.Component {
filterTreeNode={false}
onChange={this.onChangeLV}
/>

<h2>use treeDataSimpleMode</h2>
<TreeSelect
style={{ width: 300 }}
Expand All @@ -318,6 +323,7 @@ class Demo extends React.Component {
this.onSelect(...args);
}}
/>

<h2>Testing in extreme conditions (Boundary conditions test) </h2>
<TreeSelect
style={{ width: 200 }}
Expand All @@ -341,6 +347,7 @@ class Demo extends React.Component {
]}
onChange={(val, ...args) => console.log(val, ...args)}
/>

<h2>use TreeNode Component (not recommend)</h2>
<TreeSelect
style={{ width: 200 }}
Expand Down Expand Up @@ -377,6 +384,7 @@ class Demo extends React.Component {
</TreeNode>
<TreeNode value="same value3" title="same title" key="0-3" />
</TreeSelect>

<h2>title render</h2>
<TreeSelect<{ label: string }>
open
Expand Down
55 changes: 20 additions & 35 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import LegacyContext from './LegacyContext';
import TreeSelectContext from './TreeSelectContext';
import type { Key, SafeKey } from './interface';
import { getAllKeys, isCheckDisabled } from './utils/valueUtil';
import { flattenTreeData } from 'rc-tree/lib/utils/treeUtil';

const HIDDEN_STYLE = {
width: 0,
Expand Down Expand Up @@ -77,10 +76,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
(prev, next) => next[0] && prev[1] !== next[1],
);

// ========================== Active Key ==========================
const [activeKey, setActiveKey] = React.useState<Key>(null);
const activeEntity = keyEntities[activeKey as SafeKey];

// ========================== Values ==========================
const mergedCheckedKeys = React.useMemo(() => {
if (!checkable) {
Expand All @@ -93,7 +88,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
};
}, [checkable, checkedKeys, halfCheckedKeys]);

// ========================== Scroll Effect ==========================
// ========================== Scroll ==========================
React.useEffect(() => {
// Single mode should scroll to current key
if (open && !multiple && checkedKeys.length) {
Expand Down Expand Up @@ -123,15 +118,6 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
}
};

// ========================== Search ==========================
const lowerSearchValue = String(searchValue).toLowerCase();
const filterTreeNode = (treeNode: EventDataNode<any>) => {
if (!lowerSearchValue) {
return false;
}
return String(treeNode[treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue);
};

// =========================== Keys ===========================
const [expandedKeys, setExpandedKeys] = React.useState<Key[]>(treeDefaultExpandedKeys);
const [searchExpandedKeys, setSearchExpandedKeys] = React.useState<Key[]>(null);
Expand All @@ -152,7 +138,15 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
}
};

// ========================== Search Effect ==========================
// ========================== Search ==========================
const lowerSearchValue = String(searchValue).toLowerCase();
const filterTreeNode = (treeNode: EventDataNode<any>) => {
if (!lowerSearchValue) {
return false;
}
return String(treeNode[treeNodeFilterProp]).toLowerCase().includes(lowerSearchValue);
};

React.useEffect(() => {
if (searchValue) {
setSearchExpandedKeys(getAllKeys(treeData, fieldNames));
Expand All @@ -161,25 +155,14 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
}, [searchValue]);

// ========================== Get First Selectable Node ==========================
const getFirstMatchingNode = (
nodes: EventDataNode<any>[],
searchVal?: string,
): EventDataNode<any> | null => {
const getFirstMatchingNode = (nodes: EventDataNode<any>[]): EventDataNode<any> | null => {
for (const node of nodes) {
if (node.disabled || node.selectable === false) {
continue;
}

if (searchVal) {
if (filterTreeNode(node)) {
return node;
}
} else {
if (!node.disabled && node.selectable !== false) {
return node;
}

if (node[fieldNames.children]) {
const matchInChildren = getFirstMatchingNode(node[fieldNames.children], searchVal);
const matchInChildren = getFirstMatchingNode(node[fieldNames.children]);
if (matchInChildren) {
return matchInChildren;
}
Expand All @@ -188,16 +171,18 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
return null;
};

// ========================== Active Key Effect ==========================
// ========================== Active ==========================
const [activeKey, setActiveKey] = React.useState<Key>(null);
const activeEntity = keyEntities[activeKey as SafeKey];

React.useEffect(() => {
if (!open) {
setActiveKey(null);
return;
}

// Prioritize activating the searched node
// // Prioritize activating the searched node
if (searchValue) {
const firstNode = getFirstMatchingNode(treeData, searchValue);
const firstNode = getFirstMatchingNode(memoTreeData);
setActiveKey(firstNode ? firstNode[fieldNames.value] : null);
return;
}
Expand All @@ -209,7 +194,7 @@ const OptionList: React.ForwardRefRenderFunction<ReviseRefOptionListProps> = (_,
}

// If no search value and no checked nodes, activate the first node
const firstNode = getFirstMatchingNode(treeData);
const firstNode = getFirstMatchingNode(memoTreeData);
setActiveKey(firstNode ? firstNode[fieldNames.value] : null);
}, [open, searchValue]);

Expand Down

0 comments on commit 4640c4d

Please sign in to comment.