-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathChipsSNCF.tsx
92 lines (85 loc) · 2.29 KB
/
ChipsSNCF.tsx
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { useState } from 'react';
import nextId from 'react-id-generator';
enum colorClasses {
primary = 'bg-primary',
secondary = 'bg-secondary',
purple = 'bg-purple',
pink = 'bg-pink',
red = 'bg-red',
orange = 'bg-orange',
yellow = 'bg-yellow text-dark',
green = 'bg-green text-dark',
teal = 'bg-teal text-dark',
cyan = 'bg-cyan',
white = 'bg-white text-dark',
}
type Props = {
addTag: (tag: string) => void;
tags?: string[];
title?: JSX.Element | string;
removeTag: (tagIdx: number) => void;
color?: string;
};
export default function ChipsSNCF({
addTag,
tags = [],
title,
removeTag,
color = 'primary',
}: Props) {
const [chipInputValue, setChipInputValue] = useState('');
const chip = (label: string, idx: number) => {
const chipColor = colorClasses[color as keyof typeof colorClasses];
return (
<div role="list" key={nextId()}>
<div className="chips-group" role="listitem">
<span
data-testid="scenario-details-tag"
className={`chips chips-label pr-1 ${chipColor}`}
>
{label}
</span>
<button
type="button"
className={`chips chips-btn chips-only-icon ${chipColor}`}
aria-label="remove tag"
onClick={() => removeTag(idx)}
>
<i className="icons-close" aria-hidden="true" />
</button>
</div>
</div>
);
};
const validateInput = (
e: React.KeyboardEvent<HTMLInputElement> & { target: HTMLInputElement }
) => {
if (e.key === 'Enter' && e.target) {
addTag(e.target.value);
setChipInputValue('');
}
};
const chipsID = `chipsSNCF${nextId()}`;
return (
<div className="chips-container">
{title && (
<label className="font-weight-medium mb-2" htmlFor={chipsID}>
{title}
</label>
)}
<div className="form-chips-container">
{tags && tags.map((label, idx) => chip(label, idx))}
<input
data-role="typewriter"
type="text"
data-testid="chips-input"
className="chips-input"
id={chipsID}
onKeyDown={validateInput}
value={chipInputValue}
onChange={(e) => setChipInputValue(e.target.value)}
/>
</div>
</div>
);
}