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