regex tweaks

This commit is contained in:
Josh Hawkins 2024-09-18 08:59:54 -05:00
parent 03707d06d3
commit 176dc0cf3a

View File

@ -196,10 +196,6 @@ export default function InputWithTags({
const handleInputChange = useCallback( const handleInputChange = useCallback(
(value: string) => { (value: string) => {
if (value === "") {
return;
}
setInputValue(value); setInputValue(value);
const words = value.split(/[\s,]+/); const words = value.split(/[\s,]+/);
@ -255,7 +251,6 @@ export default function InputWithTags({
const handleClearInput = useCallback(() => { const handleClearInput = useCallback(() => {
setInputFocused(false); setInputFocused(false);
// setInputValue("");
resetSuggestions(""); resetSuggestions("");
setSearch(""); setSearch("");
inputRef?.current?.blur(); inputRef?.current?.blur();
@ -278,13 +273,24 @@ export default function InputWithTags({
if (currentFilterType) { if (currentFilterType) {
// Apply the selected suggestion to the current filter type // Apply the selected suggestion to the current filter type
createFilter(currentFilterType, suggestion); createFilter(currentFilterType, suggestion);
setInputValue((prev) => setInputValue((prev) => {
prev.replace(`${currentFilterType}:`, "").trim(), const regex = new RegExp(`${currentFilterType}:[^\\s,]*`, "g");
); return prev.replace(regex, "").trim();
});
} else if (suggestion in allSuggestions) { } else if (suggestion in allSuggestions) {
// Set the suggestion as a new filter type // Set the suggestion as a new filter type
setCurrentFilterType(suggestion as FilterType); setCurrentFilterType(suggestion as FilterType);
setInputValue((prev) => `${prev}${suggestion}:`); setInputValue((prev) => {
// Remove any partial match of the filter type, including incomplete matches
const words = prev.split(/\s+/);
const lastWord = words[words.length - 1];
if (lastWord && suggestion.startsWith(lastWord.toLowerCase())) {
words[words.length - 1] = suggestion + ":";
} else {
words.push(suggestion + ":");
}
return words.join(" ").trim();
});
} else { } else {
// Add the suggestion as a standalone word // Add the suggestion as a standalone word
setInputValue((prev) => `${prev}${suggestion} `); setInputValue((prev) => `${prev}${suggestion} `);