@@ -25,14 +25,63 @@ const CLASS_NAMES = {
2525 suggestionDisabled : 'is-disabled'
2626}
2727
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+
2878class ReactTags extends React . Component {
2979 constructor ( props ) {
3080 super ( props )
3181
3282 this . state = {
3383 query : '' ,
3484 focused : false ,
35- expanded : false ,
3685 selected : - 1 ,
3786 classNames : Object . assign ( { } , CLASS_NAMES , this . props . classNames )
3887 }
@@ -55,51 +104,22 @@ class ReactTags extends React.Component {
55104 }
56105
57106 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 )
81110 }
82111
83112 // 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 )
86115 }
87116
88117 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 )
97119 }
98120
99121 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 )
103123 }
104124 }
105125
0 commit comments