From e618756dca960f07d29492f46c15b0812246ca80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9D=91=BE=F0=9D=92=96=F0=9D=92=99=F0=9D=92=89?= Date: Thu, 15 May 2025 15:54:56 +0800 Subject: [PATCH] fix: simplify input props handling and improve accessibility features (#1150) * fix: simplify input props handling and improve accessibility features * chore: add unit test (cherry picked from commit e6a3ca7f918495a3fdc8f7866c5d8e07941a5799) # Conflicts: # src/Selector/Input.tsx --- src/Selector/Input.tsx | 75 ++++++------------------------------------ tests/Select.test.tsx | 9 +++++ 2 files changed, 19 insertions(+), 65 deletions(-) diff --git a/src/Selector/Input.tsx b/src/Selector/Input.tsx index b5c4ed079..39a7bb58d 100644 --- a/src/Selector/Input.tsx +++ b/src/Selector/Input.tsx @@ -2,7 +2,9 @@ import * as React from 'react'; import classNames from 'classnames'; import { composeRef } from '@rc-component/util/lib/ref'; import { warning } from '@rc-component/util/lib/warning'; +import composeProps from '@rc-component/util/lib/composeProps'; import useBaseProps from '../hooks/useBaseProps'; + type InputRef = HTMLInputElement | HTMLTextAreaElement; interface InputProps { @@ -39,23 +41,13 @@ const Input: React.ForwardRefRenderFunction = (props, ref) prefixCls, id, inputElement, - disabled, - tabIndex, - autoFocus, autoComplete, editable, activeDescendantId, value, - maxLength, - onKeyDown, - onMouseDown, - onChange, - onPaste, - onCompositionStart, - onCompositionEnd, - onBlur, open, attrs, + ...restProps } = props; const { classNames: contextClassNames, styles: contextStyles } = useBaseProps() || {}; @@ -64,16 +56,6 @@ const Input: React.ForwardRefRenderFunction = (props, ref) 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.`, @@ -81,18 +63,16 @@ const Input: React.ForwardRefRenderFunction = (props, ref) 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, + originProps.className, contextClassNames?.input, ), @@ -105,48 +85,13 @@ const Input: React.ForwardRefRenderFunction = (props, ref) 'aria-activedescendant': open ? activeDescendantId : undefined, ...attrs, value: editable ? value : '', - maxLength, readOnly: !editable, unselectable: !editable ? 'on' : null, - style: { ...style, opacity: editable ? null : 0, ...contextStyles?.input }, - - onKeyDown: (event: React.KeyboardEvent) => { - onKeyDown(event); - if (onOriginKeyDown) { - onOriginKeyDown(event); - } - }, - onMouseDown: (event: React.MouseEvent) => { - onMouseDown(event); - if (onOriginMouseDown) { - onOriginMouseDown(event); - } - }, - onChange: (event: React.ChangeEvent) => { - onChange(event); - if (onOriginChange) { - onOriginChange(event); - } - }, - onCompositionStart(event: React.CompositionEvent) { - onCompositionStart(event); - if (onOriginCompositionStart) { - onOriginCompositionStart(event); - } - }, - onCompositionEnd(event: React.CompositionEvent) { - onCompositionEnd(event); - if (onOriginCompositionEnd) { - onOriginCompositionEnd(event); - } - }, - onPaste, - onBlur(event: React.FocusEvent) { - onBlur(event); - if (onOriginBlur) { - onOriginBlur(event); - } + style: { + ...originProps.style, + opacity: editable ? null : 0, + ...contextStyles?.input, }, }); diff --git a/tests/Select.test.tsx b/tests/Select.test.tsx index c39c271c0..4f3bd2f2a 100644 --- a/tests/Select.test.tsx +++ b/tests/Select.test.tsx @@ -924,6 +924,7 @@ describe('Select.Basic', () => { const onCompositionEnd = jest.fn(); const textareaRef = jest.fn(); const mouseDownPreventDefault = jest.fn(); + const onPaste = jest.fn(); const { container } = render(