Skip to content

Commit

Permalink
Fix reset of active item when query hasn't changed (#3072)
Browse files Browse the repository at this point in the history
* Fix reset active item when query hasn't changed

* Address comments
  • Loading branch information
jscheiny authored and giladgray committed Oct 25, 2018
1 parent dbf9a98 commit 9145edd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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

1 comment on commit 9145edd

@blueprint-bot
Copy link

Choose a reason for hiding this comment

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

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

Previews: documentation | landing | table

Please sign in to comment.