@guardian/interactive-component-library 0.1.0-alpha.39 → 0.1.0-alpha.40

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.
@@ -1078,14 +1078,14 @@
1078
1078
  };
1079
1079
  function ArrowButton({
1080
1080
  direction = "right",
1081
- disabled = false,
1081
+ disabled: disabled2 = false,
1082
1082
  styles: styles2,
1083
1083
  onClick
1084
1084
  }) {
1085
1085
  styles2 = mergeStyles(defaultStyles$j, styles2);
1086
1086
  return jsxRuntime.jsx("button", {
1087
1087
  className: styles2.button,
1088
- disabled,
1088
+ disabled: disabled2,
1089
1089
  onClick,
1090
1090
  children: jsxRuntime.jsxs("svg", {
1091
1091
  className: [styles2.icon, direction].join(" "),
@@ -2080,14 +2080,15 @@
2080
2080
  })
2081
2081
  });
2082
2082
  }
2083
- const searchContainer = "_searchContainer_1px2g_1";
2084
- const input = "_input_1px2g_5";
2085
- const searchIcon = "_searchIcon_1px2g_27";
2086
- const clearButton = "_clearButton_1px2g_36";
2087
- const suggestions = "_suggestions_1px2g_42";
2088
- const suggestion = "_suggestion_1px2g_42";
2089
- const selected$1 = "_selected_1px2g_68";
2090
- const highlighted = "_highlighted_1px2g_72";
2083
+ const searchContainer = "_searchContainer_1kj0x_1";
2084
+ const input = "_input_1kj0x_5";
2085
+ const searchIcon = "_searchIcon_1kj0x_27";
2086
+ const clearButton = "_clearButton_1kj0x_36";
2087
+ const suggestions = "_suggestions_1kj0x_42";
2088
+ const suggestion = "_suggestion_1kj0x_42";
2089
+ const disabled = "_disabled_1kj0x_68";
2090
+ const selected$1 = "_selected_1kj0x_73";
2091
+ const highlighted = "_highlighted_1kj0x_77";
2091
2092
  const defaultStyles$5 = {
2092
2093
  searchContainer,
2093
2094
  input,
@@ -2095,6 +2096,7 @@
2095
2096
  clearButton,
2096
2097
  suggestions,
2097
2098
  suggestion,
2099
+ disabled,
2098
2100
  selected: selected$1,
2099
2101
  highlighted
2100
2102
  };
@@ -2114,18 +2116,34 @@
2114
2116
  const [selectedIndex, setSelectedIndex] = hooks.useState(-1);
2115
2117
  const [suggestions2, setSuggestions] = hooks.useState();
2116
2118
  const [showSuggestions, setShowSuggestions] = hooks.useState(true);
2117
- function onKeyDown(event) {
2119
+ async function updateSelectedIndex(diff) {
2120
+ setSelectedIndex((currentIndex) => {
2121
+ const newIndex = Math.max(Math.min(currentIndex + diff, suggestions2.length - 1), -1);
2122
+ if (newIndex >= 0 && suggestions2[newIndex].disabled) {
2123
+ return currentIndex;
2124
+ }
2125
+ return newIndex;
2126
+ });
2127
+ }
2128
+ async function onKeyDown(event) {
2118
2129
  if (event.key === "ArrowDown") {
2119
2130
  event.preventDefault();
2120
- setSelectedIndex((currentIndex) => Math.min(currentIndex + 1, suggestions2.length - 1));
2131
+ updateSelectedIndex(1);
2121
2132
  } else if (event.key === "ArrowUp") {
2122
2133
  event.preventDefault();
2123
- setSelectedIndex((currentIndex) => Math.max(currentIndex - 1, -1));
2134
+ updateSelectedIndex(-1);
2124
2135
  } else if (event.key === "Enter" && selectedIndex >= 0) {
2125
2136
  event.preventDefault();
2126
2137
  onSelectSuggestion(suggestions2[selectedIndex]);
2127
2138
  } else if (event.key === "Enter") {
2128
- onSubmit(inputRef.current.value);
2139
+ event.preventDefault();
2140
+ if (onSubmit) {
2141
+ const shouldBlur = await onSubmit(inputRef.current.value);
2142
+ if (shouldBlur) {
2143
+ inputRef.current.blur();
2144
+ }
2145
+ }
2146
+ } else if (event.key === "Escape") {
2129
2147
  inputRef.current.blur();
2130
2148
  }
2131
2149
  }
@@ -2203,18 +2221,19 @@
2203
2221
  className: styles2.suggestions,
2204
2222
  "aria-label": "Search suggestions",
2205
2223
  children: suggestions2.map((d2, index) => {
2206
- return jsxRuntime.jsx("li", {
2224
+ const shouldHighlight = !d2.disabled;
2225
+ return jsxRuntime.jsxs("li", {
2207
2226
  "aria-label": d2.text,
2208
- className: [styles2.suggestion, index === selectedIndex && styles2.selected].join(" "),
2227
+ className: [styles2.suggestion, index === selectedIndex && styles2.selected, d2.disabled && styles2.disabled].join(" "),
2209
2228
  onMouseDown: (e) => e.preventDefault(),
2210
2229
  onMouseOver: () => onMouseOver(d2, index),
2211
2230
  onClick: () => {
2212
2231
  onSelect(d2);
2213
2232
  },
2214
- children: d2.text.split(new RegExp(`(${highlightText})`, "ig")).map((part, i) => i % 2 === 1 ? jsxRuntime.jsx("span", {
2233
+ children: [shouldHighlight && d2.text.split(new RegExp(`(${highlightText})`, "ig")).map((part, i) => i % 2 === 1 ? jsxRuntime.jsx("span", {
2215
2234
  className: styles2.highlighted,
2216
2235
  children: part
2217
- }, i) : part)
2236
+ }, i) : part), !shouldHighlight && d2.text]
2218
2237
  }, index);
2219
2238
  })
2220
2239
  });