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

Commit b66d1a6

Browse files
authored
Merge pull request #17 from seeocon/master
Added the ability to delete on press, and limit the maximum number of tags.
2 parents 8b0e7bd + 2c8c9a1 commit b66d1a6

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

.DS_Store

6 KB
Binary file not shown.

CONTRIBUTORS

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

README.md

Lines changed: 3 additions & 1 deletion
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,12 @@ const UselessComponent = () => (
4343
| initialText | The input element's text |
4444
| initialTags | ['the', 'initial', 'tags'] |
4545
| onChangeTags | Fires when tags are added or removed |
46+
| maxNumberOfTags | integer: up to you (mandatory) |
4647
| onTagPress | Fires when tags are pressed |
4748
| readonly | Tags cannot be modified |
4849
| containerStyle | Style |
4950
| style | Style (`containerStyle` alias) |
5051
| inputStyle | Style |
5152
| tagContainerStyle | Style |
5253
| tagTextStyle | Style |
54+
| deleteOnPress | true/false |

Tags/index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Tags extends React.Component {
3939
);
4040
} else if (
4141
text.length > 1 &&
42-
(text.slice(-1) === " " || text.slice(-1) === ",")
42+
(text.slice(-1) === " " || text.slice(-1) === ",") && !(this.state.tags.indexOf(text.slice(0, -1).trim()) > -1)
4343
) {
4444
this.setState(
4545
{
@@ -54,6 +54,22 @@ class Tags extends React.Component {
5454
}
5555
};
5656

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+
5773
render() {
5874
return (
5975
<View
@@ -63,13 +79,17 @@ class Tags extends React.Component {
6379
<Tag
6480
key={i}
6581
label={tag}
66-
onPress={e => this.props.onTagPress(i, tag, e)}
82+
onPress={e => {
83+
this.arraySplice(tag)
84+
this.props.onTagPress(i, tag, e)
85+
}}
6786
readonly={this.props.readonly}
6887
tagContainerStyle={this.props.tagContainerStyle}
6988
tagTextStyle={this.props.tagTextStyle}
7089
/>
7190
))}
72-
{!this.props.readonly && (
91+
92+
{!this.props.readonly && (this.props.maxNumberOfTags > this.state.tags.length) && (
7393
<View style={[styles.textInputContainer]}>
7494
<TextInput
7595
value={this.state.text}
@@ -94,13 +114,14 @@ Tags.propTypes = {
94114
initialText: PropTypes.string,
95115
initialTags: PropTypes.arrayOf(PropTypes.string),
96116
onChangeTags: PropTypes.func,
97-
onTagPress: PropTypes.func,
98117
containerStyle: PropTypes.object,
99118
style: PropTypes.object,
100119
inputStyle: PropTypes.object,
101120
tagContainerStyle: PropTypes.object,
102121
tagTextStyle: PropTypes.object,
103-
readonly: PropTypes.bool
122+
readonly: PropTypes.bool,
123+
maxNumberOfTags: PropTypes.number,
124+
deleteOnPress: PropTypes.bool
104125
};
105126

106127
export { Tag };

0 commit comments

Comments
 (0)