Skip to content

Commit 7d22832

Browse files
authored
chore: Pass customize aria data (#493)
* chore: Pass customize aria data * update test
1 parent 55c13e0 commit 7d22832

File tree

5 files changed

+96
-53
lines changed

5 files changed

+96
-53
lines changed

src/Selector/Input.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ interface InputProps {
1515
value: string;
1616
open: boolean;
1717
tabIndex: number;
18+
/** Pass accessibility props to input */
19+
attrs: object;
1820

1921
onKeyDown: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
2022
onMouseDown: React.MouseEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
@@ -39,6 +41,7 @@ const Input: React.RefForwardingComponent<InputRef, InputProps> = (
3941
onChange,
4042
onPaste,
4143
open,
44+
attrs,
4245
},
4346
ref,
4447
) => {
@@ -70,6 +73,7 @@ const Input: React.RefForwardingComponent<InputRef, InputProps> = (
7073
'aria-autocomplete': 'list',
7174
'aria-controls': `${id}_list`,
7275
'aria-activedescendant': `${id}_list_${accessibilityIndex}`,
76+
...attrs,
7377
value: editable ? value : '',
7478
readOnly: !editable,
7579
unselectable: !editable ? 'on' : null,

src/Selector/MultipleSelector.tsx

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import classNames from 'classnames';
3+
import pickAttrs from 'rc-util/lib/pickAttrs';
34
import CSSMotionList from 'rc-animate/lib/CSSMotionList';
45
import TransBtn from '../TransBtn';
56
import { LabelValueType, RawValueType, CustomTagProps } from '../interface/generator';
@@ -28,37 +29,39 @@ interface SelectorProps extends InnerSelectorProps {
2829
onSelect: (value: RawValueType, option: { selected: boolean }) => void;
2930
}
3031

31-
const SelectSelector: React.FC<SelectorProps> = ({
32-
id,
33-
prefixCls,
34-
35-
values,
36-
open,
37-
searchValue,
38-
inputRef,
39-
placeholder,
40-
disabled,
41-
mode,
42-
showSearch,
43-
autoFocus,
44-
autoComplete,
45-
accessibilityIndex,
46-
tabIndex,
47-
48-
removeIcon,
49-
choiceTransitionName,
50-
51-
maxTagCount,
52-
maxTagTextLength,
53-
maxTagPlaceholder = (omittedValues: LabelValueType[]) => `+ ${omittedValues.length} ...`,
54-
tagRender,
55-
56-
onSelect,
57-
onInputChange,
58-
onInputPaste,
59-
onInputKeyDown,
60-
onInputMouseDown,
61-
}) => {
32+
const SelectSelector: React.FC<SelectorProps> = props => {
33+
const {
34+
id,
35+
prefixCls,
36+
37+
values,
38+
open,
39+
searchValue,
40+
inputRef,
41+
placeholder,
42+
disabled,
43+
mode,
44+
showSearch,
45+
autoFocus,
46+
autoComplete,
47+
accessibilityIndex,
48+
tabIndex,
49+
50+
removeIcon,
51+
choiceTransitionName,
52+
53+
maxTagCount,
54+
maxTagTextLength,
55+
maxTagPlaceholder = (omittedValues: LabelValueType[]) => `+ ${omittedValues.length} ...`,
56+
tagRender,
57+
58+
onSelect,
59+
onInputChange,
60+
onInputPaste,
61+
onInputKeyDown,
62+
onInputMouseDown,
63+
} = props;
64+
6265
const [motionAppear, setMotionAppear] = React.useState(false);
6366
const measureRef = React.useRef<HTMLSpanElement>(null);
6467
const [inputWidth, setInputWidth] = React.useState(0);
@@ -194,6 +197,7 @@ const SelectSelector: React.FC<SelectorProps> = ({
194197
onChange={onInputChange}
195198
onPaste={onInputPaste}
196199
tabIndex={tabIndex}
200+
attrs={pickAttrs(props, true)}
197201
/>
198202

199203
{/* Measure Node */}

src/Selector/SingleSelector.tsx

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import pickAttrs from 'rc-util/lib/pickAttrs';
23
import Input from './Input';
34
import { InnerSelectorProps } from '.';
45

@@ -8,30 +9,32 @@ interface SelectorProps extends InnerSelectorProps {
89
backfill?: boolean;
910
}
1011

11-
const SingleSelector: React.FC<SelectorProps> = ({
12-
inputElement,
13-
prefixCls,
14-
id,
15-
inputRef,
16-
disabled,
17-
autoFocus,
18-
autoComplete,
19-
accessibilityIndex,
20-
mode,
21-
open,
22-
values,
23-
placeholder,
24-
tabIndex,
12+
const SingleSelector: React.FC<SelectorProps> = props => {
13+
const {
14+
inputElement,
15+
prefixCls,
16+
id,
17+
inputRef,
18+
disabled,
19+
autoFocus,
20+
autoComplete,
21+
accessibilityIndex,
22+
mode,
23+
open,
24+
values,
25+
placeholder,
26+
tabIndex,
2527

26-
showSearch,
27-
searchValue,
28-
activeValue,
28+
showSearch,
29+
searchValue,
30+
activeValue,
31+
32+
onInputKeyDown,
33+
onInputMouseDown,
34+
onInputChange,
35+
onInputPaste,
36+
} = props;
2937

30-
onInputKeyDown,
31-
onInputMouseDown,
32-
onInputChange,
33-
onInputPaste,
34-
}) => {
3538
const combobox = mode === 'combobox';
3639
const inputEditable = combobox || (showSearch && open);
3740
const item = values[0];
@@ -65,6 +68,7 @@ const SingleSelector: React.FC<SelectorProps> = ({
6568
onChange={onInputChange}
6669
onPaste={onInputPaste}
6770
tabIndex={tabIndex}
71+
attrs={pickAttrs(props, true)}
6872
/>
6973
</span>
7074

tests/Accessibility.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { mount } from 'enzyme';
2+
import * as React from 'react';
3+
import Select from '../src';
4+
import { injectRunAllTimers } from './utils/common';
5+
6+
describe('Select.Accessibility', () => {
7+
injectRunAllTimers(jest);
8+
9+
beforeEach(() => {
10+
jest.useFakeTimers();
11+
});
12+
13+
afterEach(() => {
14+
jest.useRealTimers();
15+
});
16+
17+
it('pass aria info to internal input', () => {
18+
const MySelect = Select as any;
19+
const wrapper = mount(<MySelect aria-label="light" data-attr="bamboo" useless="2333" />);
20+
expect(wrapper.find('input').props()).toEqual(
21+
expect.objectContaining({
22+
'aria-label': 'light',
23+
'data-attr': 'bamboo',
24+
}),
25+
);
26+
});
27+
});

tests/__snapshots__/Select.test.tsx.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ exports[`Select.Basic render renders aria-attributes correctly 1`] = `
238238
aria-autocomplete="list"
239239
aria-controls="undefined_list"
240240
aria-haspopup="listbox"
241+
aria-label="some-label"
242+
aria-labelledby="test-id"
241243
aria-owns="undefined_list"
242244
autocomplete="off"
243245
class="antd-selection-search-input"
@@ -355,6 +357,8 @@ exports[`Select.Basic render renders data-attributes correctly 1`] = `
355357
aria-owns="undefined_list"
356358
autocomplete="off"
357359
class="antd-selection-search-input"
360+
data-id="12345"
361+
data-test="test-id"
358362
readonly=""
359363
role="combobox"
360364
style="opacity:0"

0 commit comments

Comments
 (0)