Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dcbc960

Browse files
dgrahammuan
andcommitted
Index list items within their parent list
Co-authored-by: Mu-An Chiou <[email protected]>
1 parent 0f61bbe commit dcbc960

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

examples/index.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@
3434
WALL-E
3535
</label>
3636
</li>
37+
<li class="task-list-item">
38+
<label>
39+
<input type="checkbox" class="task-list-item-checkbox">
40+
R2-D2
41+
</label>
42+
43+
<ul class="contains-task-list">
44+
<li class="task-list-item">
45+
<label>
46+
<input type="checkbox" class="task-list-item-checkbox">
47+
Baymax
48+
</label>
49+
</li>
50+
</ul>
51+
</li>
3752
<li class="task-list-item">
3853
<label>
3954
<input type="checkbox" class="task-list-item-checkbox">

src/task-lists-element.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,10 @@ function onListItemMouseOut(event: MouseEvent) {
136136
}
137137

138138
// Returns the list item position as a (list index, item index) tuple.
139-
// Listen on top-level task lists because item indexing includes nested task lists.
140-
function position(el: Element): [number, number] {
141-
const list = rootTaskList(el)
139+
function position(checkbox: HTMLInputElement): [number, number] {
140+
const list = taskList(checkbox)
142141
if (!list) throw new Error('.contains-task-list not found')
143-
const flattened = Array.from(list.querySelectorAll('li'))
144-
const index = flattened.indexOf(el.closest('.task-list-item'))
142+
const index = Array.from(list.children).indexOf(checkbox.closest('.task-list-item'))
145143
return [listIndex(list), index]
146144
}
147145

test/test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,29 @@ describe('task-lists element', function() {
3434
<input id="wall-e" type="checkbox" class="task-list-item-checkbox"> WALL-E
3535
</li>
3636
</ul>
37-
<ul>
37+
38+
<ol>
3839
<li>
40+
Nested
3941
<ul class="contains-task-list">
4042
<li class="task-list-item">
41-
<input id="baymax" type="checkbox" class="task-list-item-checkbox"> Baymax
43+
<label>
44+
<input type="checkbox" class="task-list-item-checkbox">
45+
R2-D2
46+
</label>
47+
48+
<ul class="contains-task-list">
49+
<li class="task-list-item">
50+
<label>
51+
<input id="baymax" type="checkbox" class="task-list-item-checkbox">
52+
Baymax
53+
</label>
54+
</li>
55+
</ul>
4256
</li>
4357
</ul>
4458
</li>
45-
</ul>
59+
</ol>
4660
</task-lists>`
4761
document.body.append(container)
4862
})
@@ -76,7 +90,7 @@ describe('task-lists element', function() {
7690
list.addEventListener('task-lists:check', function(event) {
7791
called = true
7892
const {position, checked} = event.detail
79-
assert.deepEqual(position, [3, 0])
93+
assert.deepEqual(position, [4, 0])
8094
assert(checked)
8195
})
8296

0 commit comments

Comments
 (0)