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

Commit 7308ecc

Browse files
committed
Make return create new tag.
1 parent 62606f4 commit 7308ecc

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

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: 22 additions & 8 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
@@ -42,19 +52,20 @@ class Tags extends React.Component {
4252
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>
@@ -120,6 +132,7 @@ Tags.defaultProps = {
120132
initialTags: [],
121133
initialText: " ",
122134
createTagOnString: [",", " "],
135+
createTagOnReturn: false,
123136
readonly: false,
124137
deleteOnTagPress: true,
125138
maxNumberOfTags: Number.POSITIVE_INFINITY
@@ -129,6 +142,7 @@ Tags.propTypes = {
129142
initialText: PropTypes.string,
130143
initialTags: PropTypes.arrayOf(PropTypes.string),
131144
createTagOnString: PropTypes.array,
145+
createTagOnReturn: PropTypes.bool,
132146
onChangeTags: PropTypes.func,
133147
readonly: PropTypes.bool,
134148
maxNumberOfTags: PropTypes.number,

0 commit comments

Comments
 (0)