@@ -39,7 +39,8 @@ class Tags extends React.Component {
3939 ) ;
4040 } else if (
4141 text . length > 1 &&
42- ( text . slice ( - 1 ) === " " || text . slice ( - 1 ) === "," ) && ! ( this . state . tags . indexOf ( text . slice ( 0 , - 1 ) . trim ( ) ) > - 1 )
42+ ( text . slice ( - 1 ) === " " || text . slice ( - 1 ) === "," ) &&
43+ ! ( this . state . tags . indexOf ( text . slice ( 0 , - 1 ) . trim ( ) ) > - 1 )
4344 ) {
4445 this . setState (
4546 {
@@ -54,51 +55,61 @@ class Tags extends React.Component {
5455 }
5556 } ;
5657
57-
58- /**
59- * void arraySplice(tag)
60- * uses the array.filter() method provided in Javascript to remove the specific tag from the list.
61- *
62- * @param {string } tag
63- */
64- arraySplice ( tag ) {
65- if ( this . props . deleteOnPress == true ) {
66- this . setState ( {
67- tags : this . state . tags . filter ( e => e !== tag )
68- } ) ;
69- }
70- }
71-
72-
7358 render ( ) {
59+ const {
60+ containerStyle,
61+ style,
62+ tagContainerStyle,
63+ tagTextStyle,
64+ deleteOnTagPress,
65+ onTagPress,
66+ readonly,
67+ maxNumberOfTags,
68+ inputStyle
69+ } = this . props ;
70+
7471 return (
75- < View
76- style = { [ styles . container , this . props . containerStyle , this . props . style ] }
77- >
72+ < View style = { [ styles . container , containerStyle , style ] } >
7873 { this . state . tags . map ( ( tag , i ) => (
7974 < Tag
8075 key = { i }
8176 label = { tag }
8277 onPress = { e => {
83- this . arraySplice ( tag )
84- this . props . onTagPress ( i , tag , e )
85- } }
86- readonly = { this . props . readonly }
87- tagContainerStyle = { this . props . tagContainerStyle }
88- tagTextStyle = { this . props . tagTextStyle }
78+ if ( deleteOnTagPress ) {
79+ this . setState (
80+ {
81+ tags : [
82+ ...this . state . tags . slice ( 0 , i ) ,
83+ ...this . state . tags . slice ( i + 1 )
84+ ]
85+ } ,
86+ ( ) => {
87+ this . props . onChangeTags &&
88+ this . props . onChangeTags ( this . state . tags ) ;
89+ onTagPress && onTagPress ( i , tag , e , true ) ;
90+ }
91+ ) ;
92+ } else {
93+ onTagPress && onTagPress ( i , tag , e , false ) ;
94+ }
95+ } }
96+ readonly = { readonly }
97+ tagContainerStyle = { tagContainerStyle }
98+ tagTextStyle = { tagTextStyle }
8999 />
90100 ) ) }
91101
92- { ! this . props . readonly && ( this . props . maxNumberOfTags > this . state . tags . length ) && (
93- < View style = { [ styles . textInputContainer ] } >
94- < TextInput
95- value = { this . state . text }
96- style = { [ styles . textInput , this . props . inputStyle ] }
97- onChangeText = { this . onChangeText }
98- underlineColorAndroid = "transparent"
99- />
100- </ View >
101- ) }
102+ { ! readonly &&
103+ maxNumberOfTags > this . state . tags . length && (
104+ < View style = { [ styles . textInputContainer ] } >
105+ < TextInput
106+ value = { this . state . text }
107+ style = { [ styles . textInput , inputStyle ] }
108+ onChangeText = { this . onChangeText }
109+ underlineColorAndroid = "transparent"
110+ />
111+ </ View >
112+ ) }
102113 </ View >
103114 ) ;
104115 }
@@ -107,7 +118,9 @@ class Tags extends React.Component {
107118Tags . defaultProps = {
108119 initialTags : [ ] ,
109120 initialText : " " ,
110- readonly : false
121+ readonly : false ,
122+ deleteOnTagPress : true ,
123+ maxNumberOfTags : Number . POSITIVE_INFINITY
111124} ;
112125
113126Tags . propTypes = {
@@ -121,7 +134,7 @@ Tags.propTypes = {
121134 tagTextStyle : PropTypes . object ,
122135 readonly : PropTypes . bool ,
123136 maxNumberOfTags : PropTypes . number ,
124- deleteOnPress : PropTypes . bool
137+ deleteOnTagPress : PropTypes . bool
125138} ;
126139
127140export { Tag } ;
0 commit comments