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

Commit 3514314

Browse files
Michael O'ConnorMichael O'Connor
authored andcommitted
Added the ability to delete on press, and add a maximum number for tags
1 parent 228df0f commit 3514314

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

.DS_Store

6 KB
Binary file not shown.

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Federico Martín Alconada Verzini (https://github.com/fedealconada)
22
Havens (https://github.com/havenS)
3+
Michael O'Connor Havens (https://github.com/seeocon)

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# React-Native-Tags
1+
# React-Native-Tags (with maximum tag and delete on press support)
22

33
[![Build Status](https://travis-ci.org/peterp/react-native-tags.svg?branch=master)](https://travis-ci.org/peterp/react-native-tags)
44
[![npm](https://img.shields.io/npm/dt/express.svg)](https://www.npmjs.com/package/react-native-tags)
@@ -43,10 +43,11 @@ const UselessComponent = () => (
4343
| initialText | The input element's text |
4444
| initialTags | ['the', 'initial', 'tags'] |
4545
| onChangeTags | Fires when tags are added or removed |
46-
| onTagPress | Fires when tags are pressed |
46+
| maxNumberOfTags | integer: up to you (mandatory) |
4747
| readonly | Removes the TextInput |
4848
| containerStyle | Style |
4949
| style | Style (`containerStyle` alias) |
5050
| inputStyle | Style |
5151
| tagContainerStyle | Style |
5252
| tagTextStyle | Style |
53+
| deleteOnPress | true/false |

Tags/index.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Tags extends React.Component {
5252
);
5353
} else if (
5454
text.length > 1 &&
55-
(text.slice(-1) === " " || text.slice(-1) === ",")
55+
(text.slice(-1) === " " || text.slice(-1) === ",") && !(this.state.tags.indexOf(text.slice(0, -1).trim()) > -1)
5656
) {
5757
this.setState(
5858
{
@@ -67,6 +67,22 @@ class Tags extends React.Component {
6767
}
6868
};
6969

70+
71+
/**
72+
* void arraySplice(tag)
73+
* uses the array.filter() method provided in Javascript to remove the specific tag from the list.
74+
*
75+
* @param {string} tag
76+
*/
77+
arraySplice(tag) {
78+
if (this.props.deleteOnPress == true){
79+
this.setState({
80+
tags: this.state.tags.filter(e => e !== tag)
81+
});
82+
}
83+
}
84+
85+
7086
render() {
7187
return (
7288
<View
@@ -76,12 +92,13 @@ class Tags extends React.Component {
7692
<Tag
7793
key={i}
7894
label={tag}
79-
onPress={e => this.props.onTagPress(i, tag, e)}
95+
onPress={() => this.arraySplice(tag)}
8096
tagContainerStyle={this.props.tagContainerStyle}
8197
tagTextStyle={this.props.tagTextStyle}
8298
/>
8399
))}
84-
{!this.props.readonly && (
100+
101+
{!this.props.readonly && (this.props.maxNumberOfTags > this.state.tags.length) && (
85102
<View style={[styles.textInputContainer]}>
86103
<TextInput
87104
value={this.state.text}
@@ -106,13 +123,14 @@ Tags.propTypes = {
106123
initialText: PropTypes.string,
107124
initialTags: PropTypes.arrayOf(PropTypes.string),
108125
onChangeTags: PropTypes.func,
109-
onTagPress: PropTypes.func,
110126
containerStyle: PropTypes.object,
111127
style: PropTypes.object,
112128
inputStyle: PropTypes.object,
113129
tagContainerStyle: PropTypes.object,
114130
tagTextStyle: PropTypes.object,
115-
readonly: PropTypes.bool
131+
readonly: PropTypes.bool,
132+
maxNumberOfTags: PropTypes.number,
133+
deleteOnPress: PropTypes.bool
116134
};
117135

118136
export { Tag };

0 commit comments

Comments
 (0)