Skip to content

Commit

Permalink
Don't clear TagInput inputValue state if controlled
Browse files Browse the repository at this point in the history
  • Loading branch information
ericjeney committed Nov 8, 2018
1 parent 0b1fc29 commit 78479ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/core/src/components/tag-input/tagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,13 @@ export class TagInput extends AbstractPureComponent<ITagInputProps, ITagInputSta
addOnBlur: false,
addOnPaste: true,
inputProps: {},
inputValue: "",
separator: /[,\n\r]/,
tagProps: {},
};

public state: ITagInputState = {
activeIndex: NONE,
inputValue: this.props.inputValue,
inputValue: this.props.inputValue || "",
isInputFocused: false,
};

Expand All @@ -207,7 +206,7 @@ export class TagInput extends AbstractPureComponent<ITagInputProps, ITagInputSta
super.componentWillReceiveProps(nextProps);

if (nextProps.inputValue !== this.props.inputValue) {
this.setState({ inputValue: nextProps.inputValue });
this.setState({ inputValue: nextProps.inputValue || "" });
}
}

Expand Down Expand Up @@ -261,15 +260,16 @@ export class TagInput extends AbstractPureComponent<ITagInputProps, ITagInputSta
}

private addTags = (value: string) => {
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 = inputValue === undefined;
shouldClearInput = Utils.safeInvoke(onAdd, newValues) !== false && shouldClearInput;
// 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: "" });
}
};
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/tag-input/tagInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ describe("<TagInput>", () => {
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
Expand Down Expand Up @@ -356,6 +362,13 @@ describe("<TagInput>", () => {
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(<TagInput onChange={onChange} values={VALUES} inputValue={NEW_VALUE} />);
pressEnterInInput(wrapper, NEW_VALUE);
assert.strictEqual(wrapper.state().inputValue, NEW_VALUE);
});
});

describe("onKeyDown", () => {
Expand Down

1 comment on commit 78479ec

@ericjeney
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't clear TagInput inputValue state if controlled

Previews: documentation | landing | table

Please sign in to comment.