11import React from "react" ;
22import PropTypes from "prop-types" ;
3- import { View , TextInput } from "react-native" ;
3+ import { View } from "react-native" ;
44
55import Tag from "./Tag" ;
6+ import Input from "./Input" ;
67import styles from "./styles" ;
78
89class Tags extends React . Component {
10+
911 constructor ( props ) {
1012 super ( props ) ;
11-
1213 this . state = {
1314 tags : props . initialTags ,
1415 text : props . initialText
1516 } ;
1617 }
1718
19+ showLastTag = ( ) => {
20+ this . setState ( state =>
21+ ( {
22+ tags : state . tags . slice ( 0 , - 1 ) ,
23+ text : state . tags . slice ( - 1 ) [ 0 ] || " "
24+ } ) ,
25+ ( ) =>
26+ this . props . onChangeTags && this . props . onChangeTags ( this . state . tags )
27+ ) ;
28+ } ;
29+
1830 addTag = text => {
19- this . setState (
20- {
21- tags : [ ...this . state . tags , text . trim ( ) ] ,
31+ this . setState ( state =>
32+ ( {
33+ tags : [ ...state . tags , text . trim ( ) ] ,
2234 text : " "
23- } ,
35+ } ) ,
2436 ( ) => this . props . onChangeTags && this . props . onChangeTags ( this . state . tags )
2537 ) ;
2638 } ;
2739
2840 onChangeText = text => {
2941 if ( text . length === 0 ) {
30- // `onKeyPress` isn't currently supported on Android; I've placed an extra
31- // space character at the start of `TextInput` which is used to determine if the
32- // user is erasing.
33- this . setState (
34- {
35- tags : this . state . tags . slice ( 0 , - 1 ) ,
36- text : this . state . tags . slice ( - 1 ) [ 0 ] || " "
37- } ,
38- ( ) =>
39- this . props . onChangeTags && this . props . onChangeTags ( this . state . tags )
40- ) ;
42+ this . showLastTag ( ) ;
4143 } else if (
4244 text . length > 1 &&
4345 this . props . createTagOnString . includes ( text . slice ( - 1 ) ) &&
@@ -53,74 +55,83 @@ class Tags extends React.Component {
5355 if ( ! this . props . createTagOnReturn ) {
5456 return ;
5557 }
56-
5758 this . addTag ( this . state . text ) ;
5859 } ;
5960
6061 render ( ) {
62+
6163 const {
6264 containerStyle,
6365 style,
64- tagContainerStyle,
65- tagTextStyle,
66- deleteTagOnPress,
67- onTagPress,
6866 readonly,
69- maxNumberOfTags,
70- inputStyle,
71- inputContainerStyle,
72- textInputProps,
73- renderTag
67+ maxNumberOfTags
7468 } = this . props ;
7569
7670 return (
7771 < View style = { [ styles . container , containerStyle , style ] } >
78- { this . state . tags . map ( ( tag , index ) => {
79- const tagProps = {
80- tag,
81- index,
82- deleteTagOnPress,
83- onPress : e => {
84- if ( deleteTagOnPress && ! readonly ) {
85- this . setState (
86- {
87- tags : [
88- ...this . state . tags . slice ( 0 , index ) ,
89- ...this . state . tags . slice ( index + 1 )
90- ]
91- } ,
92- ( ) => {
93- this . props . onChangeTags &&
94- this . props . onChangeTags ( this . state . tags ) ;
95- onTagPress && onTagPress ( index , tag , e , true ) ;
96- }
97- ) ;
98- } else {
99- onTagPress && onTagPress ( index , tag , e , false ) ;
100- }
101- } ,
102- tagContainerStyle,
103- tagTextStyle
104- } ;
105-
106- return renderTag ( tagProps ) ;
107- } ) }
108-
109- { ! readonly && maxNumberOfTags > this . state . tags . length && (
110- < View style = { [ styles . textInputContainer , inputContainerStyle ] } >
111- < TextInput
112- { ...textInputProps }
113- value = { this . state . text }
114- style = { [ styles . textInput , inputStyle ] }
115- onChangeText = { this . onChangeText }
116- onSubmitEditing = { this . onSubmitEditing }
117- underlineColorAndroid = "transparent"
118- />
119- </ View >
120- ) }
72+
73+ { this . state . tags . map ( this . _renderTag ) }
74+
75+ { ! readonly
76+ && maxNumberOfTags > this . state . tags . length
77+ && (
78+ < Input
79+ value = { this . state . text }
80+ onChangeText = { this . onChangeText }
81+ onSubmitEditing = { this . onSubmitEditing }
82+ { ...this . props }
83+ /> )
84+ }
85+
12186 </ View >
12287 ) ;
88+
12389 }
90+
91+ _renderTag = ( tag , index ) => {
92+
93+ const {
94+ tagContainerStyle,
95+ tagTextStyle,
96+ deleteTagOnPress,
97+ onTagPress,
98+ readonly,
99+ renderTag
100+ } = this . props ;
101+
102+ const onPress = e => {
103+ if ( deleteTagOnPress && ! readonly ) {
104+ this . setState ( state =>
105+ ( {
106+ tags : [
107+ ...state . tags . slice ( 0 , index ) ,
108+ ...state . tags . slice ( index + 1 )
109+ ]
110+ } ) ,
111+ ( ) => {
112+ this . props . onChangeTags &&
113+ this . props . onChangeTags ( this . state . tags ) ;
114+ onTagPress && onTagPress ( index , tag , e , true ) ;
115+ }
116+ ) ;
117+ } else {
118+ onTagPress && onTagPress ( index , tag , e , false ) ;
119+ }
120+ } ;
121+
122+ const tagProps = {
123+ tag,
124+ index,
125+ deleteTagOnPress,
126+ onPress,
127+ tagContainerStyle,
128+ tagTextStyle
129+ } ;
130+
131+ return renderTag ( tagProps ) ;
132+
133+ } ;
134+
124135}
125136
126137Tags . defaultProps = {
@@ -156,5 +167,5 @@ Tags.propTypes = {
156167 textInputProps : PropTypes . object
157168} ;
158169
159- export { Tag } ;
160- export default Tags ;
170+ export { Tags } ;
171+ export default Tags ;
0 commit comments