Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TagInput] onAdd can return false to prevent clearing input #1309

Merged
merged 7 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/labs/src/tag-input/tagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export interface ITagInputProps extends IProps {
* Callback invoked when a new tag is added by the user pressing `enter` on the input.
* Receives the current value of the input field. New tags are expected to be appended to
* the list.
*
* If the provided function returns `false`, the input will not be cleared. This is useful,
* for example, if the provided `value` is somehow invalid and should not be added as a tag.
*/
onAdd?: (value: string) => void;
onAdd?: (value: string) => boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

What about boolean | void, so that you can return both true or nothing if you want the tag to be added?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yep, missed that. Fixed. 👍


/**
* Callback invoked when the user clicks the X button on a tag.
Expand Down Expand Up @@ -167,8 +170,10 @@ export class TagInput extends AbstractComponent<ITagInputProps, ITagInputState>
const { selectionEnd, value } = event.currentTarget;
if (event.which === Keys.ENTER && value.length > 0) {
// enter key on non-empty string invokes onAdd
this.setState({ inputValue: "" });
Utils.safeInvoke(this.props.onAdd, value);
const shouldClearInput = Utils.safeInvoke(this.props.onAdd, value);
if (shouldClearInput) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a switch in the examples for "Unique values" 👍

Can also come as a follow up PR, happy to take care of this

Copy link
Contributor Author

@cmslewis cmslewis Jun 30, 2017

Choose a reason for hiding this comment

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

I'm iffy on adding a "unique values" switch, because it's doesn't correspond to out-of-the-box behavior. Would love to figure out a good way to communicate how to do that in a future PR though. Maybe we could just include a code snippet similar to NumericInputs section on uncontrolled usage:

http://blueprintjs.com/docs/#core/components/forms/numeric-input.uncontrolled-mode

Copy link
Contributor

@llorca llorca Jun 30, 2017

Choose a reason for hiding this comment

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

That's fair, I mean I guess the documentation speaks for itself now, we really were lacking this return false capability

this.setState({ inputValue: "" });
}
} else if (selectionEnd === 0 && this.props.values.length > 0) {
// cursor at beginning of input allows interaction with tags.
// use selectionEnd to verify cursor position and no text selection.
Expand Down
48 changes: 35 additions & 13 deletions packages/labs/test/tagInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { assert } from "chai";
import { mount, shallow } from "enzyme";
import { mount, shallow, ShallowWrapper } from "enzyme";
import * as React from "react";

import { Intent, Keys, Tag } from "@blueprintjs/core";
Expand Down Expand Up @@ -53,27 +53,49 @@ describe("<TagInput>", () => {
});

describe("onAdd", () => {
const NEW_VALUE = "new item";

it("is not invoked on enter when input is empty", () => {
const onAdd = sinon.spy();
const wrapper = shallow(<TagInput onAdd={onAdd} values={VALUES} />);
wrapper.find("input").simulate("keydown", {
currentTarget: { value: "" },
which: Keys.ENTER,
});
const onAdd = sinon.stub().returns(true);
const wrapper = mountTagInput(onAdd);
pressEnterInInput(wrapper, "");
assert.isTrue(onAdd.notCalled);
});

it("is invoked on enter", () => {
const value = "new item";
const onAdd = sinon.spy();
const wrapper = shallow(<TagInput onAdd={onAdd} values={VALUES} />);
const onAdd = sinon.stub().returns(true);
const wrapper = mountTagInput(onAdd);
pressEnterInInput(wrapper, NEW_VALUE);
assert.isTrue(onAdd.calledOnce);
assert.strictEqual(onAdd.args[0][0], NEW_VALUE);
});

it("does not clear the input if onAdd returns false", () => {
const onAdd = sinon.stub().returns(false);
const wrapper = mountTagInput(onAdd);
wrapper.setState({ inputValue: NEW_VALUE });
pressEnterInInput(wrapper, NEW_VALUE);
assert.strictEqual(wrapper.state().inputValue, NEW_VALUE);
});

it("clears the input if onAdd returns true", () => {
const onAdd = sinon.stub().returns(true);
const wrapper = mountTagInput(onAdd);
wrapper.setState({ inputValue: NEW_VALUE });
pressEnterInInput(wrapper, NEW_VALUE);
assert.strictEqual(wrapper.state().inputValue, "");
});

function mountTagInput(onAdd: Sinon.SinonStub) {
return shallow(<TagInput onAdd={onAdd} values={VALUES} />);
}

function pressEnterInInput(wrapper: ShallowWrapper<any, any>, value: string) {
wrapper.find("input").simulate("keydown", {
currentTarget: { value },
which: Keys.ENTER,
});
assert.isTrue(onAdd.calledOnce);
assert.strictEqual(onAdd.args[0][0], value);
});
}
});

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