Skip to content

Commit

Permalink
Merge pull request #175 from RaoHai/passthroughOnKeyDown
Browse files Browse the repository at this point in the history
 passThrough `onKeyDown` of Custom Input
  • Loading branch information
yesmeck committed Apr 2, 2017
2 parents 712d960 + b14b045 commit 5e1686c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ function saveRef(name, component) {
this[name] = component;
}

function chaining(...fns) {
return function (...args) {
for (let i = 0; i < fns.length; i++) {
if (fns[i] && typeof fns[i] === 'function') {
fns[i].apply(this, args);
}
}
};
}

const Select = React.createClass({
propTypes: SelectPropTypes,

Expand Down Expand Up @@ -470,7 +480,7 @@ const Select = React.createClass({
{React.cloneElement(inputElement, {
ref: this.saveInputRef,
onChange: this.onInputChange,
onKeyDown: this.onInputKeyDown,
onKeyDown: chaining(this.onInputKeyDown, inputElement.props.onKeyDown),
value: this.state.inputValue,
disabled: props.disabled,
className: inputCls,
Expand Down
5 changes: 4 additions & 1 deletion tests/Select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,19 +427,22 @@ describe('Select', () => {
});

it('combox could comstomize input element', () => {
const handleKeyDown = jest.fn();
const wrapper = mount(
<Select combobox getInputElement={() => <textarea />}>
<Select combobox getInputElement={() => <textarea onKeyDown={handleKeyDown} />}>
<Option value="1">1</Option>
<Option value="2">2</Option>
</Select>
);

expect(wrapper.find('textarea').length).toBe(1);
wrapper.find('.rc-select').simulate('click');
wrapper.find('.rc-select').find('textarea').simulate('keyDown', { keyCode: KeyCode.NUM_ONE });
const dropdownWrapper = mount(wrapper.find('Trigger').node.getComponent());

dropdownWrapper.find('MenuItem').first().simulate('click');
expect(wrapper.state().inputValue).toBe('1');
expect(handleKeyDown).toBeCalled();
});

describe('propTypes', () => {
Expand Down

0 comments on commit 5e1686c

Please sign in to comment.