Skip to content

Commit

Permalink
Merge pull request #145 from RaoHai/getInputElement
Browse files Browse the repository at this point in the history
 feature: Customize input element of combobox
  • Loading branch information
yiminghe authored Jan 3, 2017
2 parents f7c2323 + 2412cb2 commit e8d6292
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ React.render(c, container);
|onDeselect | called when a option is deselected. param is option's value. only called for multiple or tags | Function(value) | - |
|defaultActiveFirstOption | whether active first option by default | bool | true |
|getPopupContainer | container which popup select menu rendered into | function(trigger:Node):Node | function(){return document.body;} |
|getInputElement| customize input element | function(): Element | - |

### Option props

Expand Down
21 changes: 13 additions & 8 deletions src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const Select = React.createClass({
dropdownStyle: PropTypes.object,
maxTagTextLength: PropTypes.number,
tokenSeparators: PropTypes.arrayOf(PropTypes.string),
getInputElement: PropTypes.func,
},

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

getInputElement() {
const props = this.props;
const inputElement = props.getInputElement ? props.getInputElement() : <input />;
const inputCls = classnames(inputElement.props.className, {
[`${props.prefixCls}-search__field`]: true,
});
return (<div className={`${props.prefixCls}-search__field__wrap`}>
<input
ref={this.saveInputRef}
onChange={this.onInputChange}
onKeyDown={this.onInputKeyDown}
value={this.state.inputValue}
disabled={props.disabled}
className={`${props.prefixCls}-search__field`}
/>
{React.cloneElement(inputElement, {
ref: this.saveInputRef,
onChange: this.onInputChange,
onKeyDown: this.onInputKeyDown,
value: this.state.inputValue,
disabled: props.disabled,
className: inputCls,
})}
<span
ref={this.saveInputMirrorRef}
className={`${props.prefixCls}-search__field__mirror`}
Expand Down
16 changes: 16 additions & 0 deletions tests/Select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,20 @@ describe('Select', () => {

expect(input.focus).toBeCalled();
});

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

expect(wrapper.find('textarea').length).toBe(1);
wrapper.find('.rc-select').simulate('click');
const dropdownWrapper = mount(wrapper.find('Trigger').node.getComponent());

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

0 comments on commit e8d6292

Please sign in to comment.