Skip to content

Commit 03becc6

Browse files
committed
fix(Dropdown): resolve DAP violations (#4260)
1 parent 5f954e9 commit 03becc6

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

packages/react/src/components/Dropdown/Dropdown-story.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const itemToElement = item => {
6060
return (
6161
<div>
6262
<span>{itemAsArray[0]}</span>
63-
<span style={{ color: 'red' }}> {itemAsArray[1]}</span>
63+
<span style={{ color: 'blue' }}> {itemAsArray[1]}</span>
6464
</div>
6565
);
6666
};

packages/react/src/components/Dropdown/Dropdown.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { settings } from 'carbon-components';
1313
import { WarningFilled16 } from '@carbon/icons-react';
1414
import ListBox, { PropTypes as ListBoxPropTypes } from '../ListBox';
1515
import { match, keys } from '../../internal/keyboard';
16+
import setupGetInstanceId from '../../tools/setupGetInstanceId';
1617

1718
const { prefix } = settings;
1819

@@ -24,6 +25,8 @@ const defaultItemToString = item => {
2425
return item ? item.label : '';
2526
};
2627

28+
const getInstanceId = setupGetInstanceId();
29+
2730
export default class Dropdown extends React.Component {
2831
static propTypes = {
2932
/**
@@ -144,6 +147,10 @@ export default class Dropdown extends React.Component {
144147
helperText: '',
145148
};
146149

150+
constructor(props) {
151+
super(props);
152+
this.dropdownInstanceId = getInstanceId();
153+
}
147154
handleOnChange = selectedItem => {
148155
if (this.props.onChange) {
149156
this.props.onChange({ selectedItem });
@@ -183,8 +190,11 @@ export default class Dropdown extends React.Component {
183190
const titleClasses = cx(`${prefix}--label`, {
184191
[`${prefix}--label--disabled`]: disabled,
185192
});
193+
194+
const dropdownId = `dropdown-${this.dropdownInstanceId}`;
195+
186196
const title = titleText ? (
187-
<label htmlFor={id} className={titleClasses}>
197+
<label htmlFor={dropdownId} className={titleClasses}>
188198
{titleText}
189199
</label>
190200
) : null;
@@ -229,8 +239,9 @@ export default class Dropdown extends React.Component {
229239
toggleMenu,
230240
}) => (
231241
<ListBox
232-
id={id}
233242
type={type}
243+
id={dropdownId}
244+
aria-label={ariaLabel}
234245
className={className({ isOpen })}
235246
disabled={disabled}
236247
isOpen={isOpen}
@@ -268,7 +279,7 @@ export default class Dropdown extends React.Component {
268279
/>
269280
</ListBox.Field>
270281
{isOpen && (
271-
<ListBox.Menu aria-label={ariaLabel} id={id}>
282+
<ListBox.Menu aria-labelledby={dropdownId} id={id}>
272283
{items.map((item, index) => (
273284
<ListBox.MenuItem
274285
key={itemToString(item)}

packages/react/src/components/Dropdown/__snapshots__/Dropdown-test.js.snap

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ exports[`Dropdown should render 1`] = `
6767
<ListBox
6868
className="bx--dropdown"
6969
disabled={false}
70-
id="test-dropdown"
70+
id="dropdown-1"
7171
innerRef={[Function]}
7272
isOpen={false}
7373
light={false}
7474
type="default"
7575
>
7676
<div
7777
className="bx--dropdown bx--list-box"
78-
id="test-dropdown"
78+
id="dropdown-1"
7979
onClick={[Function]}
8080
onKeyDown={[Function]}
8181
role="listbox"
@@ -242,15 +242,15 @@ exports[`Dropdown should render custom item components 1`] = `
242242
<ListBox
243243
className="bx--dropdown bx--dropdown--open"
244244
disabled={false}
245-
id="test-dropdown"
245+
id="dropdown-5"
246246
innerRef={[Function]}
247247
isOpen={true}
248248
light={false}
249249
type="default"
250250
>
251251
<div
252252
className="bx--dropdown bx--dropdown--open bx--list-box bx--list-box--expanded"
253-
id="test-dropdown"
253+
id="dropdown-5"
254254
onClick={[Function]}
255255
onKeyDown={[Function]}
256256
role="listbox"
@@ -344,9 +344,11 @@ exports[`Dropdown should render custom item components 1`] = `
344344
</div>
345345
</ListBoxField>
346346
<ListBoxMenu
347+
aria-labelledby="dropdown-5"
347348
id="test-dropdown"
348349
>
349350
<div
351+
aria-labelledby="dropdown-5"
350352
className="bx--list-box__menu"
351353
id="test-dropdown__menu"
352354
role="listbox"
@@ -574,15 +576,15 @@ exports[`Dropdown should render with strings as items 1`] = `
574576
<ListBox
575577
className="bx--dropdown bx--dropdown--open"
576578
disabled={false}
577-
id="test-dropdown"
579+
id="dropdown-4"
578580
innerRef={[Function]}
579581
isOpen={true}
580582
light={false}
581583
type="default"
582584
>
583585
<div
584586
className="bx--dropdown bx--dropdown--open bx--list-box bx--list-box--expanded"
585-
id="test-dropdown"
587+
id="dropdown-4"
586588
onClick={[Function]}
587589
onKeyDown={[Function]}
588590
role="listbox"
@@ -676,9 +678,11 @@ exports[`Dropdown should render with strings as items 1`] = `
676678
</div>
677679
</ListBoxField>
678680
<ListBoxMenu
681+
aria-labelledby="dropdown-4"
679682
id="test-dropdown"
680683
>
681684
<div
685+
aria-labelledby="dropdown-4"
682686
className="bx--list-box__menu"
683687
id="test-dropdown__menu"
684688
role="listbox"

0 commit comments

Comments
 (0)