Skip to content

fix: simplify input props handling and improve accessibility features #1150

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

Merged
merged 2 commits into from
May 15, 2025
Merged
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
69 changes: 5 additions & 64 deletions src/Selector/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import classNames from 'classnames';
import { composeRef } from 'rc-util/lib/ref';
import { warning } from 'rc-util/lib/warning';
import composeProps from 'rc-util/lib/composeProps';

type InputRef = HTMLInputElement | HTMLTextAreaElement;

Expand Down Expand Up @@ -39,57 +40,36 @@ const Input: React.ForwardRefRenderFunction<InputRef, InputProps> = (props, ref)
prefixCls,
id,
inputElement,
disabled,
tabIndex,
autoFocus,
autoComplete,
editable,
activeDescendantId,
value,
maxLength,
onKeyDown,
onMouseDown,
onChange,
onPaste,
onCompositionStart,
onCompositionEnd,
onBlur,
open,
attrs,
...restProps
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

故意不用 rest 的,因为复合组件的 API 不一定和原生相同。这样带过去以后就收不回来了

} = props;

let inputNode: React.ComponentElement<any, any> = inputElement || <input />;

const { ref: originRef, props: originProps } = inputNode;

const {
onKeyDown: onOriginKeyDown,
onChange: onOriginChange,
onMouseDown: onOriginMouseDown,
onCompositionStart: onOriginCompositionStart,
onCompositionEnd: onOriginCompositionEnd,
onBlur: onOriginBlur,
style,
} = originProps;

warning(
!('maxLength' in inputNode.props),
`Passing 'maxLength' to input element directly may not work because input in BaseSelect is controlled.`,
);

inputNode = React.cloneElement(inputNode, {
type: 'search',
...originProps,
...composeProps(restProps, originProps, true),

// Override over origin props
id,
ref: composeRef(ref, originRef as any),
disabled,
tabIndex,
autoComplete: autoComplete || 'off',

autoFocus,
className: classNames(`${prefixCls}-selection-search-input`, inputNode?.props?.className),
className: classNames(`${prefixCls}-selection-search-input`, originProps?.className),

role: 'combobox',
'aria-expanded': open || false,
Expand All @@ -100,49 +80,10 @@ const Input: React.ForwardRefRenderFunction<InputRef, InputProps> = (props, ref)
'aria-activedescendant': open ? activeDescendantId : undefined,
...attrs,
value: editable ? value : '',
maxLength,
readOnly: !editable,
unselectable: !editable ? 'on' : null,

style: { ...style, opacity: editable ? null : 0 },

onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => {
onKeyDown(event);
if (onOriginKeyDown) {
onOriginKeyDown(event);
}
},
onMouseDown: (event: React.MouseEvent<HTMLElement>) => {
onMouseDown(event);
if (onOriginMouseDown) {
onOriginMouseDown(event);
}
},
onChange: (event: React.ChangeEvent<HTMLElement>) => {
onChange(event);
if (onOriginChange) {
onOriginChange(event);
}
},
onCompositionStart(event: React.CompositionEvent<HTMLElement>) {
onCompositionStart(event);
if (onOriginCompositionStart) {
onOriginCompositionStart(event);
}
},
onCompositionEnd(event: React.CompositionEvent<HTMLElement>) {
onCompositionEnd(event);
if (onOriginCompositionEnd) {
onOriginCompositionEnd(event);
}
},
onPaste,
onBlur(event: React.FocusEvent<HTMLElement>) {
onBlur(event);
if (onOriginBlur) {
onOriginBlur(event);
}
},
style: { ...originProps.style, opacity: editable ? null : 0 },
});

return inputNode;
Expand Down
9 changes: 9 additions & 0 deletions tests/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ describe('Select.Basic', () => {
const onCompositionEnd = jest.fn();
const textareaRef = jest.fn();
const mouseDownPreventDefault = jest.fn();
const onPaste = jest.fn();
const { container } = render(
<Select
mode="combobox"
Expand All @@ -933,6 +934,7 @@ describe('Select.Basic', () => {
onMouseDown={onMouseDown}
onCompositionStart={onCompositionStart}
onCompositionEnd={onCompositionEnd}
onPaste={onPaste}
ref={textareaRef}
className="custom-input"
/>
Expand Down Expand Up @@ -964,6 +966,13 @@ describe('Select.Basic', () => {
expect(textareaRef).toHaveBeenCalled();
expect(onCompositionStart).toHaveBeenCalled();
expect(onCompositionEnd).toHaveBeenCalled();

fireEvent.paste(textareaEle, {
target: { value: 'hi' },
clipboardData: { getData: () => 'hi' },
});
expect(onPaste).toHaveBeenCalled();
expect(textareaEle.value).toEqual('hi');
});

it('not override customize props', () => {
Expand Down