vanilla-smart-select 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * VanillaSmartSelect v1.0.2
2
+ * VanillaSmartSelect v1.0.3
3
3
  * (c) 2026 Ailton Occhi <ailton.occhi@hotmail.com>
4
4
  * Released under the MIT License.
5
5
  */
@@ -4143,9 +4143,9 @@ class ResultsAdapter extends BaseAdapter {
4143
4143
  // Update results list
4144
4144
  this.results.update(filteredResults);
4145
4145
 
4146
- // Auto-highlight first non-disabled item when there are results
4146
+ // Auto-highlight selected item if exists, otherwise first non-disabled item
4147
4147
  if (filteredResults && filteredResults.length > 0) {
4148
- this._autoHighlightFirst(filteredResults);
4148
+ this._autoHighlightSelectedOrFirst(filteredResults);
4149
4149
  }
4150
4150
 
4151
4151
  // Emit results event for accessibility announcements
@@ -4201,13 +4201,13 @@ class ResultsAdapter extends BaseAdapter {
4201
4201
  // Update results list
4202
4202
  this.results.update(this.accumulatedResults);
4203
4203
 
4204
- // Auto-highlight first non-disabled item when there are results
4204
+ // Auto-highlight selected item if exists, otherwise first non-disabled item
4205
4205
  if (
4206
4206
  this.accumulatedResults &&
4207
4207
  this.accumulatedResults.length > 0 &&
4208
4208
  !append
4209
4209
  ) {
4210
- this._autoHighlightFirst(this.accumulatedResults);
4210
+ this._autoHighlightSelectedOrFirst(this.accumulatedResults);
4211
4211
  }
4212
4212
 
4213
4213
  // Emit results event for accessibility announcements
@@ -4345,6 +4345,71 @@ class ResultsAdapter extends BaseAdapter {
4345
4345
  return null;
4346
4346
  }
4347
4347
 
4348
+ /**
4349
+ * Auto-highlight selected item if exists, otherwise first non-disabled item
4350
+ * @param {Array} results - Filtered results
4351
+ * @private
4352
+ */
4353
+ _autoHighlightSelectedOrFirst(results) {
4354
+ // Try to highlight the currently selected item first
4355
+ if (this.dataAdapter) {
4356
+ const currentSelection = this.dataAdapter.current();
4357
+
4358
+ // If there's a selection, try to find and highlight it
4359
+ if (currentSelection && currentSelection.length > 0) {
4360
+ const selectedItem = currentSelection[0]; // Use first selected item
4361
+ const flatIndex = this._findFlatIndexOfItem(results, selectedItem.id);
4362
+
4363
+ if (flatIndex !== -1) {
4364
+ this.results.highlight(flatIndex);
4365
+
4366
+ // Update ARIA activedescendant
4367
+ if (this.instance.accessibilityManager) {
4368
+ this.instance.accessibilityManager.updateActiveDescendant(flatIndex);
4369
+ }
4370
+ return; // Successfully highlighted selected item
4371
+ }
4372
+ }
4373
+ }
4374
+
4375
+ // No selection or selected item not found - highlight first non-disabled item
4376
+ this._autoHighlightFirst(results);
4377
+ }
4378
+
4379
+ /**
4380
+ * Find the flat index of an item by ID in results (including groups)
4381
+ * @param {Array} results - Results array (may contain groups)
4382
+ * @param {string|number} itemId - Item ID to find
4383
+ * @returns {number} Flat index or -1 if not found
4384
+ * @private
4385
+ */
4386
+ _findFlatIndexOfItem(results, itemId) {
4387
+ let flatIndex = 0;
4388
+
4389
+ for (let i = 0; i < results.length; i++) {
4390
+ const item = results[i];
4391
+
4392
+ // Check if it's a group
4393
+ if (item.children && Array.isArray(item.children)) {
4394
+ // Search in group children
4395
+ for (let j = 0; j < item.children.length; j++) {
4396
+ if (String(item.children[j].id) === String(itemId)) {
4397
+ return flatIndex;
4398
+ }
4399
+ flatIndex++;
4400
+ }
4401
+ } else {
4402
+ // Regular item - check if it matches
4403
+ if (String(item.id) === String(itemId)) {
4404
+ return flatIndex;
4405
+ }
4406
+ flatIndex++;
4407
+ }
4408
+ }
4409
+
4410
+ return -1; // Not found
4411
+ }
4412
+
4348
4413
  /**
4349
4414
  * Auto-highlight first non-disabled item
4350
4415
  * @param {Array} results - Filtered results
@@ -6306,7 +6371,7 @@ var decorators$1 = /*#__PURE__*/Object.freeze({
6306
6371
  * Vanilla-Smart-Select
6307
6372
  * Modern JavaScript dropdown enhancement library without jQuery dependencies
6308
6373
  *
6309
- * @version 1.0.2
6374
+ * @version 1.0.3
6310
6375
  * @author Ailton Occhi <ailton.occhi@hotmail.com>
6311
6376
  * @license MIT
6312
6377
  */