From 7308ecc5e03589d69b18be1ac6f40d2aee162133 Mon Sep 17 00:00:00 2001 From: Peter Pistorius Date: Sun, 16 Sep 2018 17:27:28 +0200 Subject: [PATCH] Make return create new tag. --- Tags/__tests__/Tags_enzyme-tests.js | 10 +++++++ .../__snapshots__/Tags-tests.js.snap | 1 + Tags/index.js | 30 ++++++++++++++----- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Tags/__tests__/Tags_enzyme-tests.js b/Tags/__tests__/Tags_enzyme-tests.js index a22c620..280ae38 100644 --- a/Tags/__tests__/Tags_enzyme-tests.js +++ b/Tags/__tests__/Tags_enzyme-tests.js @@ -20,6 +20,16 @@ describe("Tags", () => { expect(onChangeTags.mock.calls).toEqual([[["dog"]], [["dog", "cat"]]]); }); + it("should add a new tag when return is pressed", () => { + const onChangeTags = jest.fn(); + const wrapper = shallow( + + ).find("TextInput"); + wrapper.simulate("ChangeText", "dog"); + wrapper.simulate("SubmitEditing"); + expect(onChangeTags.mock.calls).toEqual([[["dog"]]]); + }); + it("should remove a tag when the text is empty", () => { const onChangeTags = jest.fn(); const wrapper = shallow().find( diff --git a/Tags/__tests__/__snapshots__/Tags-tests.js.snap b/Tags/__tests__/__snapshots__/Tags-tests.js.snap index 78e7835..47bdffd 100644 --- a/Tags/__tests__/__snapshots__/Tags-tests.js.snap +++ b/Tags/__tests__/__snapshots__/Tags-tests.js.snap @@ -179,6 +179,7 @@ exports[`Tags should render props correctly 1`] = ` { + this.setState( + { + tags: [...this.state.tags, text.trim()], + text: " " + }, + () => this.props.onChangeTags && this.props.onChangeTags(this.state.tags) + ); + }; + onChangeText = text => { if (text.length === 0) { // `onKeyPress` isn't currently supported on Android; I've placed an extra @@ -42,19 +52,20 @@ class Tags extends React.Component { this.props.createTagOnString.includes(text.slice(-1)) && !(this.state.tags.indexOf(text.slice(0, -1).trim()) > -1) ) { - this.setState( - { - tags: [...this.state.tags, text.slice(0, -1).trim()], - text: " " - }, - () => - this.props.onChangeTags && this.props.onChangeTags(this.state.tags) - ); + this.addTag(text.slice(0, -1)); } else { this.setState({ text }); } }; + onSubmitEditing = () => { + if (!this.props.createTagOnReturn) { + return; + } + + this.addTag(this.state.text); + }; + render() { const { containerStyle, @@ -107,6 +118,7 @@ class Tags extends React.Component { value={this.state.text} style={[styles.textInput, inputStyle]} onChangeText={this.onChangeText} + onSubmitEditing={this.onSubmitEditing} underlineColorAndroid="transparent" /> @@ -120,6 +132,7 @@ Tags.defaultProps = { initialTags: [], initialText: " ", createTagOnString: [",", " "], + createTagOnReturn: false, readonly: false, deleteOnTagPress: true, maxNumberOfTags: Number.POSITIVE_INFINITY @@ -129,6 +142,7 @@ Tags.propTypes = { initialText: PropTypes.string, initialTags: PropTypes.arrayOf(PropTypes.string), createTagOnString: PropTypes.array, + createTagOnReturn: PropTypes.bool, onChangeTags: PropTypes.func, readonly: PropTypes.bool, maxNumberOfTags: PropTypes.number,