Skip to content

Commit e6a3ca7

Browse files
authored
fix: simplify input props handling and improve accessibility features (#1150)
* fix: simplify input props handling and improve accessibility features * chore: add unit test
1 parent b68a078 commit e6a3ca7

File tree

2 files changed

+14
-64
lines changed

2 files changed

+14
-64
lines changed

src/Selector/Input.tsx

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as React from 'react';
22
import classNames from 'classnames';
33
import { composeRef } from 'rc-util/lib/ref';
44
import { warning } from 'rc-util/lib/warning';
5+
import composeProps from 'rc-util/lib/composeProps';
56

67
type InputRef = HTMLInputElement | HTMLTextAreaElement;
78

@@ -39,57 +40,36 @@ const Input: React.ForwardRefRenderFunction<InputRef, InputProps> = (props, ref)
3940
prefixCls,
4041
id,
4142
inputElement,
42-
disabled,
43-
tabIndex,
4443
autoFocus,
4544
autoComplete,
4645
editable,
4746
activeDescendantId,
4847
value,
49-
maxLength,
50-
onKeyDown,
51-
onMouseDown,
52-
onChange,
53-
onPaste,
54-
onCompositionStart,
55-
onCompositionEnd,
56-
onBlur,
5748
open,
5849
attrs,
50+
...restProps
5951
} = props;
6052

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

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

65-
const {
66-
onKeyDown: onOriginKeyDown,
67-
onChange: onOriginChange,
68-
onMouseDown: onOriginMouseDown,
69-
onCompositionStart: onOriginCompositionStart,
70-
onCompositionEnd: onOriginCompositionEnd,
71-
onBlur: onOriginBlur,
72-
style,
73-
} = originProps;
74-
7557
warning(
7658
!('maxLength' in inputNode.props),
7759
`Passing 'maxLength' to input element directly may not work because input in BaseSelect is controlled.`,
7860
);
7961

8062
inputNode = React.cloneElement(inputNode, {
8163
type: 'search',
82-
...originProps,
64+
...composeProps(restProps, originProps, true),
8365

8466
// Override over origin props
8567
id,
8668
ref: composeRef(ref, originRef as any),
87-
disabled,
88-
tabIndex,
8969
autoComplete: autoComplete || 'off',
9070

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

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

107-
style: { ...style, opacity: editable ? null : 0 },
108-
109-
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => {
110-
onKeyDown(event);
111-
if (onOriginKeyDown) {
112-
onOriginKeyDown(event);
113-
}
114-
},
115-
onMouseDown: (event: React.MouseEvent<HTMLElement>) => {
116-
onMouseDown(event);
117-
if (onOriginMouseDown) {
118-
onOriginMouseDown(event);
119-
}
120-
},
121-
onChange: (event: React.ChangeEvent<HTMLElement>) => {
122-
onChange(event);
123-
if (onOriginChange) {
124-
onOriginChange(event);
125-
}
126-
},
127-
onCompositionStart(event: React.CompositionEvent<HTMLElement>) {
128-
onCompositionStart(event);
129-
if (onOriginCompositionStart) {
130-
onOriginCompositionStart(event);
131-
}
132-
},
133-
onCompositionEnd(event: React.CompositionEvent<HTMLElement>) {
134-
onCompositionEnd(event);
135-
if (onOriginCompositionEnd) {
136-
onOriginCompositionEnd(event);
137-
}
138-
},
139-
onPaste,
140-
onBlur(event: React.FocusEvent<HTMLElement>) {
141-
onBlur(event);
142-
if (onOriginBlur) {
143-
onOriginBlur(event);
144-
}
145-
},
86+
style: { ...originProps.style, opacity: editable ? null : 0 },
14687
});
14788

14889
return inputNode;

tests/Select.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ describe('Select.Basic', () => {
923923
const onCompositionEnd = jest.fn();
924924
const textareaRef = jest.fn();
925925
const mouseDownPreventDefault = jest.fn();
926+
const onPaste = jest.fn();
926927
const { container } = render(
927928
<Select
928929
mode="combobox"
@@ -933,6 +934,7 @@ describe('Select.Basic', () => {
933934
onMouseDown={onMouseDown}
934935
onCompositionStart={onCompositionStart}
935936
onCompositionEnd={onCompositionEnd}
937+
onPaste={onPaste}
936938
ref={textareaRef}
937939
className="custom-input"
938940
/>
@@ -964,6 +966,13 @@ describe('Select.Basic', () => {
964966
expect(textareaRef).toHaveBeenCalled();
965967
expect(onCompositionStart).toHaveBeenCalled();
966968
expect(onCompositionEnd).toHaveBeenCalled();
969+
970+
fireEvent.paste(textareaEle, {
971+
target: { value: 'hi' },
972+
clipboardData: { getData: () => 'hi' },
973+
});
974+
expect(onPaste).toHaveBeenCalled();
975+
expect(textareaEle.value).toEqual('hi');
967976
});
968977

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

0 commit comments

Comments
 (0)