Skip to content

Commit

Permalink
fix: fix tree key ts type (ant-design#23348)
Browse files Browse the repository at this point in the history
* fix tree key ts type

* revert snap
  • Loading branch information
yoyo837 authored Apr 17, 2020
1 parent 5e7f3cc commit c70b49a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
16 changes: 8 additions & 8 deletions components/tree/DirectoryTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classNames from 'classnames';
import omit from 'omit.js';
import debounce from 'lodash/debounce';
import { conductExpandParent } from 'rc-tree/lib/util';
import { EventDataNode, DataNode } from 'rc-tree/lib/interface';
import { EventDataNode, DataNode, Key } from 'rc-tree/lib/interface';
import { convertDataToEntities, convertTreeToData } from 'rc-tree/lib/utils/treeUtil';
import FileOutlined from '@ant-design/icons/FileOutlined';
import FolderOpenOutlined from '@ant-design/icons/FolderOpenOutlined';
Expand All @@ -20,8 +20,8 @@ export interface DirectoryTreeProps extends TreeProps {
}

export interface DirectoryTreeState {
expandedKeys?: string[];
selectedKeys?: string[];
expandedKeys?: Key[];
selectedKeys?: Key[];
}

function getIcon(props: AntdTreeNodeAttribute): React.ReactNode {
Expand Down Expand Up @@ -60,9 +60,9 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta
onDebounceExpand: (event: React.MouseEvent<HTMLElement>, node: EventDataNode) => void;

// Shift click usage
lastSelectedKey?: string;
lastSelectedKey?: Key;

cachedSelectedKeys?: string[];
cachedSelectedKeys?: Key[];

constructor(props: DirectoryTreeProps) {
super(props);
Expand Down Expand Up @@ -93,7 +93,7 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta
}

onExpand = (
expandedKeys: string[],
expandedKeys: Key[],
info: {
node: EventDataNode;
expanded: boolean;
Expand Down Expand Up @@ -139,7 +139,7 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta
};

onSelect = (
keys: string[],
keys: Key[],
event: {
event: 'select';
selected: boolean;
Expand Down Expand Up @@ -167,7 +167,7 @@ class DirectoryTree extends React.Component<DirectoryTreeProps, DirectoryTreeSta
const shiftPick: boolean = nativeEvent.shiftKey;

// Generate new selected keys
let newSelectedKeys: string[];
let newSelectedKeys: Key[];
if (multiple && ctrlPick) {
// Control click
newSelectedKeys = keys;
Expand Down
22 changes: 11 additions & 11 deletions components/tree/Tree.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import RcTree, { TreeNode, TreeProps as RcTreeProps } from 'rc-tree';
import classNames from 'classnames';
import { DataNode } from 'rc-tree/lib/interface';
import { DataNode, Key } from 'rc-tree/lib/interface';

import DirectoryTree from './DirectoryTree';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface AntTreeNodeProps {
disabled?: boolean;
disableCheckbox?: boolean;
title?: string | React.ReactNode;
key?: string;
key?: Key;
eventKey?: string;
isLeaf?: boolean;
checked?: boolean;
Expand Down Expand Up @@ -76,13 +76,13 @@ export interface AntTreeNodeMouseEvent {
}

export interface AntTreeNodeDragEnterEvent extends AntTreeNodeMouseEvent {
expandedKeys: string[];
expandedKeys: Key[];
}

export interface AntTreeNodeDropEvent {
node: AntTreeNode;
dragNode: AntTreeNode;
dragNodesKeys: string[];
dragNodesKeys: Key[];
dropPosition: number;
dropToGap?: boolean;
event: React.MouseEvent<HTMLElement>;
Expand All @@ -109,21 +109,21 @@ export interface TreeProps extends Omit<RcTreeProps, 'prefixCls'> {
/** 默认展开对应树节点 */
defaultExpandParent?: boolean;
/** 默认展开指定的树节点 */
defaultExpandedKeys?: string[];
defaultExpandedKeys?: Key[];
/** (受控)展开指定的树节点 */
expandedKeys?: string[];
expandedKeys?: Key[];
/** (受控)选中复选框的树节点 */
checkedKeys?: string[] | { checked: string[]; halfChecked: string[] };
checkedKeys?: Key[] | { checked: Key[]; halfChecked: Key[] };
/** 默认选中复选框的树节点 */
defaultCheckedKeys?: string[];
defaultCheckedKeys?: Key[];
/** (受控)设置选中的树节点 */
selectedKeys?: string[];
selectedKeys?: Key[];
/** 默认选中的树节点 */
defaultSelectedKeys?: string[];
defaultSelectedKeys?: Key[];
selectable?: boolean;
/** 点击树节点触发 */
filterAntTreeNode?: (node: AntTreeNode) => boolean;
loadedKeys?: string[];
loadedKeys?: Key[];
/** 设置节点可拖拽(IE>8) */
draggable?: boolean;
style?: React.CSSProperties;
Expand Down
24 changes: 12 additions & 12 deletions components/tree/utils/dictUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DataNode } from 'rc-tree/lib/interface';
import { DataNode, Key } from 'rc-tree/lib/interface';

enum Record {
None,
Expand All @@ -8,7 +8,7 @@ enum Record {

function traverseNodesKey(
treeData: DataNode[],
callback: (key: string | number | null, node: DataNode) => boolean,
callback: (key: Key | number | null, node: DataNode) => boolean,
) {
function processNode(dataNode: DataNode) {
const { key, children } = dataNode;
Expand All @@ -23,11 +23,11 @@ function traverseNodesKey(
/** 计算选中范围,只考虑expanded情况以优化性能 */
export function calcRangeKeys(
treeData: DataNode[],
expandedKeys: string[],
startKey?: string,
endKey?: string,
): string[] {
const keys: string[] = [];
expandedKeys: Key[],
startKey?: Key,
endKey?: Key,
): Key[] {
const keys: Key[] = [];
let record: Record = Record.None;

if (startKey && startKey === endKey) {
Expand All @@ -37,11 +37,11 @@ export function calcRangeKeys(
return [];
}

function matchKey(key: string) {
function matchKey(key: Key) {
return key === startKey || key === endKey;
}

traverseNodesKey(treeData, (key: string) => {
traverseNodesKey(treeData, (key: Key) => {
if (record === Record.End) {
return false;
}
Expand Down Expand Up @@ -71,10 +71,10 @@ export function calcRangeKeys(
return keys;
}

export function convertDirectoryKeysToNodes(treeData: DataNode[], keys: string[]) {
const restKeys: string[] = [...keys];
export function convertDirectoryKeysToNodes(treeData: DataNode[], keys: Key[]) {
const restKeys: Key[] = [...keys];
const nodes: DataNode[] = [];
traverseNodesKey(treeData, (key: string, node: DataNode) => {
traverseNodesKey(treeData, (key: Key, node: DataNode) => {
const index = restKeys.indexOf(key);
if (index !== -1) {
nodes.push(node);
Expand Down

0 comments on commit c70b49a

Please sign in to comment.