Skip to content

Commit 90d2614

Browse files
Tatsiana KostsikavaTatsiana Kostsikava
authored andcommitted
add data attribute to react-select
1 parent 052e864 commit 90d2614

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

.changeset/strange-grapes-mix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-select': minor
3+
---
4+
5+
Add `dataAttributes` prop to allow passing data attributes (e.g., `data-testid`) to the select container

docs/examples/BasicMulti.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ export default () => (
1111
options={colourOptions}
1212
className="basic-multi-select"
1313
classNamePrefix="select"
14+
dataAttributes={{
15+
'data-testid': 'basic-multi-select'
16+
}}
1417
/>
1518
);

docs/examples/BasicSingle.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export default () => {
3030
isSearchable={isSearchable}
3131
name="color"
3232
options={colourOptions}
33+
dataAttributes={{
34+
'data-testid': 'basic-single-select'
35+
}}
3336
/>
3437

3538
<div

packages/react-select/src/Select.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ export interface Props<
277277
form?: string;
278278
/** Marks the value-holding input as required for form validation */
279279
required?: boolean;
280+
/** Data attributes to be applied to the select container */
281+
dataAttributes?: Record<string, string>;
280282
}
281283

282284
export const defaultProps = {
@@ -2202,7 +2204,7 @@ export default class Select<
22022204
const { Control, IndicatorsContainer, SelectContainer, ValueContainer } =
22032205
this.getComponents();
22042206

2205-
const { className, id, isDisabled, menuIsOpen } = this.props;
2207+
const { className, id, isDisabled, menuIsOpen, dataAttributes } = this.props;
22062208
const { isFocused } = this.state;
22072209
const commonProps = (this.commonProps = this.getCommonProps());
22082210

@@ -2213,6 +2215,7 @@ export default class Select<
22132215
innerProps={{
22142216
id: id,
22152217
onKeyDown: this.onKeyDown,
2218+
...dataAttributes,
22162219
}}
22172220
isDisabled={isDisabled}
22182221
isFocused={isFocused}
@@ -2251,4 +2254,7 @@ export type PublicBaseSelectProps<
22512254
Option,
22522255
IsMulti extends boolean,
22532256
Group extends GroupBase<Option>
2254-
> = JSX.LibraryManagedAttributes<typeof Select, Props<Option, IsMulti, Group>>;
2257+
> = JSX.LibraryManagedAttributes<typeof Select, Props<Option, IsMulti, Group>> & {
2258+
/** Data attributes to be applied to the select container */
2259+
dataAttributes?: Record<string, string>;
2260+
};

packages/react-select/src/__tests__/Select.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,27 @@ cases(
23182318
}
23192319
);
23202320

2321+
cases(
2322+
'accessibility > passes through dataAttributes prop',
2323+
({ props = { ...BASIC_PROPS, dataAttributes: { 'data-testid': 'test-select' } } }) => {
2324+
let { container } = render(<Select {...props} />);
2325+
// The data attributes should be on the outermost div (SelectContainer)
2326+
const selectContainer = container.querySelector('div[data-testid]');
2327+
expect(selectContainer).toBeTruthy();
2328+
expect(selectContainer!.getAttribute('data-testid')).toBe('test-select');
2329+
},
2330+
{
2331+
'single select > should pass dataAttributes prop down to container': {},
2332+
'multi select > should pass dataAttributes prop down to container': {
2333+
props: {
2334+
...BASIC_PROPS,
2335+
dataAttributes: { 'data-testid': 'test-select' },
2336+
isMulti: true,
2337+
},
2338+
},
2339+
}
2340+
);
2341+
23212342
test('accessibility > to show the number of options available in A11yText when the menu is Open', () => {
23222343
let { container, rerender } = render(
23232344
<Select {...BASIC_PROPS} inputValue={''} autoFocus menuIsOpen />

0 commit comments

Comments
 (0)