@sprawlify/primitives 0.1.11 → 0.1.12

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.
Files changed (103) hide show
  1. package/dist/{collection-BBgC8xCb.cjs → collection-D0Abtxc0.cjs} +68 -0
  2. package/dist/{collection-DgzidRvr.mjs → collection-GSpaYJ2f.mjs} +45 -1
  3. package/dist/collection.cjs +5 -1
  4. package/dist/collection.d.cts +49 -3
  5. package/dist/collection.d.mts +49 -3
  6. package/dist/collection.mjs +2 -2
  7. package/dist/{list-collection-B_8BN0XB.d.cts → list-collection-Cjgbx_9l.d.cts} +1 -1
  8. package/dist/{list-collection-DkRA5LOq.d.mts → list-collection-IF510iNl.d.mts} +1 -1
  9. package/dist/machines/accordion/index.d.cts +1 -1
  10. package/dist/machines/accordion/index.d.mts +1 -1
  11. package/dist/machines/angle-slider/index.d.cts +1 -1
  12. package/dist/machines/angle-slider/index.d.mts +1 -1
  13. package/dist/machines/carousel/index.d.cts +1 -1
  14. package/dist/machines/carousel/index.d.mts +1 -1
  15. package/dist/machines/cascade-select/index.cjs +1 -1
  16. package/dist/machines/cascade-select/index.d.cts +2 -2
  17. package/dist/machines/cascade-select/index.d.mts +2 -2
  18. package/dist/machines/cascade-select/index.mjs +1 -1
  19. package/dist/machines/checkbox/index.d.cts +1 -1
  20. package/dist/machines/checkbox/index.d.mts +1 -1
  21. package/dist/machines/clipboard/index.d.cts +1 -1
  22. package/dist/machines/clipboard/index.d.mts +1 -1
  23. package/dist/machines/collapsible/index.d.cts +1 -1
  24. package/dist/machines/collapsible/index.d.mts +1 -1
  25. package/dist/machines/color-picker/index.d.cts +1 -1
  26. package/dist/machines/color-picker/index.d.mts +1 -1
  27. package/dist/machines/combobox/index.cjs +279 -355
  28. package/dist/machines/combobox/index.d.cts +5 -4
  29. package/dist/machines/combobox/index.d.mts +5 -4
  30. package/dist/machines/combobox/index.mjs +279 -355
  31. package/dist/machines/date-input/index.cjs +1830 -0
  32. package/dist/machines/date-input/index.d.cts +223 -0
  33. package/dist/machines/date-input/index.d.mts +223 -0
  34. package/dist/machines/date-input/index.mjs +1813 -0
  35. package/dist/machines/date-picker/index.d.cts +1 -1
  36. package/dist/machines/date-picker/index.d.mts +1 -1
  37. package/dist/machines/dialog/index.d.cts +1 -1
  38. package/dist/machines/dialog/index.d.mts +1 -1
  39. package/dist/machines/drawer/index.d.cts +1 -1
  40. package/dist/machines/drawer/index.d.mts +1 -1
  41. package/dist/machines/dropdown-menu/index.d.cts +1 -1
  42. package/dist/machines/dropdown-menu/index.d.mts +1 -1
  43. package/dist/machines/editable/index.d.cts +1 -1
  44. package/dist/machines/editable/index.d.mts +1 -1
  45. package/dist/machines/file-upload/index.d.cts +1 -1
  46. package/dist/machines/file-upload/index.d.mts +1 -1
  47. package/dist/machines/floating-panel/index.d.cts +1 -1
  48. package/dist/machines/floating-panel/index.d.mts +1 -1
  49. package/dist/machines/hover-card/index.d.cts +1 -1
  50. package/dist/machines/hover-card/index.d.mts +1 -1
  51. package/dist/machines/listbox/index.cjs +41 -16
  52. package/dist/machines/listbox/index.d.cts +5 -4
  53. package/dist/machines/listbox/index.d.mts +5 -4
  54. package/dist/machines/listbox/index.mjs +42 -17
  55. package/dist/machines/marquee/index.d.cts +3 -3
  56. package/dist/machines/marquee/index.d.mts +3 -3
  57. package/dist/machines/navigation-menu/index.d.cts +1 -1
  58. package/dist/machines/navigation-menu/index.d.mts +1 -1
  59. package/dist/machines/number-input/index.d.cts +1 -1
  60. package/dist/machines/number-input/index.d.mts +1 -1
  61. package/dist/machines/password-input/index.d.cts +1 -1
  62. package/dist/machines/password-input/index.d.mts +1 -1
  63. package/dist/machines/pin-input/index.d.cts +1 -1
  64. package/dist/machines/pin-input/index.d.mts +1 -1
  65. package/dist/machines/popover/index.d.cts +1 -1
  66. package/dist/machines/popover/index.d.mts +1 -1
  67. package/dist/machines/progress/index.d.cts +1 -1
  68. package/dist/machines/progress/index.d.mts +1 -1
  69. package/dist/machines/radio-group/index.d.cts +1 -1
  70. package/dist/machines/radio-group/index.d.mts +1 -1
  71. package/dist/machines/rating-group/index.d.cts +1 -1
  72. package/dist/machines/rating-group/index.d.mts +1 -1
  73. package/dist/machines/select/index.cjs +47 -18
  74. package/dist/machines/select/index.d.cts +4 -3
  75. package/dist/machines/select/index.d.mts +4 -3
  76. package/dist/machines/select/index.mjs +48 -19
  77. package/dist/machines/signature-pad/index.d.cts +1 -1
  78. package/dist/machines/signature-pad/index.d.mts +1 -1
  79. package/dist/machines/slider/index.d.cts +1 -1
  80. package/dist/machines/slider/index.d.mts +1 -1
  81. package/dist/machines/steps/index.d.cts +1 -1
  82. package/dist/machines/steps/index.d.mts +1 -1
  83. package/dist/machines/switch/index.d.cts +1 -1
  84. package/dist/machines/switch/index.d.mts +1 -1
  85. package/dist/machines/tabs/index.d.cts +1 -1
  86. package/dist/machines/tabs/index.d.mts +1 -1
  87. package/dist/machines/tags-input/index.d.cts +1 -1
  88. package/dist/machines/tags-input/index.d.mts +1 -1
  89. package/dist/machines/timer/index.d.cts +1 -1
  90. package/dist/machines/timer/index.d.mts +1 -1
  91. package/dist/machines/toast/index.d.cts +1 -1
  92. package/dist/machines/toast/index.d.mts +1 -1
  93. package/dist/machines/tooltip/index.d.cts +1 -1
  94. package/dist/machines/tooltip/index.d.mts +1 -1
  95. package/dist/machines/tour/index.d.cts +1 -1
  96. package/dist/machines/tour/index.d.mts +1 -1
  97. package/dist/machines/tree-view/index.cjs +1 -1
  98. package/dist/machines/tree-view/index.d.cts +1 -1
  99. package/dist/machines/tree-view/index.d.mts +1 -1
  100. package/dist/machines/tree-view/index.mjs +1 -1
  101. package/dist/{selection-DdNvrKbj.d.mts → selection-6urf-c8j.d.mts} +1 -1
  102. package/dist/{selection-Bb_ZSVxe.d.cts → selection-BmS2RDMq.d.cts} +1 -1
  103. package/package.json +11 -1
@@ -1,7 +1,7 @@
1
1
  import { t as createAnatomy } from "../../create-anatomy-Dr0evdYy.mjs";
2
2
  import { Cn as setCaretToEnd, H as nextTick, I as scrollIntoView, L as clickIfLink, S as query, St as isLeftClick, W as raf, _t as isComposingEvent, bn as ariaAttr, bt as isDownloadingEvent, cn as isAnchorElement, ft as getEventKey, vt as isContextMenuEvent, wt as isOpeningInNewTab, xn as dataAttr, z as observeAttributes } from "../../dom-query-BFuRs3l4.mjs";
3
3
  import { At as remove, Y as match, ft as isEqual, mt as addOrRemove, rt as isBoolean, t as ensure, u as createSplitProps } from "../../utils-VVoZ_v29.mjs";
4
- import { s as ListCollection } from "../../collection-DgzidRvr.mjs";
4
+ import { a as createSelectedItemMap, d as ListCollection, o as deriveSelectionState, s as resolveSelectedItems } from "../../collection-GSpaYJ2f.mjs";
5
5
  import { o as setup } from "../../core-CTtbW9qh.mjs";
6
6
  import "../../interact-outside-Ba50N1a5.mjs";
7
7
  import { n as trackDismissableElement } from "../../dismissable-B9k5K6f9.mjs";
@@ -92,7 +92,7 @@ function connect(service, normalize) {
92
92
  value: context.get("value"),
93
93
  valueAsString: computed("valueAsString"),
94
94
  hasSelectedItems: computed("hasSelectedItems"),
95
- selectedItems: context.get("selectedItems"),
95
+ selectedItems: computed("selectedItems"),
96
96
  collection: prop("collection"),
97
97
  multiple: !!prop("multiple"),
98
98
  disabled: !!disabled,
@@ -564,9 +564,11 @@ const machine = createMachine({
564
564
  };
565
565
  },
566
566
  initialState({ prop }) {
567
- return prop("open") || prop("defaultOpen") ? "suggesting" : "idle";
567
+ return prop("open") || prop("defaultOpen") ? "open.suggesting" : "closed.idle";
568
568
  },
569
569
  context({ prop, bindable, getContext, getEvent }) {
570
+ const initialValue = prop("value") ?? prop("defaultValue") ?? [];
571
+ const initialSelectedItems = prop("collection").findMany(initialValue);
570
572
  return {
571
573
  currentPlacement: bindable(() => ({ defaultValue: void 0 })),
572
574
  value: bindable(() => ({
@@ -578,15 +580,22 @@ const machine = createMachine({
578
580
  },
579
581
  onChange(value) {
580
582
  const context = getContext();
581
- const prevSelectedItems = context.get("selectedItems");
582
583
  const collection = prop("collection");
583
- const findItems = (vals) => vals.map((v) => prevSelectedItems.find((item) => collection.getItemValue(item) === v) || collection.find(v));
584
- const nextItems = findItems(value);
585
- const effectiveValue = prop("value") || value;
586
- context.set("selectedItems", findItems(effectiveValue));
584
+ const proposed = deriveSelectionState({
585
+ values: value,
586
+ collection,
587
+ selectedItemMap: context.get("selectedItemMap")
588
+ });
589
+ const effectiveValue = prop("value") ?? value;
590
+ const effective = effectiveValue === value ? proposed : deriveSelectionState({
591
+ values: effectiveValue,
592
+ collection,
593
+ selectedItemMap: proposed.nextSelectedItemMap
594
+ });
595
+ context.set("selectedItemMap", effective.nextSelectedItemMap);
587
596
  prop("onValueChange")?.({
588
597
  value,
589
- items: nextItems
598
+ items: proposed.selectedItems
590
599
  });
591
600
  }
592
601
  })),
@@ -629,9 +638,11 @@ const machine = createMachine({
629
638
  const highlightedValue = prop("highlightedValue");
630
639
  return { defaultValue: prop("collection").find(highlightedValue) };
631
640
  }),
632
- selectedItems: bindable(() => {
633
- const value = prop("value") || prop("defaultValue") || [];
634
- return { defaultValue: prop("collection").findMany(value) };
641
+ selectedItemMap: bindable(() => {
642
+ return { defaultValue: createSelectedItemMap({
643
+ selectedItems: initialSelectedItems,
644
+ collection: prop("collection")
645
+ }) };
635
646
  })
636
647
  };
637
648
  },
@@ -641,7 +652,12 @@ const machine = createMachine({
641
652
  autoComplete: ({ prop }) => prop("inputBehavior") === "autocomplete",
642
653
  autoHighlight: ({ prop }) => prop("inputBehavior") === "autohighlight",
643
654
  hasSelectedItems: ({ context }) => context.get("value").length > 0,
644
- valueAsString: ({ context, prop }) => prop("collection").stringifyItems(context.get("selectedItems")),
655
+ selectedItems: ({ context, prop }) => resolveSelectedItems({
656
+ values: context.get("value"),
657
+ collection: prop("collection"),
658
+ selectedItemMap: context.get("selectedItemMap")
659
+ }),
660
+ valueAsString: ({ computed, prop }) => prop("collection").stringifyItems(computed("selectedItems")),
645
661
  isCustomValue: ({ context, computed }) => context.get("inputValue") !== computed("valueAsString")
646
662
  },
647
663
  watch({ context, prop, track, action, send }) {
@@ -676,156 +692,162 @@ const machine = createMachine({
676
692
  actions: ["setInitialFocus"]
677
693
  }]),
678
694
  states: {
679
- idle: {
680
- tags: ["idle", "closed"],
681
- entry: ["scrollContentToTop", "clearHighlightedValue"],
682
- on: {
683
- "CONTROLLED.OPEN": { target: "interacting" },
684
- "TRIGGER.CLICK": [{
685
- guard: "isOpenControlled",
686
- actions: [
687
- "setInitialFocus",
688
- "highlightFirstSelectedItem",
689
- "invokeOnOpen"
690
- ]
691
- }, {
692
- target: "interacting",
693
- actions: [
694
- "setInitialFocus",
695
- "highlightFirstSelectedItem",
696
- "invokeOnOpen"
697
- ]
698
- }],
699
- "INPUT.CLICK": [{
700
- guard: "isOpenControlled",
701
- actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
702
- }, {
703
- target: "interacting",
704
- actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
705
- }],
706
- "INPUT.FOCUS": { target: "focused" },
707
- OPEN: [{
708
- guard: "isOpenControlled",
709
- actions: ["invokeOnOpen"]
710
- }, {
711
- target: "interacting",
712
- actions: ["invokeOnOpen"]
713
- }],
714
- "VALUE.CLEAR": {
715
- target: "focused",
716
- actions: [
717
- "clearInputValue",
718
- "clearSelectedItems",
719
- "setInitialFocus"
720
- ]
721
- }
722
- }
723
- },
724
- focused: {
725
- tags: ["focused", "closed"],
726
- entry: ["scrollContentToTop", "clearHighlightedValue"],
727
- on: {
728
- "CONTROLLED.OPEN": [{
729
- guard: "isChangeEvent",
730
- target: "suggesting"
731
- }, { target: "interacting" }],
732
- "INPUT.CHANGE": [
733
- {
734
- guard: and("isOpenControlled", "openOnChange"),
735
- actions: [
736
- "setInputValue",
737
- "invokeOnOpen",
738
- "highlightFirstItemIfNeeded"
739
- ]
740
- },
741
- {
742
- guard: "openOnChange",
743
- target: "suggesting",
744
- actions: [
745
- "setInputValue",
746
- "invokeOnOpen",
747
- "highlightFirstItemIfNeeded"
748
- ]
749
- },
750
- { actions: ["setInputValue"] }
751
- ],
752
- "LAYER.INTERACT_OUTSIDE": { target: "idle" },
753
- "INPUT.ESCAPE": {
754
- guard: and("isCustomValue", not("allowCustomValue")),
755
- actions: ["revertInputValue"]
756
- },
757
- "INPUT.BLUR": { target: "idle" },
758
- "INPUT.CLICK": [{
759
- guard: "isOpenControlled",
760
- actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
761
- }, {
762
- target: "interacting",
763
- actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
764
- }],
765
- "TRIGGER.CLICK": [{
766
- guard: "isOpenControlled",
767
- actions: [
768
- "setInitialFocus",
769
- "highlightFirstSelectedItem",
770
- "invokeOnOpen"
771
- ]
772
- }, {
773
- target: "interacting",
774
- actions: [
775
- "setInitialFocus",
776
- "highlightFirstSelectedItem",
777
- "invokeOnOpen"
778
- ]
779
- }],
780
- "INPUT.ARROW_DOWN": [
781
- {
782
- guard: and("isOpenControlled", "autoComplete"),
783
- actions: ["invokeOnOpen"]
784
- },
785
- {
786
- guard: "autoComplete",
787
- target: "interacting",
788
- actions: ["invokeOnOpen"]
789
- },
790
- {
791
- guard: "isOpenControlled",
792
- actions: ["highlightFirstOrSelectedItem", "invokeOnOpen"]
793
- },
794
- {
795
- target: "interacting",
796
- actions: ["highlightFirstOrSelectedItem", "invokeOnOpen"]
695
+ closed: {
696
+ tags: ["closed"],
697
+ initial: "idle",
698
+ states: {
699
+ idle: {
700
+ tags: ["idle"],
701
+ entry: ["scrollContentToTop", "clearHighlightedValue"],
702
+ on: {
703
+ "CONTROLLED.OPEN": { target: "open.interacting" },
704
+ "TRIGGER.CLICK": [{
705
+ guard: "isOpenControlled",
706
+ actions: [
707
+ "setInitialFocus",
708
+ "highlightFirstSelectedItem",
709
+ "invokeOnOpen"
710
+ ]
711
+ }, {
712
+ target: "open.interacting",
713
+ actions: [
714
+ "setInitialFocus",
715
+ "highlightFirstSelectedItem",
716
+ "invokeOnOpen"
717
+ ]
718
+ }],
719
+ "INPUT.CLICK": [{
720
+ guard: "isOpenControlled",
721
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
722
+ }, {
723
+ target: "open.interacting",
724
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
725
+ }],
726
+ "INPUT.FOCUS": { target: "focused" },
727
+ OPEN: [{
728
+ guard: "isOpenControlled",
729
+ actions: ["invokeOnOpen"]
730
+ }, {
731
+ target: "open.interacting",
732
+ actions: ["invokeOnOpen"]
733
+ }],
734
+ "VALUE.CLEAR": {
735
+ target: "focused",
736
+ actions: [
737
+ "clearInputValue",
738
+ "clearSelectedItems",
739
+ "setInitialFocus"
740
+ ]
741
+ }
797
742
  }
798
- ],
799
- "INPUT.ARROW_UP": [
800
- {
801
- guard: and("isOpenControlled", "autoComplete"),
802
- actions: ["invokeOnOpen"]
803
- },
804
- {
805
- guard: "autoComplete",
806
- target: "interacting",
807
- actions: ["invokeOnOpen"]
808
- },
809
- {
810
- guard: "isOpenControlled",
811
- actions: ["highlightLastOrSelectedItem", "invokeOnOpen"]
812
- },
813
- {
814
- target: "interacting",
815
- actions: ["highlightLastOrSelectedItem", "invokeOnOpen"]
743
+ },
744
+ focused: {
745
+ tags: ["focused"],
746
+ entry: ["scrollContentToTop", "clearHighlightedValue"],
747
+ on: {
748
+ "CONTROLLED.OPEN": [{
749
+ guard: "isChangeEvent",
750
+ target: "open.suggesting"
751
+ }, { target: "open.interacting" }],
752
+ "INPUT.CHANGE": [
753
+ {
754
+ guard: and("isOpenControlled", "openOnChange"),
755
+ actions: [
756
+ "setInputValue",
757
+ "invokeOnOpen",
758
+ "highlightFirstItemIfNeeded"
759
+ ]
760
+ },
761
+ {
762
+ guard: "openOnChange",
763
+ target: "open.suggesting",
764
+ actions: [
765
+ "setInputValue",
766
+ "invokeOnOpen",
767
+ "highlightFirstItemIfNeeded"
768
+ ]
769
+ },
770
+ { actions: ["setInputValue"] }
771
+ ],
772
+ "LAYER.INTERACT_OUTSIDE": { target: "idle" },
773
+ "INPUT.ESCAPE": {
774
+ guard: and("isCustomValue", not("allowCustomValue")),
775
+ actions: ["revertInputValue"]
776
+ },
777
+ "INPUT.BLUR": { target: "idle" },
778
+ "INPUT.CLICK": [{
779
+ guard: "isOpenControlled",
780
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
781
+ }, {
782
+ target: "open.interacting",
783
+ actions: ["highlightFirstSelectedItem", "invokeOnOpen"]
784
+ }],
785
+ "TRIGGER.CLICK": [{
786
+ guard: "isOpenControlled",
787
+ actions: [
788
+ "setInitialFocus",
789
+ "highlightFirstSelectedItem",
790
+ "invokeOnOpen"
791
+ ]
792
+ }, {
793
+ target: "open.interacting",
794
+ actions: [
795
+ "setInitialFocus",
796
+ "highlightFirstSelectedItem",
797
+ "invokeOnOpen"
798
+ ]
799
+ }],
800
+ "INPUT.ARROW_DOWN": [
801
+ {
802
+ guard: and("isOpenControlled", "autoComplete"),
803
+ actions: ["invokeOnOpen"]
804
+ },
805
+ {
806
+ guard: "autoComplete",
807
+ target: "open.interacting",
808
+ actions: ["invokeOnOpen"]
809
+ },
810
+ {
811
+ guard: "isOpenControlled",
812
+ actions: ["highlightFirstOrSelectedItem", "invokeOnOpen"]
813
+ },
814
+ {
815
+ target: "open.interacting",
816
+ actions: ["highlightFirstOrSelectedItem", "invokeOnOpen"]
817
+ }
818
+ ],
819
+ "INPUT.ARROW_UP": [
820
+ {
821
+ guard: and("isOpenControlled", "autoComplete"),
822
+ actions: ["invokeOnOpen"]
823
+ },
824
+ {
825
+ guard: "autoComplete",
826
+ target: "open.interacting",
827
+ actions: ["invokeOnOpen"]
828
+ },
829
+ {
830
+ guard: "isOpenControlled",
831
+ actions: ["highlightLastOrSelectedItem", "invokeOnOpen"]
832
+ },
833
+ {
834
+ target: "open.interacting",
835
+ actions: ["highlightLastOrSelectedItem", "invokeOnOpen"]
836
+ }
837
+ ],
838
+ OPEN: [{
839
+ guard: "isOpenControlled",
840
+ actions: ["invokeOnOpen"]
841
+ }, {
842
+ target: "open.interacting",
843
+ actions: ["invokeOnOpen"]
844
+ }],
845
+ "VALUE.CLEAR": { actions: ["clearInputValue", "clearSelectedItems"] }
816
846
  }
817
- ],
818
- OPEN: [{
819
- guard: "isOpenControlled",
820
- actions: ["invokeOnOpen"]
821
- }, {
822
- target: "interacting",
823
- actions: ["invokeOnOpen"]
824
- }],
825
- "VALUE.CLEAR": { actions: ["clearInputValue", "clearSelectedItems"] }
847
+ }
826
848
  }
827
849
  },
828
- interacting: {
850
+ open: {
829
851
  tags: ["open", "focused"],
830
852
  entry: ["setInitialFocus"],
831
853
  effects: [
@@ -837,23 +859,9 @@ const machine = createMachine({
837
859
  on: {
838
860
  "CONTROLLED.CLOSE": [{
839
861
  guard: "restoreFocus",
840
- target: "focused",
862
+ target: "closed.focused",
841
863
  actions: ["setFinalFocus"]
842
- }, { target: "idle" }],
843
- CHILDREN_CHANGE: [{
844
- guard: "isHighlightedItemRemoved",
845
- actions: ["clearHighlightedValue"]
846
- }, { actions: ["scrollToHighlightedItem"] }],
847
- "INPUT.HOME": { actions: ["highlightFirstItem"] },
848
- "INPUT.END": { actions: ["highlightLastItem"] },
849
- "INPUT.ARROW_DOWN": [{
850
- guard: and("autoComplete", "isLastItemHighlighted"),
851
- actions: ["clearHighlightedValue", "scrollContentToTop"]
852
- }, { actions: ["highlightNextItem"] }],
853
- "INPUT.ARROW_UP": [{
854
- guard: and("autoComplete", "isFirstItemHighlighted"),
855
- actions: ["clearHighlightedValue"]
856
- }, { actions: ["highlightPrevItem"] }],
864
+ }, { target: "closed.idle" }],
857
865
  "INPUT.ENTER": [
858
866
  {
859
867
  guard: and("isOpenControlled", "isCustomValue", not("hasHighlightedItem"), not("allowCustomValue")),
@@ -861,7 +869,7 @@ const machine = createMachine({
861
869
  },
862
870
  {
863
871
  guard: and("isCustomValue", not("hasHighlightedItem"), not("allowCustomValue")),
864
- target: "focused",
872
+ target: "closed.focused",
865
873
  actions: ["revertInputValue", "invokeOnClose"]
866
874
  },
867
875
  {
@@ -870,7 +878,7 @@ const machine = createMachine({
870
878
  },
871
879
  {
872
880
  guard: "closeOnSelect",
873
- target: "focused",
881
+ target: "closed.focused",
874
882
  actions: [
875
883
  "selectHighlightedItem",
876
884
  "invokeOnClose",
@@ -879,16 +887,6 @@ const machine = createMachine({
879
887
  },
880
888
  { actions: ["selectHighlightedItem"] }
881
889
  ],
882
- "INPUT.CHANGE": [{
883
- guard: "autoComplete",
884
- target: "suggesting",
885
- actions: ["setInputValue"]
886
- }, {
887
- target: "suggesting",
888
- actions: ["clearHighlightedValue", "setInputValue"]
889
- }],
890
- "ITEM.POINTER_MOVE": { actions: ["setHighlightedValue"] },
891
- "ITEM.POINTER_LEAVE": { actions: ["clearHighlightedValue"] },
892
890
  "ITEM.CLICK": [
893
891
  {
894
892
  guard: and("isOpenControlled", "closeOnSelect"),
@@ -896,7 +894,7 @@ const machine = createMachine({
896
894
  },
897
895
  {
898
896
  guard: "closeOnSelect",
899
- target: "focused",
897
+ target: "closed.focused",
900
898
  actions: [
901
899
  "selectItem",
902
900
  "invokeOnClose",
@@ -905,30 +903,11 @@ const machine = createMachine({
905
903
  },
906
904
  { actions: ["selectItem"] }
907
905
  ],
908
- "LAYER.ESCAPE": [
909
- {
910
- guard: and("isOpenControlled", "autoComplete"),
911
- actions: ["syncInputValue", "invokeOnClose"]
912
- },
913
- {
914
- guard: "autoComplete",
915
- target: "focused",
916
- actions: ["syncInputValue", "invokeOnClose"]
917
- },
918
- {
919
- guard: "isOpenControlled",
920
- actions: ["invokeOnClose"]
921
- },
922
- {
923
- target: "focused",
924
- actions: ["invokeOnClose", "setFinalFocus"]
925
- }
926
- ],
927
906
  "TRIGGER.CLICK": [{
928
907
  guard: "isOpenControlled",
929
908
  actions: ["invokeOnClose"]
930
909
  }, {
931
- target: "focused",
910
+ target: "closed.focused",
932
911
  actions: ["invokeOnClose"]
933
912
  }],
934
913
  "LAYER.INTERACT_OUTSIDE": [
@@ -938,7 +917,7 @@ const machine = createMachine({
938
917
  },
939
918
  {
940
919
  guard: and("isCustomValue", not("allowCustomValue")),
941
- target: "idle",
920
+ target: "closed.idle",
942
921
  actions: ["revertInputValue", "invokeOnClose"]
943
922
  },
944
923
  {
@@ -946,7 +925,7 @@ const machine = createMachine({
946
925
  actions: ["invokeOnClose"]
947
926
  },
948
927
  {
949
- target: "idle",
928
+ target: "closed.idle",
950
929
  actions: ["invokeOnClose"]
951
930
  }
952
931
  ],
@@ -954,7 +933,7 @@ const machine = createMachine({
954
933
  guard: "isOpenControlled",
955
934
  actions: ["invokeOnClose"]
956
935
  }, {
957
- target: "focused",
936
+ target: "closed.focused",
958
937
  actions: ["invokeOnClose", "setFinalFocus"]
959
938
  }],
960
939
  "VALUE.CLEAR": [{
@@ -965,7 +944,7 @@ const machine = createMachine({
965
944
  "invokeOnClose"
966
945
  ]
967
946
  }, {
968
- target: "focused",
947
+ target: "closed.focused",
969
948
  actions: [
970
949
  "clearInputValue",
971
950
  "clearSelectedItems",
@@ -973,156 +952,99 @@ const machine = createMachine({
973
952
  "setFinalFocus"
974
953
  ]
975
954
  }]
976
- }
977
- },
978
- suggesting: {
979
- tags: ["open", "focused"],
980
- effects: [
981
- "trackFocusVisible",
982
- "trackDismissableLayer",
983
- "scrollToHighlightedItem",
984
- "trackPlacement"
985
- ],
986
- entry: ["setInitialFocus"],
987
- on: {
988
- "CONTROLLED.CLOSE": [{
989
- guard: "restoreFocus",
990
- target: "focused",
991
- actions: ["setFinalFocus"]
992
- }, { target: "idle" }],
993
- CHILDREN_CHANGE: [
994
- {
995
- guard: and("isHighlightedItemRemoved", "hasCollectionItems", "autoHighlight"),
996
- actions: ["clearHighlightedValue", "highlightFirstItem"]
997
- },
998
- {
955
+ },
956
+ initial: "interacting",
957
+ states: {
958
+ interacting: { on: {
959
+ CHILDREN_CHANGE: [{
999
960
  guard: "isHighlightedItemRemoved",
1000
961
  actions: ["clearHighlightedValue"]
962
+ }, { actions: ["scrollToHighlightedItem"] }],
963
+ "INPUT.HOME": { actions: ["highlightFirstItem"] },
964
+ "INPUT.END": { actions: ["highlightLastItem"] },
965
+ "INPUT.ARROW_DOWN": [{
966
+ guard: and("autoComplete", "isLastItemHighlighted"),
967
+ actions: ["clearHighlightedValue", "scrollContentToTop"]
968
+ }, { actions: ["highlightNextItem"] }],
969
+ "INPUT.ARROW_UP": [{
970
+ guard: and("autoComplete", "isFirstItemHighlighted"),
971
+ actions: ["clearHighlightedValue"]
972
+ }, { actions: ["highlightPrevItem"] }],
973
+ "INPUT.CHANGE": [{
974
+ guard: "autoComplete",
975
+ target: "suggesting",
976
+ actions: ["setInputValue"]
977
+ }, {
978
+ target: "suggesting",
979
+ actions: ["clearHighlightedValue", "setInputValue"]
980
+ }],
981
+ "ITEM.POINTER_MOVE": { actions: ["setHighlightedValue"] },
982
+ "ITEM.POINTER_LEAVE": { actions: ["clearHighlightedValue"] },
983
+ "LAYER.ESCAPE": [
984
+ {
985
+ guard: and("isOpenControlled", "autoComplete"),
986
+ actions: ["syncInputValue", "invokeOnClose"]
987
+ },
988
+ {
989
+ guard: "autoComplete",
990
+ target: "closed.focused",
991
+ actions: ["syncInputValue", "invokeOnClose"]
992
+ },
993
+ {
994
+ guard: "isOpenControlled",
995
+ actions: ["invokeOnClose"]
996
+ },
997
+ {
998
+ target: "closed.focused",
999
+ actions: ["invokeOnClose", "setFinalFocus"]
1000
+ }
1001
+ ]
1002
+ } },
1003
+ suggesting: { on: {
1004
+ CHILDREN_CHANGE: [
1005
+ {
1006
+ guard: and("isHighlightedItemRemoved", "hasCollectionItems", "autoHighlight"),
1007
+ actions: ["clearHighlightedValue", "highlightFirstItem"]
1008
+ },
1009
+ {
1010
+ guard: "isHighlightedItemRemoved",
1011
+ actions: ["clearHighlightedValue"]
1012
+ },
1013
+ {
1014
+ guard: "autoHighlight",
1015
+ actions: ["highlightFirstItem"]
1016
+ }
1017
+ ],
1018
+ "INPUT.ARROW_DOWN": {
1019
+ target: "interacting",
1020
+ actions: ["highlightNextItem"]
1001
1021
  },
1002
- {
1003
- guard: "autoHighlight",
1004
- actions: ["highlightFirstItem"]
1005
- }
1006
- ],
1007
- "INPUT.ARROW_DOWN": {
1008
- target: "interacting",
1009
- actions: ["highlightNextItem"]
1010
- },
1011
- "INPUT.ARROW_UP": {
1012
- target: "interacting",
1013
- actions: ["highlightPrevItem"]
1014
- },
1015
- "INPUT.HOME": {
1016
- target: "interacting",
1017
- actions: ["highlightFirstItem"]
1018
- },
1019
- "INPUT.END": {
1020
- target: "interacting",
1021
- actions: ["highlightLastItem"]
1022
- },
1023
- "INPUT.ENTER": [
1024
- {
1025
- guard: and("isOpenControlled", "isCustomValue", not("hasHighlightedItem"), not("allowCustomValue")),
1026
- actions: ["revertInputValue", "invokeOnClose"]
1027
- },
1028
- {
1029
- guard: and("isCustomValue", not("hasHighlightedItem"), not("allowCustomValue")),
1030
- target: "focused",
1031
- actions: ["revertInputValue", "invokeOnClose"]
1032
- },
1033
- {
1034
- guard: and("isOpenControlled", "closeOnSelect"),
1035
- actions: ["selectHighlightedItem", "invokeOnClose"]
1036
- },
1037
- {
1038
- guard: "closeOnSelect",
1039
- target: "focused",
1040
- actions: [
1041
- "selectHighlightedItem",
1042
- "invokeOnClose",
1043
- "setFinalFocus"
1044
- ]
1022
+ "INPUT.ARROW_UP": {
1023
+ target: "interacting",
1024
+ actions: ["highlightPrevItem"]
1045
1025
  },
1046
- { actions: ["selectHighlightedItem"] }
1047
- ],
1048
- "INPUT.CHANGE": { actions: ["setInputValue"] },
1049
- "LAYER.ESCAPE": [{
1050
- guard: "isOpenControlled",
1051
- actions: ["invokeOnClose"]
1052
- }, {
1053
- target: "focused",
1054
- actions: ["invokeOnClose"]
1055
- }],
1056
- "ITEM.POINTER_MOVE": {
1057
- target: "interacting",
1058
- actions: ["setHighlightedValue"]
1059
- },
1060
- "ITEM.POINTER_LEAVE": { actions: ["clearHighlightedValue"] },
1061
- "LAYER.INTERACT_OUTSIDE": [
1062
- {
1063
- guard: and("isOpenControlled", "isCustomValue", not("allowCustomValue")),
1064
- actions: ["revertInputValue", "invokeOnClose"]
1026
+ "INPUT.HOME": {
1027
+ target: "interacting",
1028
+ actions: ["highlightFirstItem"]
1065
1029
  },
1066
- {
1067
- guard: and("isCustomValue", not("allowCustomValue")),
1068
- target: "idle",
1069
- actions: ["revertInputValue", "invokeOnClose"]
1030
+ "INPUT.END": {
1031
+ target: "interacting",
1032
+ actions: ["highlightLastItem"]
1070
1033
  },
1071
- {
1034
+ "INPUT.CHANGE": { actions: ["setInputValue"] },
1035
+ "LAYER.ESCAPE": [{
1072
1036
  guard: "isOpenControlled",
1073
1037
  actions: ["invokeOnClose"]
1074
- },
1075
- {
1076
- target: "idle",
1038
+ }, {
1039
+ target: "closed.focused",
1077
1040
  actions: ["invokeOnClose"]
1078
- }
1079
- ],
1080
- "TRIGGER.CLICK": [{
1081
- guard: "isOpenControlled",
1082
- actions: ["invokeOnClose"]
1083
- }, {
1084
- target: "focused",
1085
- actions: ["invokeOnClose"]
1086
- }],
1087
- "ITEM.CLICK": [
1088
- {
1089
- guard: and("isOpenControlled", "closeOnSelect"),
1090
- actions: ["selectItem", "invokeOnClose"]
1091
- },
1092
- {
1093
- guard: "closeOnSelect",
1094
- target: "focused",
1095
- actions: [
1096
- "selectItem",
1097
- "invokeOnClose",
1098
- "setFinalFocus"
1099
- ]
1041
+ }],
1042
+ "ITEM.POINTER_MOVE": {
1043
+ target: "interacting",
1044
+ actions: ["setHighlightedValue"]
1100
1045
  },
1101
- { actions: ["selectItem"] }
1102
- ],
1103
- CLOSE: [{
1104
- guard: "isOpenControlled",
1105
- actions: ["invokeOnClose"]
1106
- }, {
1107
- target: "focused",
1108
- actions: ["invokeOnClose", "setFinalFocus"]
1109
- }],
1110
- "VALUE.CLEAR": [{
1111
- guard: "isOpenControlled",
1112
- actions: [
1113
- "clearInputValue",
1114
- "clearSelectedItems",
1115
- "invokeOnClose"
1116
- ]
1117
- }, {
1118
- target: "focused",
1119
- actions: [
1120
- "clearInputValue",
1121
- "clearSelectedItems",
1122
- "invokeOnClose",
1123
- "setFinalFocus"
1124
- ]
1125
- }]
1046
+ "ITEM.POINTER_LEAVE": { actions: ["clearHighlightedValue"] }
1047
+ } }
1126
1048
  }
1127
1049
  }
1128
1050
  },
@@ -1494,10 +1416,12 @@ const machine = createMachine({
1494
1416
  const { context, prop } = params;
1495
1417
  const collection = prop("collection");
1496
1418
  const value = context.get("value");
1497
- const selectedItems = value.map((v) => {
1498
- return context.get("selectedItems").find((item) => collection.getItemValue(item) === v) || collection.find(v);
1419
+ const next = deriveSelectionState({
1420
+ values: value,
1421
+ collection,
1422
+ selectedItemMap: context.get("selectedItemMap")
1499
1423
  });
1500
- context.set("selectedItems", selectedItems);
1424
+ context.set("selectedItemMap", next.nextSelectedItemMap);
1501
1425
  const inputValue = match(prop("selectionBehavior"), {
1502
1426
  preserve: context.get("inputValue"),
1503
1427
  replace: collection.stringifyMany(value),