list.js icon indicating copy to clipboard operation
list.js copied to clipboard

`list.reIndex()` Omits `self.update()`, Leaving `visibleItems` and `matchingItems` Empty

Open codeCraft-Ritik opened this issue 4 weeks ago • 0 comments

Affected File & Line

  • File: src/index.js
  • Lines: 98–105 (this.reIndex)

Description

The documented purpose of reIndex() is to re-parse the DOM list when elements are added or modified externally. In src/index.js:

this.reIndex = function () {
  self.items = []
  self.visibleItems = []
  self.matchingItems = []
  self.searched = false
  self.filtered = false
  self.parse(self.list)
}

reIndex() resets self.items, self.visibleItems, and self.matchingItems, then calls self.parse(self.list). While self.parse() populates self.items, it does not populate self.visibleItems or self.matchingItems.

Every other method in List.js that mutates items (init.start, add, remove, show, search, filter, sort) ends with a call to self.update(). reIndex() completely omits self.update().

Real-World Impact

Following a call to list.reIndex():

  1. list.visibleItems is empty ([]).
  2. list.matchingItems is empty ([]).
  3. Items have not had .show() or .hide() applied.
  4. The 'updated' event is never dispatched.
  5. Connected plugins and pagination components remain completely desynchronized from the actual DOM state.

Steps to Reproduce

  1. Initialize a List with 1 item: list.items.length === 1, list.visibleItems.length === 1.
  2. Append a new <li> element to list.list in the DOM.
  3. Call list.reIndex().
  4. Inspect list.visibleItems.length and list.matchingItems.length.

Minimal Reproduction Code (Jest / Node.js)

const List = require('./src/index');

document.body.innerHTML = '<div id="list"><ul class="list"><li><span class="name">Item 1</span></li></ul></div>';

const list = new List('list', { valueNames: ['name'] });

console.log('Initial visibleItems:', list.visibleItems.length); // 1

// Add element to DOM and re-index
const li = document.createElement('li');
li.innerHTML = '<span class="name">Item 2</span>';
list.list.appendChild(li);

list.reIndex();

console.log('After reIndex items:', list.items.length);               // 2
console.log('After reIndex visibleItems:', list.visibleItems.length);   // Actual: 0, Expected: 2
console.log('After reIndex matchingItems:', list.matchingItems.length); // Actual: 0, Expected: 2

Expected Behavior

After list.reIndex(), visibleItems and matchingItems contain the 2 parsed items, and the 'updated' event fires.

Actual Behavior

list.items.length is 2, but list.visibleItems and list.matchingItems are [] (length 0).

Root Cause

reIndex() forgets to call self.update().

Proposed Fix

--- a/src/index.js
+++ b/src/index.js
@@ -102,6 +102,7 @@ module.exports = function (id, options, values) {
     self.searched = false
     self.filtered = false
     self.parse(self.list)
+    self.update()
   }

   this.toJSON = function () {

codeCraft-Ritik avatar Nov 05 '26 05:11 codeCraft-Ritik