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

Commit 0de38e2

Browse files
authored
Merge pull request #22 from peterp/pp-custom-strings-to-create-tag
Custom strings to create tag and press return for new tag
2 parents 71c0c85 + dc91789 commit 0de38e2

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ const UselessComponent = () => (
4141

4242
## Props
4343

44-
| PropName | Description | Default |
45-
| ------------------- | ------------------------------------------ | -------- |
46-
| initialText | The input element's text | |
47-
| initialTags | ['the', 'initial', 'tags'] | |
48-
| onChangeTags | Fires when tags are added or removed | |
49-
| maxNumberOfTags | The max number of tags that can be entered | infinity |
50-
| onTagPress | Fires when tags are pressed | |
51-
| readonly | Tags cannot be modified | false |
52-
| deleteTagOnPress | Remove the tag when pressed | true |
53-
| containerStyle | Style | |
54-
| style | Style (`containerStyle` alias) | |
55-
| inputContainerStyle | Style | |
56-
| inputStyle | Style | |
57-
| tagContainerStyle | Style | |
58-
| tagTextStyle | Style | |
44+
| PropName | Description | Default |
45+
| ------------------- | ------------------------------------------ | --------------- |
46+
| initialText | The input element's text | |
47+
| initialTags | ['the', 'initial', 'tags'] | |
48+
| createTagOnString | Triggers new tag creation | [",", ".", " "] |
49+
| onChangeTags | Fires when tags are added or removed | |
50+
| maxNumberOfTags | The max number of tags that can be entered | infinity |
51+
| onTagPress | Fires when tags are pressed | |
52+
| readonly | Tags cannot be modified | false |
53+
| deleteTagOnPress | Remove the tag when pressed | true |
54+
| containerStyle | Style | |
55+
| style | Style (`containerStyle` alias) | |
56+
| inputContainerStyle | Style | |
57+
| inputStyle | Style | |
58+
| tagContainerStyle | Style | |
59+
| tagTextStyle | Style | |

Tags/__tests__/Tags_enzyme-tests.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ describe("Tags", () => {
2020
expect(onChangeTags.mock.calls).toEqual([[["dog"]], [["dog", "cat"]]]);
2121
});
2222

23+
it("should add a new tag when return is pressed", () => {
24+
const onChangeTags = jest.fn();
25+
const wrapper = shallow(
26+
<Tags createTagOnReturn onChangeTags={onChangeTags} />
27+
).find("TextInput");
28+
wrapper.simulate("ChangeText", "dog");
29+
wrapper.simulate("SubmitEditing");
30+
expect(onChangeTags.mock.calls).toEqual([[["dog"]]]);
31+
});
32+
2333
it("should remove a tag when the text is empty", () => {
2434
const onChangeTags = jest.fn();
2535
const wrapper = shallow(<Tags onChangeTags={onChangeTags} />).find(

Tags/__tests__/__snapshots__/Tags-tests.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ exports[`Tags should render props correctly 1`] = `
179179
<TextInput
180180
allowFontScaling={true}
181181
onChangeText={[Function]}
182+
onSubmitEditing={[Function]}
182183
style={
183184
Array [
184185
Object {

Tags/index.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class Tags extends React.Component {
2424
});
2525
}
2626

27+
addTag = text => {
28+
this.setState(
29+
{
30+
tags: [...this.state.tags, text.trim()],
31+
text: " "
32+
},
33+
() => this.props.onChangeTags && this.props.onChangeTags(this.state.tags)
34+
);
35+
};
36+
2737
onChangeText = text => {
2838
if (text.length === 0) {
2939
// `onKeyPress` isn't currently supported on Android; I've placed an extra
@@ -39,22 +49,23 @@ class Tags extends React.Component {
3949
);
4050
} else if (
4151
text.length > 1 &&
42-
(text.slice(-1) === " " || text.slice(-1) === ",") &&
52+
this.props.createTagOnString.includes(text.slice(-1)) &&
4353
!(this.state.tags.indexOf(text.slice(0, -1).trim()) > -1)
4454
) {
45-
this.setState(
46-
{
47-
tags: [...this.state.tags, text.slice(0, -1).trim()],
48-
text: " "
49-
},
50-
() =>
51-
this.props.onChangeTags && this.props.onChangeTags(this.state.tags)
52-
);
55+
this.addTag(text.slice(0, -1));
5356
} else {
5457
this.setState({ text });
5558
}
5659
};
5760

61+
onSubmitEditing = () => {
62+
if (!this.props.createTagOnReturn) {
63+
return;
64+
}
65+
66+
this.addTag(this.state.text);
67+
};
68+
5869
render() {
5970
const {
6071
containerStyle,
@@ -107,6 +118,7 @@ class Tags extends React.Component {
107118
value={this.state.text}
108119
style={[styles.textInput, inputStyle]}
109120
onChangeText={this.onChangeText}
121+
onSubmitEditing={this.onSubmitEditing}
110122
underlineColorAndroid="transparent"
111123
/>
112124
</View>
@@ -119,6 +131,8 @@ class Tags extends React.Component {
119131
Tags.defaultProps = {
120132
initialTags: [],
121133
initialText: " ",
134+
createTagOnString: [",", " "],
135+
createTagOnReturn: false,
122136
readonly: false,
123137
deleteOnTagPress: true,
124138
maxNumberOfTags: Number.POSITIVE_INFINITY
@@ -127,6 +141,8 @@ Tags.defaultProps = {
127141
Tags.propTypes = {
128142
initialText: PropTypes.string,
129143
initialTags: PropTypes.arrayOf(PropTypes.string),
144+
createTagOnString: PropTypes.array,
145+
createTagOnReturn: PropTypes.bool,
130146
onChangeTags: PropTypes.func,
131147
readonly: PropTypes.bool,
132148
maxNumberOfTags: PropTypes.number,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-tags",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "Tag input component for React Native",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)