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

[base-ui][Select] Fix display of selected Options with rich content #40689

Merged
merged 1 commit into from
Jan 19, 2024
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
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());
Copy link
Member

Choose a reason for hiding this comment

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

How does this affect/fix the demo? Testing it out and getting the same value for both innerText and textContent:

Screen.Recording.2024-01-19.at.10.52.58.mov

Copy link
Member Author

Choose a reason for hiding this comment

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

In the "before" demo, the Select is initially "collapsed" (too short).

The difference between innerText and textContent (that I learned today) is that innerText takes display properties into consideration. So if an option is hidden (as it initially is), innerText will return ''. textContent, on the other hand always returns what's defined in the DOM node, regardless of visibility.

Copy link
Member

Choose a reason for hiding this comment

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

Aah, right, it's collapsed, so it's hidden, and innerText doesn't return it, gotcha!


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
Loading