@xqmsg/ui-core 0.16.0 → 0.16.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.
@@ -5,6 +5,7 @@ export interface DropdownProps {
5
5
  options: FieldOptions;
6
6
  dropdownRef: RefObject<HTMLDivElement>;
7
7
  position: 'top' | 'bottom';
8
+ optionIndex?: number | null;
8
9
  }
9
10
  /**
10
11
  * A functional React component utilized to render the `Dropdown` component
@@ -513,7 +513,8 @@ var Dropdown = function Dropdown(_ref) {
513
513
  var onSelectItem = _ref.onSelectItem,
514
514
  options = _ref.options,
515
515
  dropdownRef = _ref.dropdownRef,
516
- position = _ref.position;
516
+ position = _ref.position,
517
+ optionIndex = _ref.optionIndex;
517
518
  var DropdownContent = React.useMemo(function () {
518
519
  return options.map(function (option, idx) {
519
520
  return /*#__PURE__*/React__default.createElement(React__default.Fragment, null, option.value === 'section_header' && options[idx + 1] && options[idx + 1].value !== 'section_header' && /*#__PURE__*/React__default.createElement(react.Box, {
@@ -540,18 +541,19 @@ var Dropdown = function Dropdown(_ref) {
540
541
  px: "8px",
541
542
  py: "4px",
542
543
  width: "100%",
543
- color: colors.label.primary.light,
544
+ color: optionIndex === idx ? colors.label.primary.dark : colors.label.primary.light,
544
545
  _hover: {
545
546
  color: colors.label.primary.dark,
546
547
  bg: colors.fill.action,
547
548
  borderRadius: '4px',
548
549
  width: '100%'
549
550
  },
550
- bg: "inherit",
551
- whiteSpace: "nowrap"
551
+ bg: optionIndex === idx ? colors.fill.action : 'inherit',
552
+ whiteSpace: "nowrap",
553
+ id: option.value
552
554
  }, option.label));
553
555
  });
554
- }, [onSelectItem, options]);
556
+ }, [onSelectItem, optionIndex, options]);
555
557
  return /*#__PURE__*/React__default.createElement(react.Flex, {
556
558
  flexDirection: "column",
557
559
  ref: dropdownRef,
@@ -640,9 +642,21 @@ var StackedSelect = /*#__PURE__*/React__default.forwardRef(function (_ref2, _ref
640
642
  selectedOption = _useState2[0],
641
643
  setSelectedOption = _useState2[1];
642
644
 
643
- var _useState3 = React.useState('top'),
644
- position = _useState3[0],
645
- setPosition = _useState3[1];
645
+ var _useState3 = React.useState(null),
646
+ optionIndex = _useState3[0],
647
+ setOptionIndex = _useState3[1];
648
+
649
+ var _useState4 = React.useState('top'),
650
+ position = _useState4[0],
651
+ setPosition = _useState4[1];
652
+
653
+ var _useState5 = React.useState(''),
654
+ searchValue = _useState5[0],
655
+ setSearchValue = _useState5[1];
656
+
657
+ var _useState6 = React.useState(''),
658
+ debouncedSearchValue = _useState6[0],
659
+ setDebouncedSearchValue = _useState6[1];
646
660
 
647
661
  var boundingClientRect = (_dropdownRef$current = dropdownRef.current) == null ? void 0 : _dropdownRef$current.getBoundingClientRect();
648
662
  React.useEffect(function () {
@@ -677,6 +691,92 @@ var StackedSelect = /*#__PURE__*/React__default.forwardRef(function (_ref2, _ref
677
691
  setIsFocussed(false);
678
692
  };
679
693
 
694
+ var handleOnKeyDown = function handleOnKeyDown(e) {
695
+ var initialOptionIndex = options[0].value === 'section_header' ? 1 : 0;
696
+
697
+ if (!isFocussed && (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
698
+ setIsFocussed(true);
699
+ return setOptionIndex(initialOptionIndex);
700
+ }
701
+
702
+ if (isFocussed) {
703
+ if (optionIndex === null && (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
704
+ return setOptionIndex(initialOptionIndex);
705
+ }
706
+
707
+ if (e.key === 'ArrowUp' && optionIndex !== null && optionIndex > 0) {
708
+ var _dropdownMenuRef$curr;
709
+
710
+ var incrementValue = options[optionIndex - 1] && options[optionIndex - 1].value === 'section_header' ? 2 : 1;
711
+ setOptionIndex(optionIndex - incrementValue);
712
+ return (_dropdownMenuRef$curr = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr.scrollTo({
713
+ top: optionIndex * 24,
714
+ behavior: 'smooth'
715
+ });
716
+ }
717
+
718
+ if (e.key === 'ArrowDown' && optionIndex !== null && optionIndex < options.length) {
719
+ var _dropdownMenuRef$curr2;
720
+
721
+ var _incrementValue = options[optionIndex + 1] && options[optionIndex + 1].value === 'section_header' ? 2 : 1;
722
+
723
+ setOptionIndex(optionIndex + _incrementValue);
724
+ return (_dropdownMenuRef$curr2 = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr2.scrollTo({
725
+ top: optionIndex * 24,
726
+ behavior: 'smooth'
727
+ });
728
+ }
729
+
730
+ if (e.key === 'Enter' && optionIndex !== null) {
731
+ var option = options.find(function (_, idx) {
732
+ return optionIndex === idx;
733
+ });
734
+ if (!option) return;
735
+
736
+ if (handleOnChange) {
737
+ handleOnChange(option.value);
738
+ }
739
+
740
+ setSelectedOption(option == null ? void 0 : option.label);
741
+ setValue(name, option.value, {
742
+ shouldDirty: true,
743
+ shouldValidate: true
744
+ });
745
+ return setIsFocussed(false);
746
+ }
747
+
748
+ if (e.key === 'Tab') {
749
+ return setIsFocussed(false);
750
+ }
751
+ }
752
+ };
753
+
754
+ React.useEffect(function () {
755
+ if (searchValue.length) {
756
+ var _dropdownMenuRef$curr3;
757
+
758
+ var idx = options.findIndex(function (option) {
759
+ return option.label.substring(0, searchValue.length).toLowerCase() === searchValue.toLowerCase();
760
+ });
761
+ (_dropdownMenuRef$curr3 = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr3.scrollTo({
762
+ top: idx * 24,
763
+ behavior: 'smooth'
764
+ });
765
+ setSearchValue('');
766
+ setDebouncedSearchValue('');
767
+ }
768
+ }, [options, searchValue]);
769
+ var updateSearchValue = React.useMemo(function () {
770
+ return lodash.debounce(function (val) {
771
+ setSearchValue(val);
772
+ }, 1000);
773
+ }, []);
774
+
775
+ var update = function update(value) {
776
+ updateSearchValue(value);
777
+ setDebouncedSearchValue(value);
778
+ };
779
+
680
780
  return /*#__PURE__*/React__default.createElement(react.Box, {
681
781
  ref: dropdownRef,
682
782
  position: "relative"
@@ -694,24 +794,10 @@ var StackedSelect = /*#__PURE__*/React__default.forwardRef(function (_ref2, _ref
694
794
  value: selectedOption,
695
795
  disabled: disabled,
696
796
  autoComplete: "off",
697
- onKeyDown: function onKeyDown(e) {
698
- if (isFocussed) {
699
- var _dropdownMenuRef$curr;
700
-
701
- if (e.key === 'Tab') {
702
- return setIsFocussed(false);
703
- }
704
-
705
- var idx = options.findIndex(function (option) {
706
- return option.label[0].toLocaleLowerCase() === e.key;
707
- });
708
- console.log(idx);
709
- (_dropdownMenuRef$curr = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr.scrollTo({
710
- top: idx * 27,
711
- behavior: 'smooth'
712
- });
713
- }
714
- }
797
+ onChange: function onChange(e) {
798
+ return update(debouncedSearchValue.concat(e.target.value));
799
+ },
800
+ onKeyDown: handleOnKeyDown
715
801
  })), /*#__PURE__*/React__default.createElement(react.InputRightElement, {
716
802
  cursor: disabled ? 'not-allowed' : 'pointer',
717
803
  onClick: function onClick() {
@@ -727,7 +813,8 @@ var StackedSelect = /*#__PURE__*/React__default.forwardRef(function (_ref2, _ref
727
813
  onSelectItem: function onSelectItem(option) {
728
814
  return handleOnSelectItem(option);
729
815
  },
730
- options: options
816
+ options: options,
817
+ optionIndex: optionIndex
731
818
  }));
732
819
  });
733
820
 
@@ -833,10 +920,26 @@ var StackedMultiSelect = /*#__PURE__*/React__default.forwardRef(function (_ref2,
833
920
  shouldSideScroll = _useState4[0],
834
921
  setShouldSideScroll = _useState4[1];
835
922
 
836
- var _useState5 = React.useState('top'),
837
- position = _useState5[0],
838
- setPosition = _useState5[1];
923
+ var _useState5 = React.useState(null),
924
+ optionIndex = _useState5[0],
925
+ setOptionIndex = _useState5[1];
839
926
 
927
+ var _useState6 = React.useState('top'),
928
+ position = _useState6[0],
929
+ setPosition = _useState6[1];
930
+
931
+ var _useState7 = React.useState(''),
932
+ searchValue = _useState7[0],
933
+ setSearchValue = _useState7[1];
934
+
935
+ var _useState8 = React.useState(''),
936
+ debouncedSearchValue = _useState8[0],
937
+ setDebouncedSearchValue = _useState8[1];
938
+
939
+ console.log({
940
+ searchValue: searchValue,
941
+ debouncedSearchValue: debouncedSearchValue
942
+ });
840
943
  var boundingClientRect = (_dropdownRef$current = dropdownRef.current) == null ? void 0 : _dropdownRef$current.getBoundingClientRect();
841
944
  React.useEffect(function () {
842
945
  if (window.innerHeight - ((boundingClientRect == null ? void 0 : boundingClientRect.y) + 240) >= 0) {
@@ -916,26 +1019,89 @@ var StackedMultiSelect = /*#__PURE__*/React__default.forwardRef(function (_ref2,
916
1019
  });
917
1020
  };
918
1021
 
919
- return /*#__PURE__*/React__default.createElement(react.Box, {
920
- ref: dropdownRef,
921
- position: "relative",
922
- onKeyDown: function onKeyDown(e) {
923
- if (isFocussed) {
924
- var _dropdownMenuRef$curr;
1022
+ var handleOnKeyDown = function handleOnKeyDown(e) {
1023
+ var initialOptionIndex = options[0].value === 'section_header' ? 1 : 0;
925
1024
 
926
- if (e.key === 'Tab') {
927
- return setIsFocussed(false);
928
- }
1025
+ if (!isFocussed && (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
1026
+ setIsFocussed(true);
1027
+ return setOptionIndex(initialOptionIndex);
1028
+ }
929
1029
 
930
- var idx = options.findIndex(function (option) {
931
- return option.label[0].toLocaleLowerCase() === e.key;
1030
+ if (isFocussed) {
1031
+ if (optionIndex === null && (e.key === 'Enter' || e.key === 'ArrowUp' || e.key === 'ArrowDown')) {
1032
+ return setOptionIndex(initialOptionIndex);
1033
+ }
1034
+
1035
+ if (e.key === 'ArrowUp' && optionIndex !== null && optionIndex > 0) {
1036
+ var _dropdownMenuRef$curr;
1037
+
1038
+ var incrementValue = localOptions[optionIndex - 1] && localOptions[optionIndex - 1].value === 'section_header' ? 2 : 1;
1039
+ setOptionIndex(optionIndex - incrementValue);
1040
+ return (_dropdownMenuRef$curr = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr.scrollTo({
1041
+ top: optionIndex * 24,
1042
+ behavior: 'smooth'
932
1043
  });
933
- (_dropdownMenuRef$curr = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr.scrollTo({
934
- top: idx * 27,
1044
+ }
1045
+
1046
+ if (e.key === 'ArrowDown' && optionIndex !== null && optionIndex < localOptions.length) {
1047
+ var _dropdownMenuRef$curr2;
1048
+
1049
+ var _incrementValue = localOptions[optionIndex + 1] && localOptions[optionIndex + 1].value === 'section_header' ? 2 : 1;
1050
+
1051
+ setOptionIndex(optionIndex + _incrementValue);
1052
+ return (_dropdownMenuRef$curr2 = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr2.scrollTo({
1053
+ top: optionIndex * 24,
935
1054
  behavior: 'smooth'
936
1055
  });
937
1056
  }
1057
+
1058
+ if (e.key === 'Enter' && optionIndex !== null) {
1059
+ var option = localOptions.find(function (_, idx) {
1060
+ return optionIndex === idx;
1061
+ });
1062
+ if (!option) return;
1063
+ handleChange(option);
1064
+ return setIsFocussed(false);
1065
+ }
1066
+
1067
+ if (e.key === 'Tab') {
1068
+ return setIsFocussed(false);
1069
+ }
1070
+
1071
+ return update(debouncedSearchValue.concat(e.key));
1072
+ }
1073
+ };
1074
+
1075
+ React.useEffect(function () {
1076
+ if (searchValue.length) {
1077
+ var _dropdownMenuRef$curr3;
1078
+
1079
+ var idx = options.findIndex(function (option) {
1080
+ return option.label.substring(0, searchValue.length).toLowerCase() === searchValue.toLowerCase();
1081
+ });
1082
+ (_dropdownMenuRef$curr3 = dropdownMenuRef.current) == null ? void 0 : _dropdownMenuRef$curr3.scrollTo({
1083
+ top: idx * 24,
1084
+ behavior: 'smooth'
1085
+ });
1086
+ setSearchValue('');
1087
+ setDebouncedSearchValue('');
938
1088
  }
1089
+ }, [options, searchValue]);
1090
+ var updateSearchValue = React.useMemo(function () {
1091
+ return lodash.debounce(function (val) {
1092
+ setSearchValue(val);
1093
+ }, 1000);
1094
+ }, []);
1095
+
1096
+ var update = function update(value) {
1097
+ updateSearchValue(value);
1098
+ setDebouncedSearchValue(value);
1099
+ };
1100
+
1101
+ return /*#__PURE__*/React__default.createElement(react.Box, {
1102
+ ref: dropdownRef,
1103
+ position: "relative",
1104
+ onKeyDown: handleOnKeyDown
939
1105
  }, /*#__PURE__*/React__default.createElement(react.Flex, {
940
1106
  fontSize: "13px",
941
1107
  h: "26px",
@@ -1018,7 +1184,8 @@ var StackedMultiSelect = /*#__PURE__*/React__default.forwardRef(function (_ref2,
1018
1184
  return handleChange(option);
1019
1185
  },
1020
1186
  options: localOptions,
1021
- position: position
1187
+ position: position,
1188
+ optionIndex: optionIndex
1022
1189
  }));
1023
1190
  });
1024
1191