Skip to content

Commit c5f512c

Browse files
authored
fix: clone input should not replace customize props (#722)
1 parent ca95af6 commit c5f512c

File tree

2 files changed

+76
-59
lines changed

2 files changed

+76
-59
lines changed

src/Selector/Input.tsx

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,16 @@ const Input: React.RefForwardingComponent<InputRef, InputProps> = (
5858
) => {
5959
let inputNode: React.ComponentElement<any, any> = inputElement || <input />;
6060

61+
const { ref: originRef, props: originProps } = inputNode;
62+
6163
const {
62-
ref: originRef,
63-
props: {
64-
onKeyDown: onOriginKeyDown,
65-
onChange: onOriginChange,
66-
onMouseDown: onOriginMouseDown,
67-
onCompositionStart: onOriginCompositionStart,
68-
onCompositionEnd: onOriginCompositionEnd,
69-
style,
70-
},
71-
} = inputNode;
64+
onKeyDown: onOriginKeyDown,
65+
onChange: onOriginChange,
66+
onMouseDown: onOriginMouseDown,
67+
onCompositionStart: onOriginCompositionStart,
68+
onCompositionEnd: onOriginCompositionEnd,
69+
style,
70+
} = originProps;
7271

7372
inputNode = React.cloneElement(inputNode, {
7473
id,
@@ -79,7 +78,7 @@ const Input: React.RefForwardingComponent<InputRef, InputProps> = (
7978
type: 'search',
8079
autoFocus,
8180
className: classNames(`${prefixCls}-selection-search-input`, inputNode?.props?.className),
82-
style: { ...style, opacity: editable ? null : 0 },
81+
8382
role: 'combobox',
8483
'aria-expanded': open,
8584
'aria-haspopup': 'listbox',
@@ -92,6 +91,12 @@ const Input: React.RefForwardingComponent<InputRef, InputProps> = (
9291
maxLength,
9392
readOnly: !editable,
9493
unselectable: !editable ? 'on' : null,
94+
95+
...originProps,
96+
97+
// Override over origin props
98+
style: { ...style, opacity: editable ? null : 0 },
99+
95100
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => {
96101
onKeyDown(event);
97102
if (onOriginKeyDown) {

tests/Select.test.tsx

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -827,55 +827,68 @@ describe('Select.Basic', () => {
827827
expect(focusSpy).toHaveBeenCalled();
828828
});
829829

830-
it('combobox could customize input element', () => {
831-
const onKeyDown = jest.fn();
832-
const onChange = jest.fn();
833-
const onMouseDown = jest.fn();
834-
const onCompositionStart = jest.fn();
835-
const onCompositionEnd = jest.fn();
836-
const textareaRef = jest.fn();
837-
const mouseDownPreventDefault = jest.fn();
838-
const wrapper = mount(
839-
<Select
840-
mode="combobox"
841-
getInputElement={() => (
842-
<textarea
843-
onKeyDown={onKeyDown}
844-
onChange={onChange}
845-
onMouseDown={onMouseDown}
846-
onCompositionStart={onCompositionStart}
847-
onCompositionEnd={onCompositionEnd}
848-
ref={textareaRef}
849-
className="custom-input"
850-
/>
851-
)}
852-
>
853-
<Option value="1">1</Option>
854-
<Option value="2">2</Option>
855-
</Select>,
856-
);
830+
describe('combobox could customize input element', () => {
831+
it('work', () => {
832+
const onKeyDown = jest.fn();
833+
const onChange = jest.fn();
834+
const onMouseDown = jest.fn();
835+
const onCompositionStart = jest.fn();
836+
const onCompositionEnd = jest.fn();
837+
const textareaRef = jest.fn();
838+
const mouseDownPreventDefault = jest.fn();
839+
const wrapper = mount(
840+
<Select
841+
mode="combobox"
842+
getInputElement={() => (
843+
<textarea
844+
onKeyDown={onKeyDown}
845+
onChange={onChange}
846+
onMouseDown={onMouseDown}
847+
onCompositionStart={onCompositionStart}
848+
onCompositionEnd={onCompositionEnd}
849+
ref={textareaRef}
850+
className="custom-input"
851+
/>
852+
)}
853+
>
854+
<Option value="1">1</Option>
855+
<Option value="2">2</Option>
856+
</Select>,
857+
);
857858

858-
expect(wrapper.find('textarea').length).toBe(1);
859-
toggleOpen(wrapper);
860-
wrapper
861-
.find('.rc-select')
862-
.find('textarea')
863-
.simulate('mouseDown', { preventDefault: mouseDownPreventDefault })
864-
.simulate('keyDown', { which: KeyCode.NUM_ONE })
865-
.simulate('change', { target: { value: '1' } })
866-
.simulate('compositionStart')
867-
.simulate('compositionEnd');
859+
expect(wrapper.find('textarea').length).toBe(1);
860+
toggleOpen(wrapper);
861+
wrapper
862+
.find('.rc-select')
863+
.find('textarea')
864+
.simulate('mouseDown', { preventDefault: mouseDownPreventDefault })
865+
.simulate('keyDown', { which: KeyCode.NUM_ONE })
866+
.simulate('change', { target: { value: '1' } })
867+
.simulate('compositionStart')
868+
.simulate('compositionEnd');
868869

869-
selectItem(wrapper);
870-
expect(wrapper.find('textarea').props().value).toEqual('1');
871-
expect(wrapper.find('textarea').hasClass('custom-input')).toBe(true);
872-
expect(mouseDownPreventDefault).not.toHaveBeenCalled();
873-
expect(onKeyDown).toHaveBeenCalled();
874-
expect(onChange).toHaveBeenCalled();
875-
expect(onMouseDown).toHaveBeenCalled();
876-
expect(textareaRef).toHaveBeenCalled();
877-
expect(onCompositionStart).toHaveBeenCalled();
878-
expect(onCompositionEnd).toHaveBeenCalled();
870+
selectItem(wrapper);
871+
expect(wrapper.find('textarea').props().value).toEqual('1');
872+
expect(wrapper.find('textarea').hasClass('custom-input')).toBe(true);
873+
expect(mouseDownPreventDefault).not.toHaveBeenCalled();
874+
expect(onKeyDown).toHaveBeenCalled();
875+
expect(onChange).toHaveBeenCalled();
876+
expect(onMouseDown).toHaveBeenCalled();
877+
expect(textareaRef).toHaveBeenCalled();
878+
expect(onCompositionStart).toHaveBeenCalled();
879+
expect(onCompositionEnd).toHaveBeenCalled();
880+
});
881+
882+
it('not override customize props', () => {
883+
const wrapper = mount(
884+
<Select mode="combobox" getInputElement={() => <input type="email" />}>
885+
<Option value="1">1</Option>
886+
<Option value="2">2</Option>
887+
</Select>,
888+
);
889+
890+
expect(wrapper.find('input').prop('type')).toEqual('email');
891+
});
879892
});
880893

881894
it('getRawInputElement for rc-cascader', () => {
@@ -1370,7 +1383,6 @@ describe('Select.Basic', () => {
13701383
);
13711384

13721385
for (let i = 0; i < 10; i += 1) {
1373-
console.log('=>', i);
13741386
onSelect.mockReset();
13751387
wrapper.find('input').simulate('keyDown', { which: KeyCode.ENTER });
13761388
expect(onSelect).toHaveBeenCalledWith('1', expect.anything());

0 commit comments

Comments
 (0)