Skip to content

Commit e8d6292

Browse files
authored
Merge pull request #145 from RaoHai/getInputElement
feature: Customize input element of combobox
2 parents f7c2323 + 2412cb2 commit e8d6292

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ React.render(c, container);
9797
|onDeselect | called when a option is deselected. param is option's value. only called for multiple or tags | Function(value) | - |
9898
|defaultActiveFirstOption | whether active first option by default | bool | true |
9999
|getPopupContainer | container which popup select menu rendered into | function(trigger:Node):Node | function(){return document.body;} |
100+
|getInputElement| customize input element | function(): Element | - |
100101

101102
### Option props
102103

src/Select.jsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const Select = React.createClass({
7777
dropdownStyle: PropTypes.object,
7878
maxTagTextLength: PropTypes.number,
7979
tokenSeparators: PropTypes.arrayOf(PropTypes.string),
80+
getInputElement: PropTypes.func,
8081
},
8182

8283
mixins: [FilterMixin],
@@ -506,15 +507,19 @@ const Select = React.createClass({
506507

507508
getInputElement() {
508509
const props = this.props;
510+
const inputElement = props.getInputElement ? props.getInputElement() : <input />;
511+
const inputCls = classnames(inputElement.props.className, {
512+
[`${props.prefixCls}-search__field`]: true,
513+
});
509514
return (<div className={`${props.prefixCls}-search__field__wrap`}>
510-
<input
511-
ref={this.saveInputRef}
512-
onChange={this.onInputChange}
513-
onKeyDown={this.onInputKeyDown}
514-
value={this.state.inputValue}
515-
disabled={props.disabled}
516-
className={`${props.prefixCls}-search__field`}
517-
/>
515+
{React.cloneElement(inputElement, {
516+
ref: this.saveInputRef,
517+
onChange: this.onInputChange,
518+
onKeyDown: this.onInputKeyDown,
519+
value: this.state.inputValue,
520+
disabled: props.disabled,
521+
className: inputCls,
522+
})}
518523
<span
519524
ref={this.saveInputMirrorRef}
520525
className={`${props.prefixCls}-search__field__mirror`}

tests/Select.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,20 @@ describe('Select', () => {
408408

409409
expect(input.focus).toBeCalled();
410410
});
411+
412+
it('combox could comstomize input element', () => {
413+
const wrapper = mount(
414+
<Select combobox getInputElement={() => <textarea />}>
415+
<Option value="1">1</Option>
416+
<Option value="2">2</Option>
417+
</Select>
418+
);
419+
420+
expect(wrapper.find('textarea').length).toBe(1);
421+
wrapper.find('.rc-select').simulate('click');
422+
const dropdownWrapper = mount(wrapper.find('Trigger').node.getComponent());
423+
424+
dropdownWrapper.find('MenuItem').first().simulate('click');
425+
expect(wrapper.state().inputValue).toBe('1');
426+
});
411427
});

0 commit comments

Comments
 (0)