Skip to content

Commit

Permalink
Don't clear TagInput inputValue state if controlled (#3137)
Browse files Browse the repository at this point in the history
* Don't clear TagInput inputValue state if controlled

Fixes #3134

* Address PR comments

* Revert fixes
  • Loading branch information
ericjeney authored and giladgray committed Nov 15, 2018
1 parent 650c2f7 commit 412f0e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
19 changes: 9 additions & 10 deletions packages/core/src/components/tag-input/tagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,15 @@ 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 = 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: "" });
}
};
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 412f0e3

@blueprint-bot
Copy link

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 (#3137)

Previews: documentation | landing | table

Please sign in to comment.