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

fix: update Dropdown and MultiSelect to handle interactive labels #18643

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 43 additions & 2 deletions packages/react/src/components/Dropdown/Dropdown.stories.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import { FolderOpen, Folders, Information, View } from '@carbon/icons-react';

import { WithLayer } from '../../../.storybook/templates/WithLayer';

import { default as Dropdown, DropdownSkeleton } from './';
import Button from '../Button';
import { AILabel, AILabelContent, AILabelActions } from '../AILabel';
import {
Toggletip,
ToggletipActions,
ToggletipButton,
ToggletipContent,
ToggletipLabel,
} from '../Toggletip';
import { IconButton } from '../IconButton';
import { View, FolderOpen, Folders } from '@carbon/icons-react';
import mdx from './Dropdown.mdx';
import Link from '../Link';

export default {
title: 'Components/Dropdown',
Expand Down Expand Up @@ -452,3 +460,36 @@ export const withAILabel = (args) => {
withAILabel.argTypes = {
...sharedArgTypes,
};

export const withToggletipLabel = () => {
return (
<div>
<Dropdown
label="placeholder"
id="dropdown"
items={[]}
titleText={
<div style={{ display: 'flex', alignItems: 'center' }}>
<ToggletipLabel>Dropdown title</ToggletipLabel>
<Toggletip>
<ToggletipButton label="Show information">
<Information />
</ToggletipButton>
<ToggletipContent>
<p>
Lorem ipsum dolor sit amet, di os consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut fsil labore et dolore
magna aliqua.
</p>
<ToggletipActions>
<Link href="#">Link action</Link>
<Button size="sm">Button</Button>
</ToggletipActions>
</ToggletipContent>
</Toggletip>
</div>
}
/>
</div>
);
};
14 changes: 8 additions & 6 deletions packages/react/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/**
* Copyright IBM Corp. 2022
* Copyright IBM Corp. 2022, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, {
useCallback,
useContext,
useState,
FocusEvent,
ForwardedRef,
isValidElement,
MouseEvent,
ReactNode,
useCallback,
useContext,
useEffect,
useMemo,
ReactElement,
useState,
} from 'react';
import {
useSelect,
Expand Down Expand Up @@ -608,10 +608,12 @@ const Dropdown = React.forwardRef(
return React.isValidElement(element) ? element : null;
}, [slug, decorator]);

const labelProps = !isValidElement(titleText) ? getLabelProps() : null;

return (
<div className={wrapperClasses} {...other}>
{titleText && (
<label className={titleClasses} {...getLabelProps()}>
<label className={titleClasses} {...labelProps}>
{titleText}
</label>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand Down Expand Up @@ -458,4 +458,20 @@ describe('Test useEffect ', () => {
await waitForPosition();
assertMenuClosed();
});

it('should add label props when `titleText` is a string', () => {
render(<Dropdown {...mockProps} titleText="Dropdown Title" />);

const label = screen.getByText('Dropdown Title').closest('label');

expect(label).toHaveAttribute('id');
});

it('should not add label props when `titleText` is an element', () => {
render(<Dropdown {...mockProps} titleText={<span>Dropdown Title</span>} />);

const label = screen.getByText('Dropdown Title').closest('label');

expect(label).not.toHaveAttribute('id');
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, { useEffect, useRef, useState } from 'react';
import { View, FolderOpen, Folders, Information } from '@carbon/icons-react';
import { action } from '@storybook/addon-actions';

import { WithLayer } from '../../../.storybook/templates/WithLayer';
Expand All @@ -17,7 +18,14 @@ import Button from '../Button';
import ButtonSet from '../ButtonSet';
import { AILabel, AILabelContent, AILabelActions } from '../AILabel';
import { IconButton } from '../IconButton';
import { View, FolderOpen, Folders } from '@carbon/icons-react';
import {
Toggletip,
ToggletipActions,
ToggletipButton,
ToggletipContent,
ToggletipLabel,
} from '../Toggletip';
import Link from '../Link';

export default {
title: 'Components/MultiSelect',
Expand Down Expand Up @@ -595,3 +603,40 @@ export const ExperimentalAutoAlign = (args) => {
};

ExperimentalAutoAlign.argTypes = { ...sharedArgTypes };

export const withToggletipLabel = (args) => {
return (
<div>
<MultiSelect
label="Multiselect Label"
id="carbon-multiselect-example"
titleText={
<div style={{ display: 'flex', alignItems: 'center' }}>
<ToggletipLabel>Multiselect title</ToggletipLabel>
<Toggletip>
<ToggletipButton label="Show information">
<Information />
</ToggletipButton>
<ToggletipContent>
<p>
Lorem ipsum dolor sit amet, di os consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut fsil labore et dolore
magna aliqua.
</p>
<ToggletipActions>
<Link href="#">Link action</Link>
<Button size="sm">Button</Button>
</ToggletipActions>
</ToggletipContent>
</Toggletip>
</div>
}
helperText="This is helper text"
items={items}
itemToString={(item) => (item ? item.text : '')}
selectionFeedback="top-after-reopen"
{...args}
/>
</div>
);
};
5 changes: 4 additions & 1 deletion packages/react/src/components/MultiSelect/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import React, {
useMemo,
ReactNode,
useLayoutEffect,
isValidElement,
} from 'react';
import ListBox, {
ListBoxSize,
Expand Down Expand Up @@ -729,9 +730,11 @@ const MultiSelect = React.forwardRef(
[enableFloatingStyles, getMenuProps, refs.setFloating]
);

const labelProps = !isValidElement(titleText) ? getLabelProps() : null;

return (
<div className={wrapperClasses}>
<label className={titleClasses} {...getLabelProps()}>
<label className={titleClasses} {...labelProps}>
{titleText && titleText}
{selectedItems.length > 0 && (
<span className={`${prefix}--visually-hidden`}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
Expand All @@ -24,9 +24,18 @@ import userEvent from '@testing-library/user-event';

const prefix = 'cds';
const waitForPosition = () => act(async () => {});

describe('MultiSelect', () => {
let mockProps;
beforeEach(() => {
jest.mock('../../../internal/deprecateFieldOnObject');
mockProps = {
id: 'test-multiselect',
initialSelectedItems: [],
items: generateItems(5, generateGenericItem),
label: 'Test label',
onChange: jest.fn(),
};
});

describe.skip('automated accessibility tests', () => {
Expand Down Expand Up @@ -925,4 +934,22 @@ describe('MultiSelect', () => {
expect(result).toBe('');
expect(mockItemToString).not.toHaveBeenCalled();
});

it('should add label props when `titleText` is a string', () => {
render(<MultiSelect {...mockProps} titleText="MultiSelect Title" />);

const label = screen.getByText('MultiSelect Title').closest('label');

expect(label).toHaveAttribute('id');
});

it('should not add label props when `titleText` is an element', () => {
render(
<MultiSelect {...mockProps} titleText={<span>MultiSelect Title</span>} />
);

const label = screen.getByText('MultiSelect Title').closest('label');

expect(label).not.toHaveAttribute('id');
});
});
Loading