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