-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken-input.js
96 lines (85 loc) · 1.91 KB
/
token-input.js
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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { forwardRef, useState } from '@wordpress/element';
/**
* Internal dependencies
*/
export function UnForwardedTokenInput(
props,
ref,
) {
const {
value,
isExpanded,
instanceId,
selectedSuggestionIndex,
className,
onChange,
onFocus,
onBlur,
...restProps
} = props;
const [ hasFocus, setHasFocus ] = useState( false );
const size = value ? value.length + 1 : 0;
const onChangeHandler = ( event ) => {
if ( onChange ) {
onChange( {
value: event.target.value,
} );
}
};
const onFocusHandler = ( e ) => {
setHasFocus( true );
onFocus?.( e );
};
const onBlurHandler = (
e
) => {
setHasFocus( false );
onBlur?.( e );
};
return (
<input
ref={ ref }
id={ instanceId }
type="text"
{ ...restProps }
value={ value || '' }
onChange={ onChangeHandler }
onFocus={ onFocusHandler }
onBlur={ onBlurHandler }
size={ size }
className={ classnames(
className,
'codeamp-components-multi-select-control__input'
) }
autoComplete="off"
role="combobox"
aria-expanded={ isExpanded }
aria-autocomplete="list"
aria-owns={
isExpanded
? `${ instanceId }-suggestions`
: undefined
}
aria-activedescendant={
// Only add the `aria-activedescendant` attribute when:
// - the user is actively interacting with the input (`hasFocus`)
// - there is a selected suggestion (`selectedSuggestionIndex !== -1`)
// - the list of suggestions are rendered in the DOM (`isExpanded`)
hasFocus && selectedSuggestionIndex !== -1 && isExpanded
? `${ instanceId }-suggestions-${ selectedSuggestionIndex }`
: undefined
}
aria-describedby={ `${ instanceId }-howto` }
data-lpignore="true" // Disable LastPass :/
/>
);
}
export const TokenInput = forwardRef( UnForwardedTokenInput );
export default TokenInput;