Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Context.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import type { IconType } from 'rc-tree/lib/interface';
import type { Key, LegacyDataNode } from './interface';
import type { FlattenDataNode, Key, LegacyDataNode, RawValueType } from './interface';
import type { SkipType } from './hooks/useKeyValueMapping';

interface ContextProps {
checkable: boolean | React.ReactNode;
Expand All @@ -19,6 +20,15 @@ interface ContextProps {
treeMotion: any;
loadData: (treeNode: LegacyDataNode) => Promise<unknown>;
onTreeLoad: (loadedKeys: Key[]) => void;

// Cache help content. These can be generated by parent component.
// Let's reuse this.
getEntityByKey: (key: Key, skipType?: SkipType, ignoreDisabledCheck?: boolean) => FlattenDataNode;
getEntityByValue: (
value: RawValueType,
skipType?: SkipType,
ignoreDisabledCheck?: boolean,
) => FlattenDataNode;
}

export const SelectContext = React.createContext<ContextProps>(null);
13 changes: 5 additions & 8 deletions src/OptionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import Tree from 'rc-tree';
import type { EventDataNode, ScrollTo } from 'rc-tree/lib/interface';
import type { FlattenDataNode, RawValueType, DataNode, TreeDataNode, Key } from './interface';
import { SelectContext } from './Context';
import useKeyValueMapping from './hooks/useKeyValueMapping';
import useKeyValueMap from './hooks/useKeyValueMap';

const HIDDEN_STYLE = {
width: 0,
Expand Down Expand Up @@ -91,6 +89,9 @@ const OptionList: React.RefForwardingComponent<
treeLoadedKeys,
treeMotion,
onTreeLoad,

getEntityByKey,
getEntityByValue,
} = React.useContext(SelectContext);

const treeRef = React.useRef<Tree>();
Expand All @@ -101,9 +102,6 @@ const OptionList: React.RefForwardingComponent<
(prev, next) => next[0] && prev[1] !== next[1],
);

const [cacheKeyMap, cacheValueMap] = useKeyValueMap(flattenOptions);
const [getEntityByKey, getEntityByValue] = useKeyValueMapping(cacheKeyMap, cacheValueMap);

// ========================== Values ==========================
const valueKeys = React.useMemo(
() =>
Expand Down Expand Up @@ -282,9 +280,8 @@ const OptionList: React.RefForwardingComponent<
);
};

const RefOptionList = React.forwardRef<ReviseRefOptionListProps, OptionListProps<DataNode[]>>(
OptionList,
);
const RefOptionList =
React.forwardRef<ReviseRefOptionListProps, OptionListProps<DataNode[]>>(OptionList);
RefOptionList.displayName = 'OptionList';

export default RefOptionList;
Loading