forked from FindFirst-Development/FindFirst-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagInput.tsx
More file actions
68 lines (64 loc) · 1.87 KB
/
TagInput.tsx
File metadata and controls
68 lines (64 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { KeyboardEvent } from "react";
import style from "./bookmarkCard.module.scss";
const TagInput = (props: {
tags: readonly string[];
inputValue: string;
setInputValue: (val: string) => void;
onDeleteTag: (index: number) => void;
onPushTag: (tag: string) => void;
testIdPrefix: `bk-${string}-` | `new-bk-`;
}) => {
function onKeyDown(e: KeyboardEvent) {
const { key } = e;
const trimmedInput = props.inputValue.trim();
if (
// Add tag via space bar or enter
(key === "Enter" || key === "Space" || key === " ") &&
trimmedInput.length &&
!props.tags.includes(trimmedInput)
) {
e.preventDefault();
props.onPushTag(props.inputValue);
props.setInputValue("");
}
// backspace delete
if (key === "Backspace" && !props.inputValue.length && props.tags.length) {
e.preventDefault();
const tagsCopy = [...props.tags];
const poppedTag = tagsCopy.pop();
if (poppedTag) {
props.onDeleteTag(props.tags.length - 1);
props.setInputValue(poppedTag);
}
}
}
return (
<div className={style.container}>
{props.tags.map((tag, index) => (
<button
key={tag}
onClick={() => props.onDeleteTag(index)}
onContextMenu={(e) => {
e.preventDefault();
props.onDeleteTag(index);
}}
type="button"
className={style.pillButton}
data-testid={`${props.testIdPrefix}tag-${tag}`}
>
{tag}
<i className="xtag bi bi-journal-x"></i>
</button>
))}
<input
className={style.input}
value={props.inputValue}
onChange={(e) => props.setInputValue(e.target.value)}
onKeyDown={onKeyDown}
placeholder="Enter a tag"
data-testid={`${props.testIdPrefix}tag-input`}
/>
</div>
);
};
export default TagInput;