Skip to content

Commit

Permalink
[base-ui][Select] Fix display of selected Options with rich content (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldudak committed Jan 19, 2024
1 parent c5bc62a commit 635db56
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/mui-base/src/Option/Option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const InnerOption = React.memo(
// If `label` is not explicitly provided, the `children` are used for convenience.
// This is used to populate the select's trigger with the selected option's label.
const computedLabel =
label ?? (typeof children === 'string' ? children : optionRef.current?.innerText);
label ?? (typeof children === 'string' ? children : optionRef.current?.textContent?.trim());

const { getRootProps, selected, highlighted, index } = useOption({
disabled,
Expand Down
45 changes: 45 additions & 0 deletions packages/mui-base/src/Select/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,51 @@ describe('<Select />', () => {
skip: ['componentProp', 'reactTestRenderer'],
}));

describe('selected option rendering', () => {
it('renders the selected option when it is specified as an only child', async () => {
const markup = (
<Select defaultValue="1">
<Option value="1">One</Option>
</Select>
);

const { getByRole } = await render(markup);
const select = getByRole('combobox');

expect(select).to.have.text('One');
});

it('renders the selected option when it is specified among many children', async () => {
const markup = (
<Select defaultValue="1">
<Option value="1">
<img src="one.png" alt="One" /> One
</Option>
</Select>
);

const { getByRole } = await render(markup);
const select = getByRole('combobox');

expect(select).to.have.text('One');
});

it('renders the selected option when it is specified in the label prop', async () => {
const markup = (
<Select defaultValue="1">
<Option value="1" label="One">
<img src="one.png" alt="One" />
</Option>
</Select>
);

const { getByRole } = await render(markup);
const select = getByRole('combobox');

expect(select).to.have.text('One');
});
});

describe('keyboard navigation', () => {
['Enter', 'ArrowDown', 'ArrowUp', ' '].forEach((key) => {
it(`opens the dropdown when the "${key}" key is down on the button`, async () => {
Expand Down

0 comments on commit 635db56

Please sign in to comment.