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

Adding resetOnSelect behavior to created item #4108

Merged
merged 5 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions packages/select/src/components/query-list/queryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,16 +422,14 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
const item = Utils.safeInvoke(this.props.createNewItemFromQuery, query);
if (item != null) {
Utils.safeInvoke(this.props.onItemSelect, item, evt);
this.setQuery("", true);
this.maybeResetQuery();
}
};

private handleItemSelect = (item: T, event?: React.SyntheticEvent<HTMLElement>) => {
this.setActiveItem(item);
Utils.safeInvoke(this.props.onItemSelect, item, event);
if (this.props.resetOnSelect) {
this.setQuery("", true);
}
this.maybeResetQuery();
};

private handlePaste = (queries: string[]) => {
Expand Down Expand Up @@ -557,6 +555,12 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
executeItemsEqual(this.props.itemsEqual, item, this.state.createNewItem),
);
}

private maybeResetQuery() {
if (this.props.resetOnSelect) {
this.setQuery("", true);
}
}
}

function pxToNumber(value: string | null) {
Expand Down
52 changes: 48 additions & 4 deletions packages/select/test/queryListTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { IQueryListProps } from "@blueprintjs/select";
import { assert } from "chai";
import { mount, ReactWrapper, shallow } from "enzyme";
import * as React from "react";
import * as sinon from "sinon";

// this is an awkward import across the monorepo, but we'd rather not introduce a cyclical dependency or create another package
import { IQueryListProps } from "@blueprintjs/select";
import { IFilm, renderFilm, TOP_100_FILMS } from "../../docs-app/src/examples/select-examples/films";
import {
IQueryListRendererProps,
IQueryListState,
Expand All @@ -30,6 +28,9 @@ import {
QueryList,
} from "../src";

// this is an awkward import across the monorepo, but we'd rather not introduce a cyclical dependency or create another package
import { IFilm, renderFilm, TOP_100_FILMS } from "../../docs-app/src/examples/select-examples/films";

type FilmQueryListWrapper = ReactWrapper<IQueryListProps<IFilm>, IQueryListState<IFilm>>;

describe("<QueryList>", () => {
Expand Down Expand Up @@ -347,5 +348,48 @@ describe("<QueryList>", () => {
assert.isTrue(createNewItemFromQuerySpy.calledWith(trimmedQuery));
assert.isTrue(createNewItemRendererSpy.calledWith(trimmedQuery));
});

it("resets the query after creating new item if resetOnSelect=true", () => {
const onQueryChangeSpy = runResetOnSelectTest(true);
assert.isTrue(onQueryChangeSpy.calledWith(""));
});

it("does not reset the query after creating new item if resetOnSelect=false", () => {
const onQueryChangeSpy = runResetOnSelectTest(false);
assert.isTrue(onQueryChangeSpy.notCalled);
});

function runResetOnSelectTest(resetOnSelect: boolean): sinon.SinonSpy {
let triggerItemCreate: ((e: any) => void) | undefined;
const onQueryChangeSpy = sinon.spy();
// supply a custom renderer so we can hook into handleClick and invoke it ourselves later
const createNewItemRenderer = sinon.spy(
(_query: string, _active: boolean, handleClick: React.MouseEventHandler<HTMLElement>) => {
triggerItemCreate = handleClick;
return <div />;
},
);
const queryList = shallow(
<FilmQueryList
{...testProps}
// Must return something in order for item creation to work.
// tslint:disable-next-line jsx-no-lambda
createNewItemFromQuery={() => ({ title: "irrelevant", rank: 0, year: 0 })}
createNewItemRenderer={createNewItemRenderer}
onQueryChange={onQueryChangeSpy}
resetOnSelect={resetOnSelect}
/>,
);

// Change the query to something non-empty so we can ensure it wasn't cleared.
// Ignore this change in the spy.
(queryList.instance() as QueryList<IFilm>).setQuery("some query");
onQueryChangeSpy.resetHistory();

assert.isDefined(triggerItemCreate, "query list should pass click handler to createNewItemRenderer");
triggerItemCreate!({});

return onQueryChangeSpy;
}
});
});