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