1
- import React from 'react'
1
+ import React , { Component , PropTypes } from 'react'
2
2
3
3
function defaultRenderTag ( props ) {
4
4
let { tag, key, onRemove, classNameRemove, ...other } = props
@@ -11,10 +11,10 @@ function defaultRenderTag (props) {
11
11
}
12
12
13
13
defaultRenderTag . propTypes = {
14
- key : React . PropTypes . number ,
15
- tag : React . PropTypes . string ,
16
- onRemove : React . PropTypes . function ,
17
- classNameRemove : React . PropTypes . string
14
+ key : PropTypes . number ,
15
+ tag : PropTypes . string ,
16
+ onRemove : PropTypes . function ,
17
+ classNameRemove : PropTypes . string
18
18
}
19
19
20
20
function defaultRenderInput ( props ) {
@@ -25,8 +25,8 @@ function defaultRenderInput (props) {
25
25
}
26
26
27
27
defaultRenderInput . propTypes = {
28
- value : React . PropTypes . string ,
29
- onChange : React . PropTypes . function
28
+ value : PropTypes . string ,
29
+ onChange : PropTypes . function
30
30
}
31
31
32
32
function defaultRenderLayout ( tagComponents , inputComponent ) {
@@ -38,42 +38,14 @@ function defaultRenderLayout (tagComponents, inputComponent) {
38
38
)
39
39
}
40
40
41
- class TagsInput extends React . Component {
41
+ class TagsInput extends Component {
42
42
constructor ( ) {
43
43
super ( )
44
44
this . state = { tag : '' }
45
45
this . focus = ::this . focus
46
46
this . blur = ::this . blur
47
47
}
48
48
49
- static propTypes = {
50
- addKeys : React . PropTypes . array ,
51
- addOnBlur : React . PropTypes . bool ,
52
- inputProps : React . PropTypes . object ,
53
- onChange : React . PropTypes . func . isRequired ,
54
- removeKeys : React . PropTypes . array ,
55
- renderInput : React . PropTypes . func ,
56
- renderTag : React . PropTypes . func ,
57
- renderLayout : React . PropTypes . func ,
58
- tagProps : React . PropTypes . object ,
59
- onlyUnique : React . PropTypes . bool ,
60
- value : React . PropTypes . array . isRequired ,
61
- maxTags : React . PropTypes . number
62
- }
63
-
64
- static defaultProps = {
65
- className : 'react-tagsinput' ,
66
- addKeys : [ 9 , 13 ] ,
67
- inputProps : { className : 'react-tagsinput-input' } ,
68
- removeKeys : [ 8 ] ,
69
- renderInput : defaultRenderInput ,
70
- renderTag : defaultRenderTag ,
71
- renderLayout : defaultRenderLayout ,
72
- tagProps : { className : 'react-tagsinput-tag' , classNameRemove : 'react-tagsinput-remove' } ,
73
- onlyUnique : false ,
74
- maxTags : - 1
75
- }
76
-
77
49
_removeTag ( index ) {
78
50
let value = this . props . value . concat ( [ ] )
79
51
if ( index > - 1 && index < value . length ) {
@@ -110,15 +82,17 @@ class TagsInput extends React.Component {
110
82
}
111
83
112
84
handleKeyDown ( e ) {
113
- let { value, removeKeys, addKeys} = this . props
85
+ let { value, removeKeys, addKeys, validationRegex } = this . props
114
86
let { tag} = this . state
115
87
let empty = tag === ''
116
88
let add = addKeys . indexOf ( e . keyCode ) !== - 1
117
89
let remove = removeKeys . indexOf ( e . keyCode ) !== - 1
118
90
119
91
if ( add ) {
120
92
e . preventDefault ( )
121
- this . _addTag ( tag )
93
+ if ( validationRegex . test ( tag ) ) {
94
+ this . _addTag ( tag )
95
+ }
122
96
}
123
97
124
98
if ( remove && value . length > 0 && empty ) {
@@ -184,4 +158,34 @@ class TagsInput extends React.Component {
184
158
}
185
159
}
186
160
161
+ TagsInput . propTypes = {
162
+ addKeys : PropTypes . array ,
163
+ addOnBlur : PropTypes . bool ,
164
+ inputProps : PropTypes . object ,
165
+ onChange : PropTypes . func . isRequired ,
166
+ removeKeys : PropTypes . array ,
167
+ renderInput : PropTypes . func ,
168
+ renderTag : PropTypes . func ,
169
+ renderLayout : PropTypes . func ,
170
+ tagProps : PropTypes . object ,
171
+ onlyUnique : PropTypes . bool ,
172
+ value : PropTypes . array . isRequired ,
173
+ maxTags : PropTypes . number ,
174
+ validationRegex : PropTypes . regexp
175
+ }
176
+
177
+ TagsInput . defaultProps = {
178
+ className : 'react-tagsinput' ,
179
+ addKeys : [ 9 , 13 ] ,
180
+ inputProps : { className : 'react-tagsinput-input' } ,
181
+ removeKeys : [ 8 ] ,
182
+ renderInput : defaultRenderInput ,
183
+ renderTag : defaultRenderTag ,
184
+ renderLayout : defaultRenderLayout ,
185
+ tagProps : { className : 'react-tagsinput-tag' , classNameRemove : 'react-tagsinput-remove' } ,
186
+ onlyUnique : false ,
187
+ maxTags : - 1 ,
188
+ validationRegex : / .* /
189
+ }
190
+
187
191
export default TagsInput
0 commit comments