@zag-js/combobox 0.58.2 → 0.59.0

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.
@@ -31,8 +31,8 @@ export function connect<T extends PropTypes, V extends CollectionItem>(
31
31
 
32
32
  function getItemState(props: ItemProps): ItemState {
33
33
  const { item } = props
34
- const disabled = collection.isItemDisabled(item)
35
- const value = collection.itemToValue(item)
34
+ const disabled = collection.getItemDisabled(item)
35
+ const value = collection.getItemValue(item)!
36
36
  return {
37
37
  value,
38
38
  disabled: Boolean(disabled || disabled),
@@ -246,6 +246,7 @@ export function connect<T extends PropTypes, V extends CollectionItem>(
246
246
  "data-state": open ? "open" : "closed",
247
247
  "aria-controls": open ? dom.getContentId(state.context) : undefined,
248
248
  disabled,
249
+ "data-invalid": dataAttr(invalid),
249
250
  "data-focusable": dataAttr(props.focusable),
250
251
  "data-readonly": dataAttr(readOnly),
251
252
  "data-disabled": dataAttr(disabled),
@@ -300,6 +301,7 @@ export function connect<T extends PropTypes, V extends CollectionItem>(
300
301
  tabIndex: -1,
301
302
  hidden: !open,
302
303
  "data-state": open ? "open" : "closed",
304
+ "data-placement": state.context.currentPlacement,
303
305
  "aria-labelledby": dom.getLabelId(state.context),
304
306
  "aria-multiselectable": state.context.multiple && composite ? true : undefined,
305
307
  onPointerDown(event) {
@@ -326,6 +328,7 @@ export function connect<T extends PropTypes, V extends CollectionItem>(
326
328
  type: "button",
327
329
  tabIndex: -1,
328
330
  disabled: disabled,
331
+ "data-invalid": dataAttr(invalid),
329
332
  "aria-label": translations.clearTriggerLabel,
330
333
  "aria-controls": dom.getInputId(state.context),
331
334
  hidden: !state.context.value.length,
@@ -390,6 +393,7 @@ export function connect<T extends PropTypes, V extends CollectionItem>(
390
393
  return normalize.element({
391
394
  ...parts.itemText.attrs,
392
395
  dir: state.context.dir,
396
+ "data-state": itemState.selected ? "checked" : "unchecked",
393
397
  "data-disabled": dataAttr(itemState.disabled),
394
398
  "data-highlighted": dataAttr(itemState.highlighted),
395
399
  })
@@ -567,8 +567,8 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
567
567
  isInputValueEmpty: (ctx) => ctx.isInputValueEmpty,
568
568
  autoComplete: (ctx) => ctx.autoComplete && !ctx.multiple,
569
569
  autoHighlight: (ctx) => ctx.autoHighlight,
570
- isFirstItemHighlighted: (ctx) => ctx.collection.first() === ctx.highlightedValue,
571
- isLastItemHighlighted: (ctx) => ctx.collection.last() === ctx.highlightedValue,
570
+ isFirstItemHighlighted: (ctx) => ctx.collection.firstValue === ctx.highlightedValue,
571
+ isLastItemHighlighted: (ctx) => ctx.collection.lastValue === ctx.highlightedValue,
572
572
  isCustomValue: (ctx) => ctx.inputValue !== ctx.valueAsString,
573
573
  allowCustomValue: (ctx) => !!ctx.allowCustomValue,
574
574
  hasHighlightedItem: (ctx) => ctx.highlightedValue != null,
@@ -747,13 +747,15 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
747
747
  set.inputValue(ctx, inputValue)
748
748
  },
749
749
  syncInitialValues(ctx) {
750
- const selectedItems = ctx.collection.items(ctx.value)
751
- const valueAsString = ctx.collection.itemsToString(selectedItems)
750
+ const selectedItems = ctx.collection.findMany(ctx.value)
751
+ const valueAsString = ctx.collection.stringifyMany(ctx.value)
752
752
 
753
- ctx.highlightedItem = ctx.collection.item(ctx.highlightedValue)
753
+ ctx.highlightedItem = ctx.collection.find(ctx.highlightedValue)
754
754
  ctx.selectedItems = selectedItems
755
755
  ctx.valueAsString = valueAsString
756
756
 
757
+ if (ctx.inputValue.trim() || ctx.multiple) return
758
+
757
759
  ctx.inputValue = match(ctx.selectionBehavior, {
758
760
  preserve: ctx.inputValue || valueAsString,
759
761
  replace: valueAsString,
@@ -789,40 +791,40 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
789
791
  },
790
792
  highlightFirstItem(ctx) {
791
793
  raf(() => {
792
- const value = ctx.collection.first()
794
+ const value = ctx.collection.firstValue
793
795
  set.highlightedValue(ctx, value)
794
796
  })
795
797
  },
796
798
  highlightFirstItemIfNeeded(ctx) {
797
799
  if (!ctx.autoHighlight) return
798
800
  raf(() => {
799
- const value = ctx.collection.first()
801
+ const value = ctx.collection.firstValue
800
802
  set.highlightedValue(ctx, value)
801
803
  })
802
804
  },
803
805
  highlightLastItem(ctx) {
804
806
  raf(() => {
805
- const value = ctx.collection.last()
807
+ const value = ctx.collection.lastValue
806
808
  set.highlightedValue(ctx, value)
807
809
  })
808
810
  },
809
811
  highlightNextItem(ctx) {
810
812
  let value: string | null = null
811
813
  if (ctx.highlightedValue) {
812
- value = ctx.collection.next(ctx.highlightedValue)
813
- if (!value && ctx.loopFocus) value = ctx.collection.first()
814
+ value = ctx.collection.getNextValue(ctx.highlightedValue)
815
+ if (!value && ctx.loopFocus) value = ctx.collection.firstValue
814
816
  } else {
815
- value = ctx.collection.first()
817
+ value = ctx.collection.firstValue
816
818
  }
817
819
  set.highlightedValue(ctx, value)
818
820
  },
819
821
  highlightPrevItem(ctx) {
820
822
  let value: string | null = null
821
823
  if (ctx.highlightedValue) {
822
- value = ctx.collection.prev(ctx.highlightedValue)
823
- if (!value && ctx.loopFocus) value = ctx.collection.last()
824
+ value = ctx.collection.getPreviousValue(ctx.highlightedValue)
825
+ if (!value && ctx.loopFocus) value = ctx.collection.lastValue
824
826
  } else {
825
- value = ctx.collection.last()
827
+ value = ctx.collection.lastValue
826
828
  }
827
829
  set.highlightedValue(ctx, value)
828
830
  },
@@ -838,7 +840,7 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
838
840
  if (ctx.hasSelectedItems) {
839
841
  value = ctx.collection.sort(ctx.value)[0]
840
842
  } else {
841
- value = ctx.collection.first()
843
+ value = ctx.collection.firstValue
842
844
  }
843
845
  set.highlightedValue(ctx, value)
844
846
  })
@@ -849,7 +851,7 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
849
851
  if (ctx.hasSelectedItems) {
850
852
  value = ctx.collection.sort(ctx.value)[0]
851
853
  } else {
852
- value = ctx.collection.last()
854
+ value = ctx.collection.lastValue
853
855
  }
854
856
  set.highlightedValue(ctx, value)
855
857
  })
@@ -857,7 +859,7 @@ export function machine<T extends CollectionItem>(userContext: UserDefinedContex
857
859
  autofillInputValue(ctx, evt) {
858
860
  const inputEl = dom.getInputEl(ctx)
859
861
  if (!ctx.autoComplete || !inputEl || !evt.keypress) return
860
- const valueText = ctx.collection.valueToString(ctx.highlightedValue)
862
+ const valueText = ctx.collection.stringify(ctx.highlightedValue)
861
863
  raf(() => {
862
864
  inputEl.value = valueText || ctx.inputValue
863
865
  })
@@ -883,14 +885,14 @@ const sync = {
883
885
  valueChange: (ctx: MachineContext) => {
884
886
  // side effect
885
887
  const prevSelectedItems = ctx.selectedItems
888
+
886
889
  ctx.selectedItems = ctx.value.map((v) => {
887
- const foundItem = prevSelectedItems.find((item) => ctx.collection.itemToValue(item) === v)
890
+ const foundItem = prevSelectedItems.find((item) => ctx.collection.hasItem(item))
888
891
  if (foundItem) return foundItem
889
- return ctx.collection.item(v)
892
+ return ctx.collection.find(v)
890
893
  })
891
894
 
892
- // set valueAsString
893
- const valueAsString = ctx.collection.itemsToString(ctx.selectedItems)
895
+ const valueAsString = ctx.collection.stringifyItems(ctx.selectedItems)
894
896
  ctx.valueAsString = valueAsString
895
897
 
896
898
  // set inputValue
@@ -915,7 +917,7 @@ const sync = {
915
917
  set.inputValue(ctx, inputValue)
916
918
  },
917
919
  highlightChange: (ctx: MachineContext) => {
918
- ctx.highlightedItem = ctx.collection.item(ctx.highlightedValue)
920
+ ctx.highlightedItem = ctx.collection.find(ctx.highlightedValue)
919
921
  },
920
922
  }
921
923