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

Fix reset of active item when query hasn't changed #3072

Merged
merged 2 commits into from
Oct 25, 2018
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
11 changes: 7 additions & 4 deletions packages/select/src/components/query-list/queryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,22 @@ export class QueryList<T> extends React.Component<IQueryListProps<T>, IQueryList

public setQuery(query: string, resetActiveItem = this.props.resetOnQuery) {
this.shouldCheckActiveItemInViewport = true;
if (query !== this.state.query) {
const hasQueryChanged = query !== this.state.query;
if (hasQueryChanged) {
Utils.safeInvoke(this.props.onQueryChange, query);
}

const filteredItems = getFilteredItems(query, this.props);
this.setState({ filteredItems, query });

// always reset active item if it's now filtered or disabled
const activeIndex = this.getActiveIndex(filteredItems);
if (
const shouldUpdateActiveItem =
resetActiveItem ||
activeIndex < 0 ||
isItemDisabled(this.state.activeItem, activeIndex, this.props.itemDisabled)
) {
isItemDisabled(this.state.activeItem, activeIndex, this.props.itemDisabled);

if (hasQueryChanged && shouldUpdateActiveItem) {
this.setActiveItem(getFirstEnabledItem(filteredItems, this.props.itemDisabled));
}
}
Expand Down
16 changes: 15 additions & 1 deletion packages/select/test/queryListTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
*/

import { assert } from "chai";
import { mount, shallow } from "enzyme";
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, ItemListPredicate, ItemListRenderer, QueryList } from "../src/index";

Expand Down Expand Up @@ -90,6 +91,19 @@ describe("<QueryList>", () => {
filmQueryList.setState({ activeItem: undefined });
assert.equal(testProps.onActiveItemChange.callCount, 0);
});

it("ensure onActiveItemChange is not called updating props and query doesn't change", () => {
const myItem = { title: "Toy Story 3", year: 2010, rank: 1 };
const props: IQueryListProps<IFilm> = {
...testProps,
activeItem: myItem,
items: [myItem],
query: "",
};
const filmQueryList: ReactWrapper<IQueryListProps<IFilm>> = mount(<FilmQueryList {...props} />);
filmQueryList.setProps(props);
assert.equal(testProps.onActiveItemChange.callCount, 0);
});
});

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