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

[Labs] Propagate event correctly when handling empty list #1256

Merged
merged 1 commit into from
Jun 21, 2017
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
4 changes: 3 additions & 1 deletion packages/labs/src/query-list/queryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ export class QueryList<T> extends React.Component<IQueryListProps<T>, IQueryList
this.shouldCheckActiveItemInViewport = false;
}
// reset active item (in the same step) if it's no longer valid
if (this.getActiveIndex() < 0) {
// Also don't fire the event if the active item is already undefined and there is nothing to pick
if (this.getActiveIndex() < 0 &&
(this.state.filteredItems.length !== 0 || this.props.activeItem !== undefined)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we expand this check to account for null values too? (i.e. this.props.activeItem != null)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@cmslewis sure will work on a unit test. I don't think we should add null. Mostly because if this.state.filteredItems is empty, then this.state.filteredItems[0] is undefined. In that case there actually is a difference to props.selectedItem and the event (null vs undefined)

Utils.safeInvoke(this.props.onActiveItemChange, this.state.filteredItems[0]);
}
}
Expand Down
22 changes: 21 additions & 1 deletion packages/labs/test/queryListTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

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

import { Film, TOP_100_FILMS } from "../examples/data";
Expand Down Expand Up @@ -63,6 +63,26 @@ describe("<QueryList>", () => {
assert.isTrue(listPredicate.called, "listPredicate should be invoked");
assert.isFalse(predicate.called, "item predicate should not be invoked");
});

it("ensure onActiveItemChange is not called with undefined and empty list", () => {
const myItem = { title: "Toy Story 3", year: 2010, rank: 1 };
const listPredicate = (query: string, films: Film[]) => {
return films.filter((film) => film.title === query);
};
const onActiveItemChange = sinon.spy(() => {return; });
const filmQueryList = mount(<FilmQueryList
{...props}
items={[myItem]}
activeItem={myItem}
onActiveItemChange={onActiveItemChange}
itemListPredicate={listPredicate}
query=""
/>);
filmQueryList.setProps({query: "FAKE_QUERY"});
filmQueryList.setProps({activeItem: undefined});
assert.isTrue(onActiveItemChange.returned(undefined));
assert.equal(onActiveItemChange.callCount, 1);
});
});

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