Skip to content
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
43 changes: 43 additions & 0 deletions src/components/List/List.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,49 @@ describe('<List />', () => {

sinon.assert.callCount(onSelect, 2);
});

it('should call onSelect when selected items are removed from the items list', () => {
const component = mount(
<List
items={itemObjs}
select="checkbox"
selected={[itemObjs[0], itemObjs[1]]}
selectedKeyMapper={(item) => item.key}
onSelect={onSelect}
>
{(item) => <div id={item.name.toLowerCase()}>{item.name}</div>}
</List>
);

sinon.assert.notCalled(onSelect);

// Remove itemObjs[0] which is selected
component.setProps({ items: [itemObjs[1], itemObjs[2]] });
component.update();

sinon.assert.calledOnce(onSelect);
sinon.assert.calledWith(onSelect, [itemObjs[1]]);
});

it('should not call onSelect when items change but no selected items are removed', () => {
const component = mount(
<List
items={itemObjs}
select="checkbox"
selected={[itemObjs[1]]}
selectedKeyMapper={(item) => item.key}
onSelect={onSelect}
>
{(item) => <div id={item.name.toLowerCase()}>{item.name}</div>}
</List>
);

// Update items but keep the selected item
component.setProps({ items: [itemObjs[0], itemObjs[1]] });
component.update();

sinon.assert.notCalled(onSelect);
});
});
});

Expand Down
8 changes: 7 additions & 1 deletion src/components/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ function List<T extends Item>({

useDeepCompareEffect(() => {
const includes = (xs: T[], x: T) => xs.map(selectedKeyMapper).includes(selectedKeyMapper(x));
let hasRemovedItems = false;
selection.forEach((item) => {
if (!includes(items, item)) {
removeItem(item);
hasRemovedItems = true;
}
});
}, [items, Array.from(selection.values()), selectedKeyMapper]);

if (hasRemovedItems) {
onSelect(Array.from(selection.values()));
}
}, [items, Array.from(selection.values()), selectedKeyMapper, onSelect]);

useDeepCompareEffect(() => {
if (selectAllRef.current) {
Expand Down
Loading