From 412f0e39713e43fe6582b2db49fb4c6730fd67bc Mon Sep 17 00:00:00 2001 From: ericjeney Date: Thu, 15 Nov 2018 13:57:42 -0500 Subject: [PATCH] Don't clear TagInput inputValue state if controlled (#3137) * Don't clear TagInput inputValue state if controlled Fixes #3134 * Address PR comments * Revert fixes --- .../src/components/tag-input/tagInput.tsx | 19 +++++++++---------- .../core/test/tag-input/tagInputTests.tsx | 13 +++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/core/src/components/tag-input/tagInput.tsx b/packages/core/src/components/tag-input/tagInput.tsx index 90ae8b48e1..a5fc082308 100644 --- a/packages/core/src/components/tag-input/tagInput.tsx +++ b/packages/core/src/components/tag-input/tagInput.tsx @@ -169,9 +169,9 @@ export interface ITagInputProps extends IProps { } export interface ITagInputState { - activeIndex?: number; - inputValue?: string; - isInputFocused?: boolean; + activeIndex: number; + inputValue: string; + isInputFocused: boolean; } /** special value for absence of active tag */ @@ -184,14 +184,13 @@ export class TagInput extends AbstractPureComponent { - const { onAdd, onChange, values } = this.props; + const { inputValue, onAdd, onChange, values } = this.props; const newValues = this.getValues(value); - let shouldClearInput = Utils.safeInvoke(onAdd, newValues); + let shouldClearInput = Utils.safeInvoke(onAdd, newValues) !== false && inputValue === undefined; // avoid a potentially expensive computation if this prop is omitted if (Utils.isFunction(onChange)) { - shouldClearInput = shouldClearInput || onChange([...values, ...newValues]); + shouldClearInput = onChange([...values, ...newValues]) !== false && shouldClearInput; } // only explicit return false cancels text clearing - if (shouldClearInput !== false) { + if (shouldClearInput) { this.setState({ inputValue: "" }); } }; diff --git a/packages/core/test/tag-input/tagInputTests.tsx b/packages/core/test/tag-input/tagInputTests.tsx index dd36ba9dfa..764fdd761a 100644 --- a/packages/core/test/tag-input/tagInputTests.tsx +++ b/packages/core/test/tag-input/tagInputTests.tsx @@ -210,6 +210,12 @@ describe("", () => { assert.strictEqual(wrapper.state().inputValue, ""); }); + it("does not clear the input if the input is controlled", () => { + const wrapper = mountTagInput(undefined, { inputValue: NEW_VALUE }); + pressEnterInInput(wrapper, NEW_VALUE); + assert.strictEqual(wrapper.state().inputValue, NEW_VALUE); + }); + it("splits input value on separator RegExp", () => { const onAdd = sinon.stub(); // this is actually the defaultProps value, but reproducing here for explicitness @@ -356,6 +362,13 @@ describe("", () => { pressEnterInInput(wrapper, NEW_VALUE); assert.strictEqual(wrapper.state().inputValue, ""); }); + + it("does not clear the input if the input is controlled", () => { + const onChange = sinon.stub(); + const wrapper = shallow(); + pressEnterInInput(wrapper, NEW_VALUE); + assert.strictEqual(wrapper.state().inputValue, NEW_VALUE); + }); }); describe("onKeyDown", () => {