Skip to content

Commit 5234309

Browse files
author
Dmitry Yadrikhinsky
committed
[TaskList] Checked init
1 parent b30274e commit 5234309

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

src/models/TaskModel.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import { ITreeItem } from '../types/ITreeItem';
33

44
interface ITaskModel extends ITreeItem<ITaskModel> {
55
projectId: string;
6+
checked: boolean;
67
}
78

89
export default class TaskModel extends AbstractModel implements ITaskModel {
910
key: string = '';
1011
title: string = '';
1112
children: TaskModel[] = [];
1213
projectId: string = '';
14+
checked: boolean = false;
1315

1416
constructor(props: ITaskModel) {
1517
super();

src/screens/projects/Projects.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ const TaskList = TreeList(
1919
() => tasksStore.getTasks(projectStore.activeProject),
2020
(list: TaskModel[]) => {
2121
tasksStore.set(projectStore.activeProject, list);
22+
},
23+
{
24+
checkable: true,
25+
onCheck(keys) {
26+
tasksStore.checkTasks(keys as string[]);
27+
console.log('Check', keys);
28+
},
29+
getDefaultChecked() {
30+
const taskKeys = tasksStore.getCheckedKeys(projectStore.activeProject);
31+
console.log('Checked List', taskKeys);
32+
return taskKeys;
33+
},
2234
}
2335
);
2436

src/screens/projects/components/TreeList.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ interface TreeListProps {
1212

1313
export default function TreeList<T extends ITreeItem>(
1414
getData: () => T[],
15-
updateData: (items: T[]) => void
15+
updateData: (items: T[]) => void,
16+
options?: {
17+
checkable?: boolean;
18+
onCheck?: (checkedKeys: React.Key[]) => void;
19+
getDefaultChecked?: () => React.Key[];
20+
}
1621
) {
1722
return observer(function TreeList({ onSelect }: TreeListProps) {
1823
const data = getData();
@@ -87,11 +92,14 @@ export default function TreeList<T extends ITreeItem>(
8792
return (
8893
<Tree
8994
className="draggable-tree"
95+
defaultCheckedKeys={options?.getDefaultChecked?.()}
96+
checkable={options?.checkable}
9097
draggable
9198
blockNode
9299
onDrop={onDrop}
93100
treeData={data}
94101
onSelect={onSelect}
102+
onCheck={options?.checkable ? options?.onCheck : undefined}
95103
/>
96104
);
97105
});

src/services/tasks/TaskStore.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,39 @@ export default class TaskStore {
3535
restore() {
3636
this.tasks = this.tasksService.getAll();
3737
}
38+
39+
getCheckedKeys(projectId: string): string[] {
40+
if (Array.isArray(this.tasks[projectId])) {
41+
const checkedIds: string[] = [];
42+
this.getCheckedKeysRecursive(this.tasks[projectId], checkedIds);
43+
return checkedIds;
44+
}
45+
return [];
46+
}
47+
48+
checkTasks(taskIds: string[]) {
49+
Object.keys(this.tasks).forEach((projectId) => {
50+
this.checkTasksRecursive(this.tasks[projectId], taskIds);
51+
});
52+
}
53+
54+
private getCheckedKeysRecursive(tasks: TaskModel[], checkedIds: string[]) {
55+
tasks.forEach((task) => {
56+
if (task.checked) {
57+
checkedIds.push(task.key);
58+
}
59+
if (Array.isArray(task.children)) {
60+
this.getCheckedKeysRecursive(task.children, checkedIds);
61+
}
62+
});
63+
}
64+
65+
private checkTasksRecursive(tasks: TaskModel[], taskIds: string[]) {
66+
tasks.forEach((task) => {
67+
task.checked = taskIds.includes(task.key);
68+
if (Array.isArray(task.children)) {
69+
this.checkTasksRecursive(task.children, taskIds);
70+
}
71+
});
72+
}
3873
}

yarn.lock

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,15 +1696,20 @@
16961696
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
16971697
integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==
16981698

1699-
"@types/node@*", "@types/node@14.14.10":
1699+
"@types/node@*":
1700+
version "14.14.37"
1701+
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.37.tgz#a3dd8da4eb84a996c36e331df98d82abd76b516e"
1702+
integrity sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==
1703+
1704+
"@types/node@14.14.10":
17001705
version "14.14.10"
17011706
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.10.tgz#5958a82e41863cfc71f2307b3748e3491ba03785"
17021707
integrity sha512-J32dgx2hw8vXrSbu4ZlVhn1Nm3GbeCFNw2FWL8S5QKucHGY0cyNwjdQdO+KMBZ4wpmC7KhLCiNsdk1RFRIYUQQ==
17031708

17041709
"@types/node@^12.0.12":
1705-
version "12.19.5"
1706-
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.19.5.tgz#9be3946136e818597c71c62d04240d0602c645d4"
1707-
integrity sha512-Wgdl27uw/jUYUFyajUGKSjDNGxmJrZi9sjeG6UJImgUtKbJoO9aldx+1XODN1EpNDX9DirvbvHHmTsNlb8GwMA==
1710+
version "12.20.7"
1711+
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.7.tgz#1cb61fd0c85cb87e728c43107b5fd82b69bc9ef8"
1712+
integrity sha512-gWL8VUkg8VRaCAUgG9WmhefMqHmMblxe2rVpMF86nZY/+ZysU+BkAp+3cz03AixWDSSz0ks5WX59yAhv/cDwFA==
17081713

17091714
"@types/normalize-package-data@^2.4.0":
17101715
version "2.4.0"
@@ -4585,9 +4590,9 @@ electron-updater@^4.3.4:
45854590
semver "^7.3.2"
45864591

45874592
electron@^11.0.1:
4588-
version "11.0.1"
4589-
resolved "https://registry.yarnpkg.com/electron/-/electron-11.0.1.tgz#e71195ff9abfbcfa0f4bf49ec9064e5e0f3f928c"
4590-
integrity sha512-UefDvxeyTREL1FpC0BcXwzLjcYiQICFSZONbwarpoqTXQU5rzEguIegHPqg5AqwYH4FVYfEuigj/Vm94rtZOzg==
4593+
version "11.4.2"
4594+
resolved "https://registry.yarnpkg.com/electron/-/electron-11.4.2.tgz#02005d9f5d77ea6485efeffdb5c5433c769ddda4"
4595+
integrity sha512-P0PRLH7cXp8ZdpA9yVPe7jRVM+QeiAtsadqmqS6XY3AYrsH+7bJnVrNuw6p/fcmp+b/UxWaCexobqQpyFJ5Qkw==
45914596
dependencies:
45924597
"@electron/get" "^1.0.1"
45934598
"@types/node" "^12.0.12"

0 commit comments

Comments
 (0)