Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit 4083685

Browse files
committed
Make tag press handling a bit more sane.
1 parent b66d1a6 commit 4083685

File tree

2 files changed

+68
-53
lines changed

2 files changed

+68
-53
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ const UselessComponent = () => (
2929
initialText="monkey"
3030
initialTags={["dog", "cat", "chicken"]}
3131
onChangeTags={tags => console.log(tags)}
32-
onTagPress={(index, tagLabel, event) => console.log(index, tagLabel, event)}
32+
onTagPress={(index, tagLabel, event, deleted) =>
33+
console.log(index, tagLabel, event, deleted ? "deleted" : "not deleted")
34+
}
3335
containerStyle={{ justifyContent: "center" }}
3436
inputStyle={{ backgroundColor: "white" }}
3537
/>
@@ -38,17 +40,17 @@ const UselessComponent = () => (
3840

3941
## Props
4042

41-
| PropName | Description |
42-
| ----------------- | ------------------------------------ |
43-
| initialText | The input element's text |
44-
| initialTags | ['the', 'initial', 'tags'] |
45-
| onChangeTags | Fires when tags are added or removed |
46-
| maxNumberOfTags | integer: up to you (mandatory) |
47-
| onTagPress | Fires when tags are pressed |
48-
| readonly | Tags cannot be modified |
49-
| containerStyle | Style |
50-
| style | Style (`containerStyle` alias) |
51-
| inputStyle | Style |
52-
| tagContainerStyle | Style |
53-
| tagTextStyle | Style |
54-
| deleteOnPress | true/false |
43+
| PropName | Description | Default |
44+
| ----------------- | ------------------------------------------ | -------- |
45+
| initialText | The input element's text | |
46+
| initialTags | ['the', 'initial', 'tags'] | |
47+
| onChangeTags | Fires when tags are added or removed | |
48+
| maxNumberOfTags | The max number of tags that can be entered | infinity |
49+
| onTagPress | Fires when tags are pressed | |
50+
| readonly | Tags cannot be modified | false |
51+
| deleteTagOnPress | Remove the tag when pressed | true |
52+
| containerStyle | Style | |
53+
| style | Style (`containerStyle` alias) | |
54+
| inputStyle | Style | |
55+
| tagContainerStyle | Style | |
56+
| tagTextStyle | Style | |

Tags/index.js

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
107118
Tags.defaultProps = {
108119
initialTags: [],
109120
initialText: " ",
110-
readonly: false
121+
readonly: false,
122+
deleteOnTagPress: true,
123+
maxNumberOfTags: Number.POSITIVE_INFINITY
111124
};
112125

113126
Tags.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

127140
export { Tag };

0 commit comments

Comments
 (0)