Skip to content

Commit 5dcb2c2

Browse files
authored
fix: simplify input props handling and improve accessibility features (#1150) (#1151)
* fix: simplify input props handling and improve accessibility features * chore: add unit test (cherry picked from commit e6a3ca7) # Conflicts: # src/Selector/Input.tsx
1 parent 5b94239 commit 5dcb2c2

File tree

2 files changed

+19
-65
lines changed

2 files changed

+19
-65
lines changed

src/Selector/Input.tsx

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import * as React from 'react';
22
import classNames from 'classnames';
33
import { composeRef } from '@rc-component/util/lib/ref';
44
import { warning } from '@rc-component/util/lib/warning';
5+
import composeProps from '@rc-component/util/lib/composeProps';
56
import useBaseProps from '../hooks/useBaseProps';
7+
68
type InputRef = HTMLInputElement | HTMLTextAreaElement;
79

810
interface InputProps {
@@ -39,23 +41,13 @@ const Input: React.ForwardRefRenderFunction<InputRef, InputProps> = (props, ref)
3941
prefixCls,
4042
id,
4143
inputElement,
42-
disabled,
43-
tabIndex,
44-
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
const { classNames: contextClassNames, styles: contextStyles } = useBaseProps() || {};
@@ -64,35 +56,23 @@ const Input: React.ForwardRefRenderFunction<InputRef, InputProps> = (props, ref)
6456

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

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

8264
inputNode = React.cloneElement(inputNode, {
8365
type: 'search',
84-
...originProps,
66+
...composeProps(restProps, originProps, true),
67+
8568
// Override over origin props
8669
id,
8770
ref: composeRef(ref, originRef as any),
88-
disabled,
89-
tabIndex,
9071
autoComplete: autoComplete || 'off',
9172

92-
autoFocus,
9373
className: classNames(
9474
`${prefixCls}-selection-search-input`,
95-
inputNode?.props?.className,
75+
originProps.className,
9676
contextClassNames?.input,
9777
),
9878

@@ -105,48 +85,13 @@ const Input: React.ForwardRefRenderFunction<InputRef, InputProps> = (props, ref)
10585
'aria-activedescendant': open ? activeDescendantId : undefined,
10686
...attrs,
10787
value: editable ? value : '',
108-
maxLength,
10988
readOnly: !editable,
11089
unselectable: !editable ? 'on' : null,
11190

112-
style: { ...style, opacity: editable ? null : 0, ...contextStyles?.input },
113-
114-
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => {
115-
onKeyDown(event);
116-
if (onOriginKeyDown) {
117-
onOriginKeyDown(event);
118-
}
119-
},
120-
onMouseDown: (event: React.MouseEvent<HTMLElement>) => {
121-
onMouseDown(event);
122-
if (onOriginMouseDown) {
123-
onOriginMouseDown(event);
124-
}
125-
},
126-
onChange: (event: React.ChangeEvent<HTMLElement>) => {
127-
onChange(event);
128-
if (onOriginChange) {
129-
onOriginChange(event);
130-
}
131-
},
132-
onCompositionStart(event: React.CompositionEvent<HTMLElement>) {
133-
onCompositionStart(event);
134-
if (onOriginCompositionStart) {
135-
onOriginCompositionStart(event);
136-
}
137-
},
138-
onCompositionEnd(event: React.CompositionEvent<HTMLElement>) {
139-
onCompositionEnd(event);
140-
if (onOriginCompositionEnd) {
141-
onOriginCompositionEnd(event);
142-
}
143-
},
144-
onPaste,
145-
onBlur(event: React.FocusEvent<HTMLElement>) {
146-
onBlur(event);
147-
if (onOriginBlur) {
148-
onOriginBlur(event);
149-
}
91+
style: {
92+
...originProps.style,
93+
opacity: editable ? null : 0,
94+
...contextStyles?.input,
15095
},
15196
});
15297

tests/Select.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ describe('Select.Basic', () => {
924924
const onCompositionEnd = jest.fn();
925925
const textareaRef = jest.fn();
926926
const mouseDownPreventDefault = jest.fn();
927+
const onPaste = jest.fn();
927928
const { container } = render(
928929
<Select
929930
mode="combobox"
@@ -934,6 +935,7 @@ describe('Select.Basic', () => {
934935
onMouseDown={onMouseDown}
935936
onCompositionStart={onCompositionStart}
936937
onCompositionEnd={onCompositionEnd}
938+
onPaste={onPaste}
937939
ref={textareaRef}
938940
className="custom-input"
939941
/>
@@ -965,6 +967,13 @@ describe('Select.Basic', () => {
965967
expect(textareaRef).toHaveBeenCalled();
966968
expect(onCompositionStart).toHaveBeenCalled();
967969
expect(onCompositionEnd).toHaveBeenCalled();
970+
971+
fireEvent.paste(textareaEle, {
972+
target: { value: 'hi' },
973+
clipboardData: { getData: () => 'hi' },
974+
});
975+
expect(onPaste).toHaveBeenCalled();
976+
expect(textareaEle.value).toEqual('hi');
968977
});
969978

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

0 commit comments

Comments
 (0)