@@ -25,14 +25,63 @@ const CLASS_NAMES = {
25
25
suggestionDisabled : 'is-disabled'
26
26
}
27
27
28
+ function pressDelimiterKey ( e ) {
29
+ if ( this . state . query || this . state . selected > - 1 ) {
30
+ e . preventDefault ( )
31
+ }
32
+
33
+ if ( this . state . query . length >= this . props . minQueryLength ) {
34
+ // Check if the user typed in an existing suggestion.
35
+ const match = this . suggestions . state . options . findIndex ( ( suggestion ) => (
36
+ suggestion . name . search ( new RegExp ( `^${ this . state . query } $` , 'i' ) ) === 0
37
+ ) )
38
+
39
+ const index = this . state . selected === - 1 ? match : this . state . selected
40
+
41
+ if ( index > - 1 ) {
42
+ this . addTag ( this . suggestions . state . options [ index ] )
43
+ } else if ( this . props . allowNew ) {
44
+ this . addTag ( { name : this . state . query } )
45
+ }
46
+ }
47
+ }
48
+
49
+ function pressUpKey ( e ) {
50
+ e . preventDefault ( )
51
+
52
+ // if first item, cycle to the bottom
53
+ if ( this . state . selected <= 0 ) {
54
+ this . setState ( { selected : this . suggestions . state . options . length - 1 } )
55
+ } else {
56
+ this . setState ( { selected : this . state . selected - 1 } )
57
+ }
58
+ }
59
+
60
+ function pressDownKey ( e ) {
61
+ e . preventDefault ( )
62
+
63
+ // if last item, cycle to top
64
+ if ( this . state . selected >= this . suggestions . state . options . length - 1 ) {
65
+ this . setState ( { selected : 0 } )
66
+ } else {
67
+ this . setState ( { selected : this . state . selected + 1 } )
68
+ }
69
+ }
70
+
71
+ function pressBackspaceKey ( ) {
72
+ // when backspace key is pressed and query is blank, delete the last tag
73
+ if ( ! this . state . query . length ) {
74
+ this . deleteTag ( this . props . tags . length - 1 )
75
+ }
76
+ }
77
+
28
78
class ReactTags extends React . Component {
29
79
constructor ( props ) {
30
80
super ( props )
31
81
32
82
this . state = {
33
83
query : '' ,
34
84
focused : false ,
35
- expanded : false ,
36
85
selected : - 1 ,
37
86
classNames : Object . assign ( { } , CLASS_NAMES , this . props . classNames )
38
87
}
@@ -55,51 +104,22 @@ class ReactTags extends React.Component {
55
104
}
56
105
57
106
onKeyDown ( e ) {
58
- const { query, selected } = this . state
59
- const { delimiters } = this . props
60
-
61
- // when one of the terminating keys is pressed, add current query to the tags.
62
- if ( delimiters . indexOf ( e . key ) > - 1 ) {
63
- if ( query || selected > - 1 ) {
64
- e . preventDefault ( )
65
- }
66
-
67
- if ( query . length >= this . props . minQueryLength ) {
68
- // Check if the user typed in an existing suggestion.
69
- const match = this . suggestions . state . options . findIndex ( ( suggestion ) => (
70
- suggestion . name . search ( new RegExp ( `^${ query } $` , 'i' ) ) === 0
71
- ) )
72
-
73
- const index = selected === - 1 ? match : selected
74
-
75
- if ( index > - 1 ) {
76
- this . addTag ( this . suggestions . state . options [ index ] )
77
- } else if ( this . props . allowNew ) {
78
- this . addTag ( { name : query } )
79
- }
80
- }
107
+ // when one of the terminating keys is pressed, add current query to the tags
108
+ if ( this . props . delimiters . indexOf ( e . key ) > - 1 ) {
109
+ pressDelimiterKey . call ( this , e )
81
110
}
82
111
83
112
// when backspace key is pressed and query is blank, delete the last tag
84
- if ( e . key === KEYS . BACKSPACE && query . length === 0 && this . props . allowBackspace ) {
85
- this . deleteTag ( this . props . tags . length - 1 )
113
+ if ( e . key === KEYS . BACKSPACE && this . props . allowBackspace ) {
114
+ pressBackspaceKey . call ( this , e )
86
115
}
87
116
88
117
if ( e . key === KEYS . UP_ARROW ) {
89
- e . preventDefault ( )
90
-
91
- // if last item, cycle to the bottom
92
- if ( selected <= 0 ) {
93
- this . setState ( { selected : this . suggestions . state . options . length - 1 } )
94
- } else {
95
- this . setState ( { selected : selected - 1 } )
96
- }
118
+ pressUpKey . call ( this , e )
97
119
}
98
120
99
121
if ( e . key === KEYS . DOWN_ARROW ) {
100
- e . preventDefault ( )
101
-
102
- this . setState ( { selected : ( selected + 1 ) % this . suggestions . state . options . length } )
122
+ pressDownKey . call ( this , e )
103
123
}
104
124
}
105
125
0 commit comments