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

[Select][base-ui] Removes highlighted class on item deselection #38790

Closed
wants to merge 16 commits into from
24 changes: 24 additions & 0 deletions packages/mui-base/src/Select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1234,4 +1234,28 @@ describe('<Select />', () => {
expect(renderOption3Spy.callCount).to.equal(0);
expect(renderOption4Spy.callCount).to.equal(0);
});

it('option should have highlighted class only when it is selected', () => {
const { getAllByRole } = render(
<Select multiple defaultValue={[10]} defaultListboxOpen>
<Option value={10}>10</Option>
<Option value={20}>20</Option>
<Option value={30}>30</Option>
<Option value={40}>40</Option>
</Select>,
);

const options = getAllByRole('option');
const secondOption = options[1];

// it doesn't have highlighted class as it is not selected
expect(secondOption).not.to.have.class(optionClasses.highlighted);

// selects option
fireEvent.click(secondOption);
expect(secondOption).to.have.class(optionClasses.highlighted);
// deselects option
fireEvent.click(secondOption);
expect(secondOption).not.to.have.class(optionClasses.highlighted);
});
});
3 changes: 2 additions & 1 deletion packages/mui-base/src/useList/listReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ function handleItemSelection<ItemValue, State extends ListState<ItemValue>>(
// if the item is already selected, remove it from the selection, otherwise add it
const newSelectedValues = toggleSelection(item, selectedValues, selectionMode, itemComparer);

const highlightedValue = selectedValues.includes(items[itemIndex]) ? null : item;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

since itemIndex is calculated by taking itemComparer into consideration, i reused itemIndex here.

return {
...state,
selectedValues: newSelectedValues,
highlightedValue: item,
highlightedValue,
};
}

Expand Down
Loading