This repository was archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathindex.tsx
More file actions
188 lines (149 loc) · 4.64 KB
/
index.tsx
File metadata and controls
188 lines (149 loc) · 4.64 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import React, {CSSProperties} from "react";
import {Tag} from "./components/Tag";
import {classSelectors} from "./utils/selectors";
type Tags = string[];
export interface ReactTagInputProps {
tags: Tags;
onChange: (tags: Tags) => void;
placeholder?: string;
maxTags?: number;
validator?: (val: string) => boolean;
editable?: boolean;
readOnly?: boolean;
removeOnBackspace?: boolean;
buttonVariant?: boolean;
buttonStyle?: CSSProperties;
removeButtonText?: (() => any) | string;
addButtonText?: (() => any) | string;
}
interface State {
input: string;
}
export default class ReactTagInput extends React.Component<ReactTagInputProps, State> {
state = {input: ""};
// Ref for input element
inputRef: React.RefObject<HTMLInputElement> = React.createRef();
onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
this.setState({input: e.target.value});
}
onInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
const {input} = this.state;
const {validator, removeOnBackspace} = this.props;
// On enter
if (e.keyCode === 13) {
// Prevent form submission if tag input is nested in <form>
e.preventDefault();
// If input is blank, do nothing
if (input === "") {
return;
}
// Check if input is valid
const valid = validator !== undefined ? validator(input) : true;
if (!valid) {
return;
}
// Add input to tag list
this.addTag(input);
}
// On backspace or delete
else if (removeOnBackspace && (e.keyCode === 8 || e.keyCode === 46)) {
// If currently typing, do nothing
if (input !== "") {
return;
}
// If input is blank, remove previous tag
this.removeTag(this.props.tags.length - 1);
}
}
onButtonAdd = () => {
const {input} = this.state;
const {validator} = this.props;
// If input is blank, do nothing
if (input === "") {
return;
}
// Check if input is valid
const valid = validator !== undefined ? validator(input) : true;
if (!valid) {
return;
}
this.addTag(input);
}
onButtonDelete = () => {
const {input} = this.state;
if (input !== "") {
return;
}
this.removeTag(this.props.tags.length - 1);
}
addTag = (value: string) => {
const tags = [...this.props.tags];
if (!tags.includes(value)) {
tags.push(value);
this.props.onChange(tags);
}
this.setState({input: ""});
}
removeTag = (i: number) => {
const tags = [...this.props.tags];
tags.splice(i, 1);
this.props.onChange(tags);
}
updateTag = (i: number, value: string) => {
const tags = [...this.props.tags];
const numOccurencesOfValue = tags.reduce((prev, currentValue, index) => prev + (currentValue === value && index !== i ? 1 : 0), 0);
if (numOccurencesOfValue > 0) {
tags.splice(i, 1);
} else {
tags[i] = value;
}
this.props.onChange(tags);
}
render() {
const {input} = this.state;
const {tags, placeholder, maxTags, editable, readOnly, validator, removeOnBackspace, buttonVariant, addButtonText, removeButtonText, buttonStyle} = this.props;
const maxTagsReached = maxTags !== undefined ? tags.length >= maxTags : false;
const isEditable = readOnly ? false : (editable || false);
const showInput = !readOnly && !maxTagsReached;
// @ts-ignore
// @ts-ignore
return (
<div className={classSelectors.wrapper}>
{tags.map((tag, i) => (
<Tag
key={i}
value={tag}
index={i}
editable={isEditable}
readOnly={readOnly || false}
inputRef={this.inputRef}
update={this.updateTag}
remove={this.removeTag}
validator={validator}
removeOnBackspace={removeOnBackspace}
/>
))}
{showInput &&
<input
ref={this.inputRef}
value={input}
className={classSelectors.input}
placeholder={ tags.length === 0 ? placeholder || "Type and press enter" : "" }
onChange={this.onInputChange}
onKeyDown={this.onInputKeyDown}
/>
}
{buttonVariant &&
<>
<button style={buttonStyle} onClick={this.onButtonDelete}>
{ typeof removeButtonText === "string" ? removeButtonText : removeButtonText ? removeButtonText() : "Remove"}
</button>
<button style={buttonStyle} onClick={this.onButtonAdd}>
{ typeof addButtonText === "string" ? addButtonText : addButtonText ? addButtonText() : "Add"}
</button>
</>
}
</div>
);
}
}