@smilodon/core 1.2.0 → 1.2.1

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.
package/dist/index.umd.js CHANGED
@@ -615,6 +615,63 @@
615
615
  this._options.optionRenderer = renderer;
616
616
  this.render();
617
617
  }
618
+ /**
619
+ * Public API: setItems() method for compatibility with EnhancedSelect
620
+ * Accepts an array of items which can be:
621
+ * - Simple primitives (string, number)
622
+ * - Objects with {label, value} structure
623
+ * - Objects with {label, value, optionComponent} for custom rendering (v1.2.0+)
624
+ */
625
+ setItems(items) {
626
+ this.items = items ?? [];
627
+ }
628
+ /**
629
+ * Public API: setValue() method to programmatically select an item
630
+ * For single select: clears selection and selects the item matching the value
631
+ * For multi-select: adds to selection if not already selected
632
+ */
633
+ setValue(value) {
634
+ if (value === null || value === undefined || value === '') {
635
+ // Clear selection
636
+ this._selectedSet.clear();
637
+ this._selectedItems.clear();
638
+ this._activeIndex = -1;
639
+ this.render();
640
+ return;
641
+ }
642
+ // Find item by value
643
+ const index = this._items.findIndex(item => {
644
+ if (typeof item === 'object' && item !== null && 'value' in item) {
645
+ return item.value === value;
646
+ }
647
+ return item === value;
648
+ });
649
+ if (index >= 0) {
650
+ const item = this._items[index];
651
+ if (!this._multi) {
652
+ // Single select: clear and set
653
+ this._selectedSet.clear();
654
+ this._selectedItems.clear();
655
+ }
656
+ this._selectedSet.add(index);
657
+ this._selectedItems.set(index, item);
658
+ this._activeIndex = index;
659
+ this.render();
660
+ }
661
+ }
662
+ /**
663
+ * Public API: getValue() method to get currently selected value(s)
664
+ * Returns single value for single-select, array for multi-select
665
+ */
666
+ getValue() {
667
+ const values = Array.from(this._selectedItems.values()).map(item => {
668
+ if (typeof item === 'object' && item !== null && 'value' in item) {
669
+ return item.value;
670
+ }
671
+ return item;
672
+ });
673
+ return this._multi ? values : (values[0] ?? null);
674
+ }
618
675
  render() {
619
676
  const { optionTemplate, optionRenderer } = this._options;
620
677
  const viewportHeight = this.getBoundingClientRect().height || 300;
@@ -642,7 +699,11 @@
642
699
  node.replaceChildren(el ?? document.createTextNode(String(item)));
643
700
  }
644
701
  else {
645
- node.textContent = String(item);
702
+ // Handle {label, value} objects or primitives
703
+ const displayText = (typeof item === 'object' && item !== null && 'label' in item)
704
+ ? String(item.label)
705
+ : String(item);
706
+ node.textContent = displayText;
646
707
  }
647
708
  });
648
709
  return;
@@ -668,7 +729,11 @@
668
729
  }
669
730
  else {
670
731
  const el = document.createElement('div');
671
- el.textContent = String(item);
732
+ // Handle {label, value} objects or primitives
733
+ const displayText = (typeof item === 'object' && item !== null && 'label' in item)
734
+ ? String(item.label)
735
+ : String(item);
736
+ el.textContent = displayText;
672
737
  el.setAttribute('data-selectable', '');
673
738
  el.setAttribute('data-index', String(i));
674
739
  this._applyOptionAttrs(el, i);
@@ -720,15 +785,23 @@
720
785
  this.render();
721
786
  // Emit with all required fields
722
787
  const selected = this._selectedSet.has(index);
788
+ const value = item?.value ?? item;
789
+ const label = item?.label ?? String(item);
723
790
  this._emit('select', {
724
791
  item,
725
792
  index,
726
- value: item?.value ?? item,
727
- label: item?.label ?? String(item),
793
+ value,
794
+ label,
728
795
  selected,
729
796
  multi: this._multi
730
797
  });
731
- this._announce(`Selected ${String(item)}`);
798
+ // Emit 'change' event for better React compatibility
799
+ this._emit('change', {
800
+ selectedItems: Array.from(this._selectedItems.values()),
801
+ selectedValues: Array.from(this._selectedItems.values()).map(i => i?.value ?? i),
802
+ selectedIndices: Array.from(this._selectedSet)
803
+ });
804
+ this._announce(`Selected ${label}`);
732
805
  }
733
806
  _onKeydown(e) {
734
807
  switch (e.key) {