@sproutsocial/seeds-react-token-input 1.0.0
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.
- package/.eslintignore +6 -0
- package/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +21 -0
- package/CHANGELOG.md +13 -0
- package/dist/esm/index.js +441 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/index.d.mts +104 -0
- package/dist/index.d.ts +104 -0
- package/dist/index.js +478 -0
- package/dist/index.js.map +1 -0
- package/jest.config.js +9 -0
- package/package.json +49 -0
- package/src/TokenInput.stories.tsx +235 -0
- package/src/TokenInput.tsx +302 -0
- package/src/TokenInputTypes.ts +119 -0
- package/src/TokenScreenReaderStatus.tsx +46 -0
- package/src/__tests__/TokenInput.test.tsx +672 -0
- package/src/__tests__/TokenInput.typetest.tsx +137 -0
- package/src/index.ts +5 -0
- package/src/styled.d.ts +7 -0
- package/src/styles.ts +136 -0
- package/src/util.ts +22 -0
- package/tsconfig.json +9 -0
- package/tsup.config.ts +12 -0
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
yarn run v1.22.22
|
|
2
|
+
$ tsup --dts
|
|
3
|
+
CLI Building entry: src/index.ts
|
|
4
|
+
CLI Using tsconfig: tsconfig.json
|
|
5
|
+
CLI tsup v8.0.2
|
|
6
|
+
CLI Using tsup config: /home/runner/work/seeds/seeds/seeds-react/seeds-react-token-input/tsup.config.ts
|
|
7
|
+
CLI Target: es2022
|
|
8
|
+
CLI Cleaning output folder
|
|
9
|
+
CJS Build start
|
|
10
|
+
ESM Build start
|
|
11
|
+
CJS dist/index.js 14.75 KB
|
|
12
|
+
CJS dist/index.js.map 26.41 KB
|
|
13
|
+
CJS ⚡️ Build success in 232ms
|
|
14
|
+
ESM dist/esm/index.js 12.11 KB
|
|
15
|
+
ESM dist/esm/index.js.map 26.25 KB
|
|
16
|
+
ESM ⚡️ Build success in 231ms
|
|
17
|
+
DTS Build start
|
|
18
|
+
DTS ⚡️ Build success in 32286ms
|
|
19
|
+
DTS dist/index.d.ts 5.00 KB
|
|
20
|
+
DTS dist/index.d.mts 5.00 KB
|
|
21
|
+
Done in 37.80s.
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @sproutsocial/seeds-react-token-input
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- bd8d03d: Migrate TokenInput from Racine to seeds-react-token-input
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [bd8d03d]
|
|
12
|
+
- @sproutsocial/seeds-react-token@1.0.0
|
|
13
|
+
- @sproutsocial/seeds-react-input@1.3.0
|
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
// src/TokenInput.tsx
|
|
2
|
+
import * as React2 from "react";
|
|
3
|
+
import { Accessory } from "@sproutsocial/seeds-react-input";
|
|
4
|
+
import Box from "@sproutsocial/seeds-react-box";
|
|
5
|
+
import Icon from "@sproutsocial/seeds-react-icon";
|
|
6
|
+
import Token from "@sproutsocial/seeds-react-token";
|
|
7
|
+
|
|
8
|
+
// src/styles.ts
|
|
9
|
+
import styled, { css } from "styled-components";
|
|
10
|
+
import { COMMON } from "@sproutsocial/seeds-react-system-props";
|
|
11
|
+
import { focusRing } from "@sproutsocial/seeds-react-mixins";
|
|
12
|
+
var Container = styled.div`
|
|
13
|
+
box-sizing: border-box;
|
|
14
|
+
position: relative;
|
|
15
|
+
display: flex;
|
|
16
|
+
flex-wrap: wrap;
|
|
17
|
+
align-items: center;
|
|
18
|
+
align-content: center;
|
|
19
|
+
cursor: text;
|
|
20
|
+
width: 100%;
|
|
21
|
+
border: 1px solid ${(props) => props.theme.colors.form.border.base};
|
|
22
|
+
border-radius: ${(props) => props.theme.radii[500]};
|
|
23
|
+
margin: 0;
|
|
24
|
+
padding: ${(props) => props.theme.space[300]};
|
|
25
|
+
padding-top: ${(props) => props.theme.space[200]};
|
|
26
|
+
background-color: ${(props) => props.theme.colors.form.background.base};
|
|
27
|
+
color: ${(props) => props.theme.colors.text.body};
|
|
28
|
+
transition: border-color ${(props) => props.theme.duration.fast}
|
|
29
|
+
${(props) => props.theme.easing.ease_in},
|
|
30
|
+
box-shadow ${(props) => props.theme.duration.fast}
|
|
31
|
+
${(props) => props.theme.easing.ease_in};
|
|
32
|
+
${(props) => props.theme.typography[200]};
|
|
33
|
+
font-family: ${(props) => props.theme.fontFamily};
|
|
34
|
+
font-weight: ${(props) => props.theme.fontWeights.normal};
|
|
35
|
+
appearance: none;
|
|
36
|
+
|
|
37
|
+
button {
|
|
38
|
+
margin: ${(props) => props.theme.space[200]}
|
|
39
|
+
${(props) => props.theme.space[200]} 0 0;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
input {
|
|
43
|
+
${(props) => props.theme.typography[200]};
|
|
44
|
+
outline: none;
|
|
45
|
+
border: none;
|
|
46
|
+
flex: 1;
|
|
47
|
+
padding: 0;
|
|
48
|
+
padding-top: ${(props) => props.theme.space[100]};
|
|
49
|
+
margin: ${(props) => props.theme.space[200]}
|
|
50
|
+
${(props) => props.theme.space[300]} ${(props) => props.theme.space[100]}
|
|
51
|
+
0;
|
|
52
|
+
color: ${(props) => props.theme.colors.text.body};
|
|
53
|
+
background-color: ${(props) => props.theme.colors.form.background.base};
|
|
54
|
+
/** This matches the height of the token so size does not change as tokens are added */
|
|
55
|
+
min-height: 20px;
|
|
56
|
+
|
|
57
|
+
&::-webkit-search-cancel-button {
|
|
58
|
+
appearance: none;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/* Explicitly removes double focus ring in environments where box-shadow focus styles have been specified (Seeds). Focus is passed up from the input to the parent container. */
|
|
62
|
+
&:focus {
|
|
63
|
+
box-shadow: none;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/* https://stackoverflow.com/questions/14007655/remove-ie10s-clear-field-x-button-on-certain-inputs */
|
|
67
|
+
&::-ms-clear {
|
|
68
|
+
display: none;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* Fix for red ring when input is marked required in Firefox */
|
|
72
|
+
&:not(output):not(:focus):-moz-ui-invalid {
|
|
73
|
+
box-shadow: none;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
&::placeholder {
|
|
77
|
+
color: ${(props) => props.theme.colors.form.placeholder.base};
|
|
78
|
+
font-style: italic;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
${(props) => props.disabled && css`
|
|
82
|
+
opacity: 0.4;
|
|
83
|
+
|
|
84
|
+
cursor: not-allowed;
|
|
85
|
+
`}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
${(props) => props.hasBeforeElement && css`
|
|
89
|
+
padding-left: 40px;
|
|
90
|
+
`}
|
|
91
|
+
|
|
92
|
+
${(props) => props.hasAfterElement && css`
|
|
93
|
+
padding-right: 40px;
|
|
94
|
+
`}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
${(props) => props.disabled && css`
|
|
98
|
+
opacity: 0.4;
|
|
99
|
+
|
|
100
|
+
cursor: not-allowed;
|
|
101
|
+
`}
|
|
102
|
+
|
|
103
|
+
${(props) => props.focused && css`
|
|
104
|
+
${focusRing}
|
|
105
|
+
`}
|
|
106
|
+
|
|
107
|
+
${(props) => props.invalid && css`
|
|
108
|
+
border-color: ${(props2) => props2.theme.colors.form.border.error};
|
|
109
|
+
`}
|
|
110
|
+
|
|
111
|
+
${(props) => props.warning && css`
|
|
112
|
+
border-color: ${(props2) => props2.theme.colors.form.border.warning};
|
|
113
|
+
`}
|
|
114
|
+
|
|
115
|
+
${COMMON}
|
|
116
|
+
`;
|
|
117
|
+
Container.displayName = "TokenInputContainer";
|
|
118
|
+
var styles_default = Container;
|
|
119
|
+
|
|
120
|
+
// src/util.ts
|
|
121
|
+
import uniqueId from "lodash.uniqueid";
|
|
122
|
+
var asTokenSpec = (text) => ({
|
|
123
|
+
id: uniqueId(`${text}-`),
|
|
124
|
+
value: text.trim()
|
|
125
|
+
});
|
|
126
|
+
var KeyNameToRegExpChar = {
|
|
127
|
+
Enter: "\\n"
|
|
128
|
+
};
|
|
129
|
+
var delimitersAsRegExp = (delimiters) => {
|
|
130
|
+
if (!delimiters)
|
|
131
|
+
return /[,\n]/;
|
|
132
|
+
const chars = delimiters.map(
|
|
133
|
+
(key) => KeyNameToRegExpChar[key] || key
|
|
134
|
+
).join("");
|
|
135
|
+
return RegExp(`[${chars}]`);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/TokenScreenReaderStatus.tsx
|
|
139
|
+
import "react";
|
|
140
|
+
import { useEffect, useRef, useState } from "react";
|
|
141
|
+
|
|
142
|
+
// ../seeds-react-visually-hidden/dist/esm/index.js
|
|
143
|
+
import styled2 from "styled-components";
|
|
144
|
+
import { visuallyHidden } from "@sproutsocial/seeds-react-mixins";
|
|
145
|
+
import { jsx } from "react/jsx-runtime";
|
|
146
|
+
var StyledVisuallyHidden = styled2.span`
|
|
147
|
+
${visuallyHidden}
|
|
148
|
+
`;
|
|
149
|
+
var VisuallyHidden = (props) => {
|
|
150
|
+
return /* @__PURE__ */ jsx(StyledVisuallyHidden, { ...props });
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// src/TokenScreenReaderStatus.tsx
|
|
154
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
155
|
+
function usePrevious(value) {
|
|
156
|
+
const ref = useRef();
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
ref.current = value;
|
|
159
|
+
});
|
|
160
|
+
return ref.current;
|
|
161
|
+
}
|
|
162
|
+
var TokenScreenReaderStatus = ({
|
|
163
|
+
tokens
|
|
164
|
+
}) => {
|
|
165
|
+
const prevTokens = usePrevious(tokens);
|
|
166
|
+
const [tokenStatus, setTokenStatus] = useState("");
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
if (prevTokens && tokens) {
|
|
169
|
+
if (prevTokens.length > tokens.length) {
|
|
170
|
+
setTokenStatus(
|
|
171
|
+
`${prevTokens.filter((item) => tokens.indexOf(item) === -1)[0]?.value} has been removed`
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
if (prevTokens.length < tokens.length) {
|
|
175
|
+
setTokenStatus(`${tokens[tokens.length - 1]?.value} has been added.`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}, [prevTokens, tokens, tokenStatus]);
|
|
179
|
+
return /* @__PURE__ */ jsx2(VisuallyHidden, { as: "div", role: "status", children: tokenStatus });
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// src/TokenInput.tsx
|
|
183
|
+
import { jsx as jsx3, jsxs } from "react/jsx-runtime";
|
|
184
|
+
var DefaultDelimiters = [",", "Enter"];
|
|
185
|
+
var ControlledPropNames = [
|
|
186
|
+
"value",
|
|
187
|
+
"hasFocus",
|
|
188
|
+
"activeToken"
|
|
189
|
+
];
|
|
190
|
+
var TokenInput = class extends React2.Component {
|
|
191
|
+
delimiterMatcher;
|
|
192
|
+
constructor(props) {
|
|
193
|
+
super(props);
|
|
194
|
+
const { hasFocus, activeToken, value, delimiters } = props;
|
|
195
|
+
this.delimiterMatcher = delimitersAsRegExp(delimiters || DefaultDelimiters);
|
|
196
|
+
this.state = {
|
|
197
|
+
prevProps: props,
|
|
198
|
+
hasFocus: hasFocus || false,
|
|
199
|
+
activeToken,
|
|
200
|
+
value: value || ""
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
static getDerivedStateFromProps(props, state) {
|
|
204
|
+
const { prevProps } = state;
|
|
205
|
+
const modifiedState = { prevProps: props };
|
|
206
|
+
ControlledPropNames.forEach((propName) => {
|
|
207
|
+
const currentProp = props[propName];
|
|
208
|
+
if (currentProp !== prevProps[propName]) {
|
|
209
|
+
modifiedState[propName] = currentProp;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
modifiedState.prevProps = props;
|
|
213
|
+
return modifiedState;
|
|
214
|
+
}
|
|
215
|
+
isDelimiter(keyName) {
|
|
216
|
+
const { delimiters = DefaultDelimiters } = this.props;
|
|
217
|
+
return delimiters.includes(keyName);
|
|
218
|
+
}
|
|
219
|
+
spawnNewTokens(texts) {
|
|
220
|
+
const {
|
|
221
|
+
onAddToken,
|
|
222
|
+
onChangeTokens,
|
|
223
|
+
tokenMaxLength: max = Infinity,
|
|
224
|
+
tokens = []
|
|
225
|
+
} = this.props;
|
|
226
|
+
const tokenSpecs = texts.map((text) => asTokenSpec(text.slice(0, max)));
|
|
227
|
+
if (onAddToken) {
|
|
228
|
+
tokenSpecs.forEach(onAddToken);
|
|
229
|
+
} else if (onChangeTokens) {
|
|
230
|
+
onChangeTokens(tokens.concat(tokenSpecs));
|
|
231
|
+
}
|
|
232
|
+
this.setState({
|
|
233
|
+
value: ""
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
deleteToken(tokenId) {
|
|
237
|
+
const { onRemoveToken, onChangeTokens, tokens = [] } = this.props;
|
|
238
|
+
const count = tokens.length;
|
|
239
|
+
if (count === 0)
|
|
240
|
+
return;
|
|
241
|
+
const id = tokenId ?? tokens[count - 1]?.id;
|
|
242
|
+
if (onRemoveToken) {
|
|
243
|
+
onRemoveToken(id ? id : "");
|
|
244
|
+
} else if (onChangeTokens) {
|
|
245
|
+
onChangeTokens(tokens.filter((tokenSpec) => tokenSpec.id !== id));
|
|
246
|
+
}
|
|
247
|
+
this.setState({
|
|
248
|
+
value: ""
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
handleChangeText = (e) => {
|
|
252
|
+
const { value } = e.currentTarget;
|
|
253
|
+
const { onChange } = this.props;
|
|
254
|
+
this.setState({
|
|
255
|
+
value
|
|
256
|
+
});
|
|
257
|
+
onChange?.(e, value);
|
|
258
|
+
};
|
|
259
|
+
handleFocus = (e) => {
|
|
260
|
+
const { onFocus } = this.props;
|
|
261
|
+
this.setState({
|
|
262
|
+
hasFocus: true
|
|
263
|
+
});
|
|
264
|
+
onFocus?.(e);
|
|
265
|
+
};
|
|
266
|
+
handleBlur = (e) => {
|
|
267
|
+
const { onBlur } = this.props;
|
|
268
|
+
if (onBlur)
|
|
269
|
+
onBlur(e);
|
|
270
|
+
this.setState({
|
|
271
|
+
hasFocus: false
|
|
272
|
+
});
|
|
273
|
+
};
|
|
274
|
+
handleKeyUp = (e) => {
|
|
275
|
+
this.props.onKeyUp?.(e, e.currentTarget.value);
|
|
276
|
+
};
|
|
277
|
+
handleKeyDown = (e) => {
|
|
278
|
+
const { onKeyDown } = this.props;
|
|
279
|
+
const { key, currentTarget } = e;
|
|
280
|
+
const text = currentTarget.value;
|
|
281
|
+
if (onKeyDown)
|
|
282
|
+
onKeyDown(e, text);
|
|
283
|
+
if (this.isDelimiter(key)) {
|
|
284
|
+
if (text) {
|
|
285
|
+
this.spawnNewTokens([text]);
|
|
286
|
+
e.preventDefault();
|
|
287
|
+
}
|
|
288
|
+
} else if (key === "Backspace") {
|
|
289
|
+
if (text === "") {
|
|
290
|
+
this.deleteToken();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
handlePaste = (e) => {
|
|
295
|
+
const text = e.clipboardData.getData("text");
|
|
296
|
+
const { onPaste } = this.props;
|
|
297
|
+
if (onPaste)
|
|
298
|
+
onPaste(e, text);
|
|
299
|
+
const subtexts = text.split(this.delimiterMatcher);
|
|
300
|
+
const texts = subtexts.filter((subtext) => subtext.length);
|
|
301
|
+
if (texts.length > 1) {
|
|
302
|
+
this.spawnNewTokens(texts);
|
|
303
|
+
e.preventDefault();
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
handleClickToken = (e, token) => {
|
|
307
|
+
const { onClickToken, disabled } = this.props;
|
|
308
|
+
if (onClickToken)
|
|
309
|
+
onClickToken(e, token.id);
|
|
310
|
+
if (!disabled) {
|
|
311
|
+
this.deleteToken(token.id);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
renderToken(token) {
|
|
315
|
+
const { iconName: defaultIconName, disabled } = this.props;
|
|
316
|
+
const activeId = this.state.activeToken;
|
|
317
|
+
const {
|
|
318
|
+
id,
|
|
319
|
+
iconName: tokenIconName,
|
|
320
|
+
iconProps = { "aria-hidden": true },
|
|
321
|
+
value,
|
|
322
|
+
valid
|
|
323
|
+
} = token;
|
|
324
|
+
const iconName = tokenIconName || defaultIconName;
|
|
325
|
+
const isActive = activeId === id;
|
|
326
|
+
return /* @__PURE__ */ jsx3(
|
|
327
|
+
Token,
|
|
328
|
+
{
|
|
329
|
+
id,
|
|
330
|
+
onClick: (e) => this.handleClickToken(e, token),
|
|
331
|
+
valid,
|
|
332
|
+
active: isActive,
|
|
333
|
+
disabled,
|
|
334
|
+
children: /* @__PURE__ */ jsxs(Box, { display: "flex", alignItems: "center", children: [
|
|
335
|
+
iconName && /* @__PURE__ */ jsx3(Icon, { name: iconName, size: "mini", pr: 300, ...iconProps }),
|
|
336
|
+
value
|
|
337
|
+
] })
|
|
338
|
+
}
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
renderTokens(tokens) {
|
|
342
|
+
return tokens.map((token) => /* @__PURE__ */ jsx3("div", { className: "TokenInput-token", children: this.renderToken(token) }, token.id));
|
|
343
|
+
}
|
|
344
|
+
render() {
|
|
345
|
+
const {
|
|
346
|
+
autoFocus,
|
|
347
|
+
autocomplete,
|
|
348
|
+
disabled,
|
|
349
|
+
isInvalid,
|
|
350
|
+
hasWarning,
|
|
351
|
+
id,
|
|
352
|
+
name,
|
|
353
|
+
placeholder,
|
|
354
|
+
required,
|
|
355
|
+
elemBefore,
|
|
356
|
+
elemAfter,
|
|
357
|
+
maxLength,
|
|
358
|
+
ariaDescribedby,
|
|
359
|
+
ariaLabel,
|
|
360
|
+
innerRef,
|
|
361
|
+
// These functions are used in the class functions above, but need to be extracted in order for `rest` to be correct
|
|
362
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
363
|
+
value,
|
|
364
|
+
onAddToken,
|
|
365
|
+
onRemoveToken,
|
|
366
|
+
onChangeTokens,
|
|
367
|
+
onClickToken,
|
|
368
|
+
onBlur,
|
|
369
|
+
onChange,
|
|
370
|
+
onFocus,
|
|
371
|
+
onKeyDown,
|
|
372
|
+
onKeyUp,
|
|
373
|
+
onPaste,
|
|
374
|
+
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
375
|
+
inputProps = {},
|
|
376
|
+
qa = {},
|
|
377
|
+
tokens,
|
|
378
|
+
...rest
|
|
379
|
+
} = this.props;
|
|
380
|
+
const { state } = this;
|
|
381
|
+
return /* @__PURE__ */ jsxs(
|
|
382
|
+
styles_default,
|
|
383
|
+
{
|
|
384
|
+
hasBeforeElement: !!elemBefore,
|
|
385
|
+
hasAfterElement: !!elemAfter,
|
|
386
|
+
disabled,
|
|
387
|
+
invalid: !!isInvalid,
|
|
388
|
+
warning: hasWarning,
|
|
389
|
+
focused: state.hasFocus,
|
|
390
|
+
...rest,
|
|
391
|
+
children: [
|
|
392
|
+
elemBefore && /* @__PURE__ */ jsx3(Accessory, { before: true, children: elemBefore }),
|
|
393
|
+
tokens && this.renderTokens(tokens),
|
|
394
|
+
/* @__PURE__ */ jsx3(TokenScreenReaderStatus, { tokens }),
|
|
395
|
+
/* @__PURE__ */ jsx3(
|
|
396
|
+
"input",
|
|
397
|
+
{
|
|
398
|
+
"aria-describedby": ariaDescribedby,
|
|
399
|
+
"aria-invalid": !!isInvalid,
|
|
400
|
+
"aria-label": ariaLabel,
|
|
401
|
+
autoFocus,
|
|
402
|
+
autoComplete: autocomplete,
|
|
403
|
+
disabled,
|
|
404
|
+
id,
|
|
405
|
+
name,
|
|
406
|
+
placeholder,
|
|
407
|
+
type: "text",
|
|
408
|
+
required,
|
|
409
|
+
value: state.value,
|
|
410
|
+
maxLength,
|
|
411
|
+
onBlur: this.handleBlur,
|
|
412
|
+
onChange: this.handleChangeText,
|
|
413
|
+
onFocus: this.handleFocus,
|
|
414
|
+
onKeyDown: this.handleKeyDown,
|
|
415
|
+
onKeyUp: this.handleKeyUp,
|
|
416
|
+
onPaste: this.handlePaste,
|
|
417
|
+
ref: innerRef,
|
|
418
|
+
"data-qa-input": name || "",
|
|
419
|
+
"data-qa-input-isdisabled": !!disabled,
|
|
420
|
+
"data-qa-input-isrequired": !!required,
|
|
421
|
+
...qa,
|
|
422
|
+
...inputProps
|
|
423
|
+
}
|
|
424
|
+
),
|
|
425
|
+
elemAfter && /* @__PURE__ */ jsx3(Accessory, { after: true, children: elemAfter })
|
|
426
|
+
]
|
|
427
|
+
}
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
// src/TokenInputTypes.ts
|
|
433
|
+
import "react";
|
|
434
|
+
|
|
435
|
+
// src/index.ts
|
|
436
|
+
var src_default = TokenInput;
|
|
437
|
+
export {
|
|
438
|
+
TokenInput,
|
|
439
|
+
src_default as default
|
|
440
|
+
};
|
|
441
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/TokenInput.tsx","../../src/styles.ts","../../src/util.ts","../../src/TokenScreenReaderStatus.tsx","../../../seeds-react-visually-hidden/src/VisuallyHidden.tsx","../../src/TokenInputTypes.ts","../../src/index.ts"],"sourcesContent":["import * as React from \"react\";\nimport { Accessory } from \"@sproutsocial/seeds-react-input\";\nimport Box from \"@sproutsocial/seeds-react-box\";\nimport Icon from \"@sproutsocial/seeds-react-icon\";\nimport Token from \"@sproutsocial/seeds-react-token\";\nimport Container from \"./styles\";\nimport { asTokenSpec, delimitersAsRegExp } from \"./util\";\nimport type { TypeTokenInputProps, TypeTokenSpec } from \"./TokenInputTypes\";\nimport { TokenScreenReaderStatus } from \"./TokenScreenReaderStatus\";\n\ntype TypeState = {\n prevProps: TypeTokenInputProps;\n hasFocus: boolean;\n activeToken: string | null | undefined;\n value: string;\n};\n\nconst DefaultDelimiters = [\",\", \"Enter\"];\nconst ControlledPropNames: (keyof TypeState)[] = [\n \"value\",\n \"hasFocus\",\n \"activeToken\",\n];\n\nexport default class TokenInput extends React.Component<\n TypeTokenInputProps,\n TypeState\n> {\n delimiterMatcher: RegExp;\n\n constructor(props: TypeTokenInputProps) {\n super(props);\n const { hasFocus, activeToken, value, delimiters } = props;\n this.delimiterMatcher = delimitersAsRegExp(delimiters || DefaultDelimiters);\n this.state = {\n prevProps: props,\n hasFocus: hasFocus || false,\n activeToken: activeToken,\n value: value || \"\",\n };\n }\n\n static getDerivedStateFromProps(\n props: Readonly<TypeTokenInputProps>,\n state: TypeState\n ) {\n const { prevProps } = state;\n const modifiedState: Partial<TypeState> = { prevProps: props };\n ControlledPropNames.forEach((propName) => {\n const currentProp = props[propName as keyof TypeTokenInputProps];\n\n // @ts-ignore: TODO - fix state types for prevProps\n if (currentProp !== prevProps[propName]) {\n modifiedState[propName] = currentProp;\n }\n });\n modifiedState.prevProps = props;\n return modifiedState;\n }\n\n isDelimiter(keyName: string) {\n const { delimiters = DefaultDelimiters } = this.props;\n return delimiters.includes(keyName);\n }\n\n spawnNewTokens(texts: string[]) {\n const {\n onAddToken,\n onChangeTokens,\n tokenMaxLength: max = Infinity,\n tokens = [],\n } = this.props;\n const tokenSpecs = texts.map((text) => asTokenSpec(text.slice(0, max)));\n\n if (onAddToken) {\n tokenSpecs.forEach(onAddToken);\n } else if (onChangeTokens) {\n onChangeTokens(tokens.concat(tokenSpecs));\n }\n\n this.setState({\n value: \"\",\n });\n }\n\n deleteToken(tokenId?: string) {\n const { onRemoveToken, onChangeTokens, tokens = [] } = this.props;\n const count = tokens.length;\n if (count === 0) return;\n const id = tokenId ?? tokens[count - 1]?.id;\n\n if (onRemoveToken) {\n onRemoveToken(id ? id : \"\");\n } else if (onChangeTokens) {\n onChangeTokens(tokens.filter((tokenSpec) => tokenSpec.id !== id));\n }\n\n this.setState({\n value: \"\",\n });\n }\n\n handleChangeText = (e: React.SyntheticEvent<HTMLInputElement>) => {\n const { value } = e.currentTarget;\n const { onChange } = this.props;\n this.setState({\n value,\n });\n onChange?.(e, value);\n };\n\n handleFocus = (e: React.FocusEvent<HTMLInputElement>) => {\n const { onFocus } = this.props;\n this.setState({\n hasFocus: true,\n });\n onFocus?.(e);\n };\n\n handleBlur = (e: React.FocusEvent<HTMLInputElement>) => {\n const { onBlur } = this.props;\n if (onBlur) onBlur(e);\n this.setState({\n hasFocus: false,\n });\n };\n\n handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => {\n this.props.onKeyUp?.(e, e.currentTarget.value);\n };\n\n handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {\n const { onKeyDown } = this.props;\n const { key, currentTarget } = e;\n const text = currentTarget.value;\n if (onKeyDown) onKeyDown(e, text);\n\n // keyPress event runs before change\n // Prevent event from bubbling up and calling change, which can lead to comma in value\n if (this.isDelimiter(key)) {\n if (text) {\n this.spawnNewTokens([text]);\n e.preventDefault();\n }\n } else if (key === \"Backspace\") {\n if (text === \"\") {\n this.deleteToken();\n }\n }\n };\n\n handlePaste = (e: React.ClipboardEvent<HTMLInputElement>) => {\n const text = e.clipboardData.getData(\"text\");\n const { onPaste } = this.props;\n if (onPaste) onPaste(e, text);\n const subtexts = text.split(this.delimiterMatcher);\n const texts = subtexts.filter((subtext) => subtext.length);\n\n if (texts.length > 1) {\n this.spawnNewTokens(texts);\n e.preventDefault();\n }\n };\n\n handleClickToken = (\n e: React.SyntheticEvent<HTMLButtonElement>,\n token: TypeTokenSpec\n ) => {\n const { onClickToken, disabled } = this.props;\n if (onClickToken) onClickToken(e, token.id);\n\n if (!disabled) {\n this.deleteToken(token.id);\n }\n };\n\n renderToken(token: TypeTokenSpec): React.ReactNode {\n const { iconName: defaultIconName, disabled } = this.props;\n const activeId = this.state.activeToken;\n const {\n id,\n iconName: tokenIconName,\n iconProps = { \"aria-hidden\": true },\n value,\n valid,\n } = token;\n const iconName = tokenIconName || defaultIconName;\n const isActive = activeId === id;\n return (\n <Token\n id={id}\n onClick={(e) => this.handleClickToken(e, token)}\n valid={valid}\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n // @ts-ignore\n active={isActive}\n disabled={disabled}\n >\n <Box display=\"flex\" alignItems=\"center\">\n {iconName && (\n <Icon name={iconName} size=\"mini\" pr={300} {...iconProps} />\n )}\n {value}\n </Box>\n </Token>\n );\n }\n\n renderTokens(tokens: TypeTokenSpec[]): React.ReactNode {\n return tokens.map<React.ReactNode>((token) => (\n <div key={token.id} className=\"TokenInput-token\">\n {this.renderToken(token)}\n </div>\n ));\n }\n\n override render() {\n const {\n autoFocus,\n autocomplete,\n disabled,\n isInvalid,\n hasWarning,\n id,\n name,\n placeholder,\n required,\n elemBefore,\n elemAfter,\n maxLength,\n ariaDescribedby,\n ariaLabel,\n innerRef,\n // These functions are used in the class functions above, but need to be extracted in order for `rest` to be correct\n /* eslint-disable @typescript-eslint/no-unused-vars */\n value,\n onAddToken,\n onRemoveToken,\n onChangeTokens,\n onClickToken,\n onBlur,\n onChange,\n onFocus,\n onKeyDown,\n onKeyUp,\n onPaste,\n /* eslint-enable @typescript-eslint/no-unused-vars */\n inputProps = {},\n qa = {},\n tokens,\n ...rest\n } = this.props;\n const { state } = this;\n return (\n <Container\n hasBeforeElement={!!elemBefore}\n hasAfterElement={!!elemAfter}\n disabled={disabled}\n invalid={!!isInvalid}\n warning={hasWarning}\n focused={state.hasFocus}\n {...rest}\n >\n {elemBefore && <Accessory before>{elemBefore}</Accessory>}\n\n {tokens && this.renderTokens(tokens)}\n\n <TokenScreenReaderStatus tokens={tokens} />\n\n <input\n aria-describedby={ariaDescribedby}\n aria-invalid={!!isInvalid}\n aria-label={ariaLabel}\n autoFocus={autoFocus}\n autoComplete={autocomplete}\n disabled={disabled}\n id={id}\n name={name}\n placeholder={placeholder}\n type=\"text\"\n required={required}\n value={state.value}\n maxLength={maxLength}\n onBlur={this.handleBlur}\n onChange={this.handleChangeText}\n onFocus={this.handleFocus}\n onKeyDown={this.handleKeyDown}\n onKeyUp={this.handleKeyUp}\n onPaste={this.handlePaste}\n ref={innerRef}\n data-qa-input={name || \"\"}\n data-qa-input-isdisabled={!!disabled}\n data-qa-input-isrequired={!!required}\n {...qa}\n {...inputProps}\n />\n\n {elemAfter && <Accessory after>{elemAfter}</Accessory>}\n </Container>\n );\n }\n}\n","import styled, { css } from \"styled-components\";\nimport { COMMON } from \"@sproutsocial/seeds-react-system-props\";\nimport { focusRing } from \"@sproutsocial/seeds-react-mixins\";\nimport type { TypeInputContainerProps } from \"@sproutsocial/seeds-react-input\";\n\ninterface TypeTokenInputContainerProps\n extends Pick<\n TypeInputContainerProps,\n \"hasBeforeElement\" | \"hasAfterElement\" | \"disabled\" | \"invalid\" | \"warning\"\n > {\n focused?: boolean;\n}\n\nconst Container = styled.div<TypeTokenInputContainerProps>`\n box-sizing: border-box;\n position: relative;\n display: flex;\n flex-wrap: wrap;\n align-items: center;\n align-content: center;\n cursor: text;\n width: 100%;\n border: 1px solid ${(props) => props.theme.colors.form.border.base};\n border-radius: ${(props) => props.theme.radii[500]};\n margin: 0;\n padding: ${(props) => props.theme.space[300]};\n padding-top: ${(props) => props.theme.space[200]};\n background-color: ${(props) => props.theme.colors.form.background.base};\n color: ${(props) => props.theme.colors.text.body};\n transition: border-color ${(props) => props.theme.duration.fast}\n ${(props) => props.theme.easing.ease_in},\n box-shadow ${(props) => props.theme.duration.fast}\n ${(props) => props.theme.easing.ease_in};\n ${(props) => props.theme.typography[200]};\n font-family: ${(props) => props.theme.fontFamily};\n font-weight: ${(props) => props.theme.fontWeights.normal};\n appearance: none;\n\n button {\n margin: ${(props) => props.theme.space[200]}\n ${(props) => props.theme.space[200]} 0 0;\n }\n\n input {\n ${(props) => props.theme.typography[200]};\n outline: none;\n border: none;\n flex: 1;\n padding: 0;\n padding-top: ${(props) => props.theme.space[100]};\n margin: ${(props) => props.theme.space[200]}\n ${(props) => props.theme.space[300]} ${(props) => props.theme.space[100]}\n 0;\n color: ${(props) => props.theme.colors.text.body};\n background-color: ${(props) => props.theme.colors.form.background.base};\n /** This matches the height of the token so size does not change as tokens are added */\n min-height: 20px;\n\n &::-webkit-search-cancel-button {\n appearance: none;\n }\n\n /* Explicitly removes double focus ring in environments where box-shadow focus styles have been specified (Seeds). Focus is passed up from the input to the parent container. */\n &:focus {\n box-shadow: none;\n }\n\n /* https://stackoverflow.com/questions/14007655/remove-ie10s-clear-field-x-button-on-certain-inputs */\n &::-ms-clear {\n display: none;\n }\n\n /* Fix for red ring when input is marked required in Firefox */\n &:not(output):not(:focus):-moz-ui-invalid {\n box-shadow: none;\n }\n\n &::placeholder {\n color: ${(props) => props.theme.colors.form.placeholder.base};\n font-style: italic;\n }\n\n ${(props) =>\n props.disabled &&\n css`\n opacity: 0.4;\n\n cursor: not-allowed;\n `}\n }\n\n ${(props) =>\n props.hasBeforeElement &&\n css`\n padding-left: 40px;\n `}\n\n ${(props) =>\n props.hasAfterElement &&\n css`\n padding-right: 40px;\n `}\n\n\n ${(props) =>\n props.disabled &&\n css`\n opacity: 0.4;\n\n cursor: not-allowed;\n `}\n\n ${(props) =>\n props.focused &&\n css`\n ${focusRing}\n `}\n\n ${(props) =>\n props.invalid &&\n css`\n border-color: ${(props) => props.theme.colors.form.border.error};\n `}\n\n\t${(props) =>\n props.warning &&\n css`\n border-color: ${(props) => props.theme.colors.form.border.warning};\n `}\n\n ${COMMON}\n`;\n\nContainer.displayName = \"TokenInputContainer\";\n\nexport default Container;\n","import uniqueId from \"lodash.uniqueid\";\nimport type { TypeTokenSpec } from \"./\";\n\nexport const asTokenSpec = (text: string): TypeTokenSpec => ({\n id: uniqueId(`${text}-`),\n value: text.trim(),\n});\n\nconst KeyNameToRegExpChar: { [key: string]: string } = {\n Enter: \"\\\\n\",\n};\n\nexport const delimitersAsRegExp = (delimiters: string[] | null | undefined) => {\n if (!delimiters) return /[,\\n]/;\n const chars = delimiters\n .map(\n (key) =>\n KeyNameToRegExpChar[key as keyof typeof KeyNameToRegExpChar] || key\n )\n .join(\"\");\n return RegExp(`[${chars}]`);\n};\n","import React from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\nimport { VisuallyHidden } from \"@sproutsocial/seeds-react-visually-hidden\";\nimport type { TypeTokenInputProps } from \"./\";\n\nfunction usePrevious(value: TypeTokenInputProps[\"tokens\"]) {\n const ref = useRef<TypeTokenInputProps[\"tokens\"]>();\n\n useEffect(() => {\n ref.current = value;\n });\n\n return ref.current;\n}\n\nexport const TokenScreenReaderStatus = ({\n tokens,\n}: {\n tokens: TypeTokenInputProps[\"tokens\"];\n}) => {\n const prevTokens = usePrevious(tokens);\n const [tokenStatus, setTokenStatus] = useState(\"\");\n\n // TODO: Use callbacks so consumers can pass localized messaging to the screen reader\n useEffect(() => {\n if (prevTokens && tokens) {\n if (prevTokens.length > tokens.length) {\n setTokenStatus(\n `${\n prevTokens.filter((item) => tokens.indexOf(item) === -1)[0]?.value\n } has been removed`\n );\n }\n\n if (prevTokens.length < tokens.length) {\n setTokenStatus(`${tokens[tokens.length - 1]?.value} has been added.`);\n }\n }\n }, [prevTokens, tokens, tokenStatus]);\n\n return (\n <VisuallyHidden as=\"div\" role=\"status\">\n {tokenStatus}\n </VisuallyHidden>\n );\n};\n","import styled from \"styled-components\";\nimport { visuallyHidden } from \"@sproutsocial/seeds-react-mixins\";\nimport type { ComponentPropsWithRef } from \"react\";\n\nconst StyledVisuallyHidden = styled.span`\n ${visuallyHidden}\n`;\n\nexport type VisuallyHiddenProps = ComponentPropsWithRef<\n typeof StyledVisuallyHidden\n>;\n\n// This wrapper component is needed for react-docgen to work.\n// It has issues with generating docs for the styled component directly.\nexport const VisuallyHidden = (props: VisuallyHiddenProps) => {\n return <StyledVisuallyHidden {...props} />;\n};\n","import * as React from \"react\";\nimport type {\n TypeStyledComponentsCommonProps,\n TypeSystemCommonProps,\n} from \"@sproutsocial/seeds-react-system-props\";\nimport type {\n TypeIconProps,\n TypeIconName,\n} from \"@sproutsocial/seeds-react-icon\";\n\nexport interface TypeTokenSpec {\n id: string;\n iconName?: TypeIconName;\n iconProps?: Omit<TypeIconProps, \"name\">;\n value: string;\n valid?: boolean;\n}\n\nexport interface TypeBaseTokenInputProps {\n /** ID of the form element, should match the \"for\" value of the associated label */\n id: string;\n name: string;\n iconName?: TypeIconName;\n\n /** Array of delimiter key names */\n delimiters?: string[];\n\n /** Current input text. Required when controlling the input text */\n value?: string;\n\n /** Current focus state. Required when controlling the focus via onFocus and onBlur */\n hasFocus?: boolean;\n\n /** Id of the currently selected token */\n activeToken?: string;\n\n /** Array of current tokens */\n tokens?: TypeTokenSpec[];\n\n /** Standard control of changing tokens. For fine-grain control use onAddToken and onRemoveToken, instead */\n onChangeTokens?: (tokens: TypeTokenSpec[]) => void;\n\n /** Fine-grained control of adding tokens. Use with onRemoveToken instead of onChangeTokens */\n onAddToken?: (tokenSpec: TypeTokenSpec) => void;\n\n /** Fine-grained control of removing tokens. Use with onAddToken instead of onChangeTokens */\n onRemoveToken?: (tokenId: string) => void;\n\n /** Controls clicking on a token. When absent, clicking a token removes itself */\n onClickToken?: (\n e: React.SyntheticEvent<HTMLButtonElement>,\n tokenId: string\n ) => void;\n\n /** Fine-grained control of the input text used to create tokens */\n onChange?: (e: React.SyntheticEvent<HTMLInputElement>, value: string) => void;\n\n /** Fine-grained control of pasted text */\n onPaste?: (e: React.ClipboardEvent<HTMLInputElement>, value: string) => void;\n onBlur?: (e: React.FocusEvent<HTMLInputElement>) => void;\n onFocus?: (e: React.FocusEvent<HTMLInputElement>) => void;\n onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>, value: string) => void;\n onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>, value: string) => void;\n\n /** Attribute used to associate other elements that describe the Input, like an error */\n ariaDescribedby?: string;\n\n /** Label used to describe the input if not used with an accompanying visual label */\n ariaLabel?: string;\n\n /** Placeholder text for when value is undefined or empty */\n placeholder?: string;\n\n /** Will autofocus the element when mounted to the DOM */\n autoFocus?: boolean;\n\n /** HTML disabled attribute */\n disabled?: boolean;\n\n /** Whether or not the current contents of the input are invalid */\n isInvalid?: boolean;\n\n /** Whether or not the current contents of the input has any warnings */\n hasWarning?: boolean;\n\n /** HTML required attribute */\n required?: boolean;\n\n /** 16x16 element, such as an icon */\n elemBefore?: React.ReactNode;\n\n /** 16x16 element, such as an icon */\n elemAfter?: React.ReactNode;\n\n /** Max input text length */\n maxLength?: number;\n\n /** Max length of the token */\n tokenMaxLength?: number;\n\n /** Props to spread onto the underlying input element */\n inputProps?: React.ComponentPropsWithoutRef<\"input\">;\n\n /** Used to get a reference to the underlying element */\n innerRef?: React.Ref<HTMLInputElement>;\n qa?: object;\n\n /** Browser autocomplete support */\n autocomplete?: string;\n}\n\nexport interface TypeTokenInputProps\n extends TypeBaseTokenInputProps,\n TypeStyledComponentsCommonProps,\n TypeSystemCommonProps,\n Omit<\n React.ComponentPropsWithoutRef<\"div\">,\n keyof TypeBaseTokenInputProps | \"color\"\n > {}\n","import TokenInput from \"./TokenInput\";\n\nexport default TokenInput;\nexport { TokenInput };\nexport * from \"./TokenInputTypes\";\n"],"mappings":";AAAA,YAAYA,YAAW;AACvB,SAAS,iBAAiB;AAC1B,OAAO,SAAS;AAChB,OAAO,UAAU;AACjB,OAAO,WAAW;;;ACJlB,OAAO,UAAU,WAAW;AAC5B,SAAS,cAAc;AACvB,SAAS,iBAAiB;AAW1B,IAAM,YAAY,OAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBASH,CAAC,UAAU,MAAM,MAAM,OAAO,KAAK,OAAO,IAAI;AAAA,mBACjD,CAAC,UAAU,MAAM,MAAM,MAAM,GAAG,CAAC;AAAA;AAAA,aAEvC,CAAC,UAAU,MAAM,MAAM,MAAM,GAAG,CAAC;AAAA,iBAC7B,CAAC,UAAU,MAAM,MAAM,MAAM,GAAG,CAAC;AAAA,sBAC5B,CAAC,UAAU,MAAM,MAAM,OAAO,KAAK,WAAW,IAAI;AAAA,WAC7D,CAAC,UAAU,MAAM,MAAM,OAAO,KAAK,IAAI;AA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sx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { TypeStyledComponentsCommonProps, TypeSystemCommonProps } from '@sproutsocial/seeds-react-system-props';
|
|
4
|
+
import { TypeIconName, TypeIconProps } from '@sproutsocial/seeds-react-icon';
|
|
5
|
+
|
|
6
|
+
interface TypeTokenSpec {
|
|
7
|
+
id: string;
|
|
8
|
+
iconName?: TypeIconName;
|
|
9
|
+
iconProps?: Omit<TypeIconProps, "name">;
|
|
10
|
+
value: string;
|
|
11
|
+
valid?: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface TypeBaseTokenInputProps {
|
|
14
|
+
/** ID of the form element, should match the "for" value of the associated label */
|
|
15
|
+
id: string;
|
|
16
|
+
name: string;
|
|
17
|
+
iconName?: TypeIconName;
|
|
18
|
+
/** Array of delimiter key names */
|
|
19
|
+
delimiters?: string[];
|
|
20
|
+
/** Current input text. Required when controlling the input text */
|
|
21
|
+
value?: string;
|
|
22
|
+
/** Current focus state. Required when controlling the focus via onFocus and onBlur */
|
|
23
|
+
hasFocus?: boolean;
|
|
24
|
+
/** Id of the currently selected token */
|
|
25
|
+
activeToken?: string;
|
|
26
|
+
/** Array of current tokens */
|
|
27
|
+
tokens?: TypeTokenSpec[];
|
|
28
|
+
/** Standard control of changing tokens. For fine-grain control use onAddToken and onRemoveToken, instead */
|
|
29
|
+
onChangeTokens?: (tokens: TypeTokenSpec[]) => void;
|
|
30
|
+
/** Fine-grained control of adding tokens. Use with onRemoveToken instead of onChangeTokens */
|
|
31
|
+
onAddToken?: (tokenSpec: TypeTokenSpec) => void;
|
|
32
|
+
/** Fine-grained control of removing tokens. Use with onAddToken instead of onChangeTokens */
|
|
33
|
+
onRemoveToken?: (tokenId: string) => void;
|
|
34
|
+
/** Controls clicking on a token. When absent, clicking a token removes itself */
|
|
35
|
+
onClickToken?: (e: React.SyntheticEvent<HTMLButtonElement>, tokenId: string) => void;
|
|
36
|
+
/** Fine-grained control of the input text used to create tokens */
|
|
37
|
+
onChange?: (e: React.SyntheticEvent<HTMLInputElement>, value: string) => void;
|
|
38
|
+
/** Fine-grained control of pasted text */
|
|
39
|
+
onPaste?: (e: React.ClipboardEvent<HTMLInputElement>, value: string) => void;
|
|
40
|
+
onBlur?: (e: React.FocusEvent<HTMLInputElement>) => void;
|
|
41
|
+
onFocus?: (e: React.FocusEvent<HTMLInputElement>) => void;
|
|
42
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>, value: string) => void;
|
|
43
|
+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>, value: string) => void;
|
|
44
|
+
/** Attribute used to associate other elements that describe the Input, like an error */
|
|
45
|
+
ariaDescribedby?: string;
|
|
46
|
+
/** Label used to describe the input if not used with an accompanying visual label */
|
|
47
|
+
ariaLabel?: string;
|
|
48
|
+
/** Placeholder text for when value is undefined or empty */
|
|
49
|
+
placeholder?: string;
|
|
50
|
+
/** Will autofocus the element when mounted to the DOM */
|
|
51
|
+
autoFocus?: boolean;
|
|
52
|
+
/** HTML disabled attribute */
|
|
53
|
+
disabled?: boolean;
|
|
54
|
+
/** Whether or not the current contents of the input are invalid */
|
|
55
|
+
isInvalid?: boolean;
|
|
56
|
+
/** Whether or not the current contents of the input has any warnings */
|
|
57
|
+
hasWarning?: boolean;
|
|
58
|
+
/** HTML required attribute */
|
|
59
|
+
required?: boolean;
|
|
60
|
+
/** 16x16 element, such as an icon */
|
|
61
|
+
elemBefore?: React.ReactNode;
|
|
62
|
+
/** 16x16 element, such as an icon */
|
|
63
|
+
elemAfter?: React.ReactNode;
|
|
64
|
+
/** Max input text length */
|
|
65
|
+
maxLength?: number;
|
|
66
|
+
/** Max length of the token */
|
|
67
|
+
tokenMaxLength?: number;
|
|
68
|
+
/** Props to spread onto the underlying input element */
|
|
69
|
+
inputProps?: React.ComponentPropsWithoutRef<"input">;
|
|
70
|
+
/** Used to get a reference to the underlying element */
|
|
71
|
+
innerRef?: React.Ref<HTMLInputElement>;
|
|
72
|
+
qa?: object;
|
|
73
|
+
/** Browser autocomplete support */
|
|
74
|
+
autocomplete?: string;
|
|
75
|
+
}
|
|
76
|
+
interface TypeTokenInputProps extends TypeBaseTokenInputProps, TypeStyledComponentsCommonProps, TypeSystemCommonProps, Omit<React.ComponentPropsWithoutRef<"div">, keyof TypeBaseTokenInputProps | "color"> {
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
type TypeState = {
|
|
80
|
+
prevProps: TypeTokenInputProps;
|
|
81
|
+
hasFocus: boolean;
|
|
82
|
+
activeToken: string | null | undefined;
|
|
83
|
+
value: string;
|
|
84
|
+
};
|
|
85
|
+
declare class TokenInput extends React.Component<TypeTokenInputProps, TypeState> {
|
|
86
|
+
delimiterMatcher: RegExp;
|
|
87
|
+
constructor(props: TypeTokenInputProps);
|
|
88
|
+
static getDerivedStateFromProps(props: Readonly<TypeTokenInputProps>, state: TypeState): Partial<TypeState>;
|
|
89
|
+
isDelimiter(keyName: string): boolean;
|
|
90
|
+
spawnNewTokens(texts: string[]): void;
|
|
91
|
+
deleteToken(tokenId?: string): void;
|
|
92
|
+
handleChangeText: (e: React.SyntheticEvent<HTMLInputElement>) => void;
|
|
93
|
+
handleFocus: (e: React.FocusEvent<HTMLInputElement>) => void;
|
|
94
|
+
handleBlur: (e: React.FocusEvent<HTMLInputElement>) => void;
|
|
95
|
+
handleKeyUp: (e: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
96
|
+
handleKeyDown: (e: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
97
|
+
handlePaste: (e: React.ClipboardEvent<HTMLInputElement>) => void;
|
|
98
|
+
handleClickToken: (e: React.SyntheticEvent<HTMLButtonElement>, token: TypeTokenSpec) => void;
|
|
99
|
+
renderToken(token: TypeTokenSpec): React.ReactNode;
|
|
100
|
+
renderTokens(tokens: TypeTokenSpec[]): React.ReactNode;
|
|
101
|
+
render(): react_jsx_runtime.JSX.Element;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { TokenInput, type TypeBaseTokenInputProps, type TypeTokenInputProps, type TypeTokenSpec, TokenInput as default };
|