Skip to content

Commit

Permalink
feat(TreeData): 新增 fromList 方法从列表生成实例
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 14, 2020
1 parent f671cdc commit 5516106
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 6 deletions.
29 changes: 26 additions & 3 deletions src/utils/TreeData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ describe('TreeData', () => {
expect(
new TreeData(data)
.setNodeProps({
value: node => node.id,
id2: node => `${node.id}[2]`,
value: _ => _.node.id,
id2: _ => `${_.node.id}[2]`,
})
.export(),
).toMatchSnapshot()
Expand Down Expand Up @@ -159,13 +159,36 @@ describe('TreeData', () => {
expect(new TreeData(data).clone().export()).toMatchSnapshot()
})

test('fromList', () => {
expect(
TreeData.fromList(
[
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 11, pid: 1 },
{ id: 4 },
{ id: 12, pid: 1 },
{ id: 111, pid: 11 },
{ id: 42, pid: 4 },
],
'id',
'pid',
)
.setNodeProps({
name: _ => `${_.depth}. ${_.node.id}`,
})
.export(),
).toMatchSnapshot()
})

test('综合', () => {
const names: string[] = []
const names2: string[] = []
expect(
new TreeData(data)
.setNodeProps({
name: node => `name${node.id}`,
name: _ => `name${_.node.id}`,
gender: () => `male`,
})
.omitNodeProps(['gender'])
Expand Down
39 changes: 36 additions & 3 deletions src/utils/TreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ export type TreeDataSingleRootData<TNode extends TreeDataNode> = TNode

export type TreeDataMultipleRootData<TNode extends TreeDataNode> = TNode[]

export type TreeDataStandardNode<TNode extends TreeDataNode> = Merge<
TNode,
{
children: Array<TreeDataStandardNode<TNode>>
}
>

export type TreeDataData<TNode extends TreeDataNode> = TNode[]

export type TreeDataChildrenPropName<TNode extends TreeDataNode> = {
Expand Down Expand Up @@ -236,9 +243,9 @@ export class TreeData<TNode extends TreeDataNode> {
*/
setNodeProps<
TProps extends {
[K in keyof TNode]?: (node: TNode) => any
[K in keyof TNode]?: (payload: TreeDataTraverseFnPayload<TNode>) => any
} & {
[K: string]: (node: TNode) => any
[K: string]: (payload: TreeDataTraverseFnPayload<TNode>) => any
}
>(
props: TProps,
Expand All @@ -252,7 +259,7 @@ export class TreeData<TNode extends TreeDataNode> {
> {
this.traverse(payload => {
for (const propName of Object.keys(props)) {
;(payload.node as any)[propName] = props[propName](payload.node)
;(payload.node as any)[propName] = props[propName](payload)
}
})
return this as any
Expand Down Expand Up @@ -429,4 +436,30 @@ export class TreeData<TNode extends TreeDataNode> {
export(): TreeDataData<TNode> {
return cloneDeep(this.data)
}

/**
* 从列表生成实例。
*
* @param list 列表
* @param idKey ID 所在键
* @param parentIdKey 父 ID 所在键
*/
static fromList<TItem extends Record<any, any>>(
list: TItem[],
idKey: keyof TItem,
parentIdKey: keyof TItem,
): TreeData<TreeDataStandardNode<TItem>> {
const _list: Array<TreeDataStandardNode<TItem>> = cloneDeep(list) as any
const data = _list
.map(item => {
item.children = _list.filter(
item2 =>
(item2 as any)[parentIdKey] != null &&
(item as any)[idKey] === (item2 as any)[parentIdKey],
) as any
return item
})
.filter(item => (item as any)[parentIdKey] == null)
return new TreeData(data)
}
}
52 changes: 52 additions & 0 deletions src/utils/__snapshots__/TreeData.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,58 @@ Array [

exports[`TreeData findNodes 2`] = `Array []`;

exports[`TreeData fromList 1`] = `
Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [],
"id": 111,
"name": "2. 111",
"pid": 11,
},
],
"id": 11,
"name": "1. 11",
"pid": 1,
},
Object {
"children": Array [],
"id": 12,
"name": "1. 12",
"pid": 1,
},
],
"id": 1,
"name": "0. 1",
},
Object {
"children": Array [],
"id": 2,
"name": "0. 2",
},
Object {
"children": Array [],
"id": 3,
"name": "0. 3",
},
Object {
"children": Array [
Object {
"children": Array [],
"id": 42,
"name": "1. 42",
"pid": 4,
},
],
"id": 4,
"name": "0. 4",
},
]
`;

exports[`TreeData omitNodeProps 1`] = `
Array [
Object {
Expand Down

0 comments on commit 5516106

Please sign in to comment.