Skip to content

Commit 820539a

Browse files
committed
fix: prevent aria props from being passed to wrapper div
1 parent 73d9975 commit 820539a

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

src/BaseSelect/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ export interface BaseSelectPrivateProps {
129129

130130
export type BaseSelectPropsWithoutPrivate = Omit<BaseSelectProps, keyof BaseSelectPrivateProps>;
131131

132-
export interface BaseSelectProps extends BaseSelectPrivateProps, React.AriaAttributes {
132+
export interface BaseSelectProps
133+
extends
134+
BaseSelectPrivateProps,
135+
React.AriaAttributes,
136+
Pick<React.HTMLAttributes<HTMLElement>, 'role'> {
133137
// Style
134138
className?: string;
135139
style?: React.CSSProperties;

src/SelectInput/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { clsx } from 'clsx';
1111
import type { ComponentsConfig } from '../hooks/useComponents';
1212
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
1313
import { composeRef } from '@rc-component/util/lib/ref';
14+
import pickAttrs from '@rc-component/util/lib/pickAttrs';
1415

1516
export interface SelectInputRef {
1617
focus: (options?: FocusOptions) => void;
@@ -207,6 +208,8 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
207208

208209
// ===================== Render =====================
209210
const domProps = omit(restProps, DEFAULT_OMIT_PROPS as any);
211+
const ariaProps = pickAttrs(domProps, { aria: true });
212+
const ariaKeys = Object.keys(ariaProps) as (keyof typeof domProps)[];
210213

211214
// Create context value with wrapped callbacks
212215
const contextValue = {
@@ -228,7 +231,7 @@ export default React.forwardRef<SelectInputRef, SelectInputProps>(function Selec
228231
return (
229232
<SelectInputContext.Provider value={contextValue}>
230233
<div
231-
{...domProps}
234+
{...omit(domProps, ariaKeys)}
232235
// Style
233236
ref={rootRef}
234237
className={className}

src/hooks/useBaseProps.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export interface BaseSelectContextProps extends BaseSelectProps {
1010
triggerOpen: boolean;
1111
multiple: boolean;
1212
toggleOpen: (open?: boolean) => void;
13-
role?: React.AriaRole;
1413
}
1514

1615
export const BaseSelectContext = React.createContext<BaseSelectContextProps>(null);

tests/Accessibility.test.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,34 @@ describe('Select.Accessibility', () => {
213213
}
214214
});
215215
});
216+
217+
it('should pass aria and role attributes to Input component', async () => {
218+
const { container } = render(
219+
<Select
220+
role="group"
221+
aria-required="true"
222+
options={[
223+
{
224+
value: '123',
225+
},
226+
{
227+
value: '1234',
228+
},
229+
{
230+
value: '12345',
231+
},
232+
]}
233+
/>,
234+
);
235+
236+
const wrapper = container.querySelector('.rc-select');
237+
expect(wrapper).not.toHaveAttribute('role');
238+
expect(wrapper).not.toHaveAttribute('aria-required');
239+
240+
const input = container.querySelector('input');
241+
expect(input).toHaveAttribute('role', 'group');
242+
expect(input).toHaveAttribute('aria-required', 'true');
243+
});
216244
});
217245

218246
describe('Select Input attributes', () => {

tests/Select.test.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,11 @@ describe('Select.Basic', () => {
136136
expect(container.firstChild).toMatchSnapshot();
137137
});
138138

139-
// [Legacy] Should not use `role` since it's meaningless
140139
it('renders role prop correctly', () => {
141140
const { container } = render(
142141
genSelect({
143142
role: 'button',
144-
} as any),
143+
}),
145144
);
146145
expect(container.firstChild).toMatchSnapshot();
147146
});

tests/__snapshots__/Select.test.tsx.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ exports[`Select.Basic filterOption could be true as described in default value 1
108108

109109
exports[`Select.Basic render renders aria-attributes correctly 1`] = `
110110
<div
111-
aria-label="some-label"
112-
aria-labelledby="test-id"
113111
class="antd select-test antd-single antd-allow-clear antd-show-search"
114112
>
115113
<div
@@ -249,7 +247,6 @@ exports[`Select.Basic render renders dropdown correctly 1`] = `null`;
249247
exports[`Select.Basic render renders role prop correctly 1`] = `
250248
<div
251249
class="antd select-test antd-single antd-allow-clear antd-show-search"
252-
role="button"
253250
>
254251
<div
255252
class="antd-content"

0 commit comments

Comments
 (0)