From 0bc33e116959458507b842ef00dd257654244e2f Mon Sep 17 00:00:00 2001 From: Kevin Leung Date: Sat, 4 Jan 2020 19:28:01 +0800 Subject: [PATCH 1/2] Support ref to element --- lib/inputfield.js | 4 +++- src/inputfield.js | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/inputfield.js b/lib/inputfield.js index 1c18383..1ab8cbe 100644 --- a/lib/inputfield.js +++ b/lib/inputfield.js @@ -96,13 +96,15 @@ var InputField = function (_Component) { var _props = this.props, onInputChange = _props.onInputChange, - restProps = _objectWithoutProperties(_props, ['onInputChange']); + inputRef = _props.inputRef, + restProps = _objectWithoutProperties(_props, ['onInputChange', 'inputRef']); return _react2.default.createElement('input', _extends({ type: 'text' }, restProps, { ref: function ref(input) { _this2.input = input; + if (inputRef) inputRef(input); }, value: this.state.value, onChange: this.handleInputChange, diff --git a/src/inputfield.js b/src/inputfield.js index df6f8b1..dd610f8 100644 --- a/src/inputfield.js +++ b/src/inputfield.js @@ -71,12 +71,15 @@ class InputField extends Component { } render() { - const { onInputChange, ...restProps } = this.props + const { onInputChange, inputRef, ...restProps } = this.props return ( {this.input = input}} + ref={input => { + this.input = input; + if(inputRef) inputRef(input); + }} value={this.state.value} onChange={this.handleInputChange} onCompositionStart={this.handleComposition} From 59a9c5af1621890a6b6a42677db65b6911b7d7d0 Mon Sep 17 00:00:00 2001 From: Kevin Leung Date: Mon, 13 Jan 2020 13:04:09 +0800 Subject: [PATCH 2/2] Allow specifying component (e.g. textarea) --- lib/inputfield.js | 8 ++++---- src/inputfield.js | 28 +++++++++++++--------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/inputfield.js b/lib/inputfield.js index 1ab8cbe..8f656e3 100644 --- a/lib/inputfield.js +++ b/lib/inputfield.js @@ -95,13 +95,12 @@ var InputField = function (_Component) { var _this2 = this; var _props = this.props, + component = _props.component, onInputChange = _props.onInputChange, inputRef = _props.inputRef, - restProps = _objectWithoutProperties(_props, ['onInputChange', 'inputRef']); + restProps = _objectWithoutProperties(_props, ['component', 'onInputChange', 'inputRef']); - return _react2.default.createElement('input', _extends({ - type: 'text' - }, restProps, { + return _react2.default.createElement(component, _extends({}, restProps, { ref: function ref(input) { _this2.input = input; if (inputRef) inputRef(input); @@ -124,6 +123,7 @@ InputField.propTypes = { onInputChange: _propTypes2.default.func }; InputField.defaultProps = { + component: 'input', onChange: noop, onInputChange: noop }; diff --git a/src/inputfield.js b/src/inputfield.js index dd610f8..ea08330 100644 --- a/src/inputfield.js +++ b/src/inputfield.js @@ -16,6 +16,7 @@ class InputField extends Component { } static defaultProps = { + component: 'input', onChange: noop, onInputChange: noop } @@ -71,21 +72,18 @@ class InputField extends Component { } render() { - const { onInputChange, inputRef, ...restProps } = this.props - return ( - { - this.input = input; - if(inputRef) inputRef(input); - }} - value={this.state.value} - onChange={this.handleInputChange} - onCompositionStart={this.handleComposition} - onCompositionEnd={this.handleComposition} - /> - ) + const { component, onInputChange, inputRef, ...restProps } = this.props + return React.createElement(component, { + ...restProps, + ref: input => { + this.input = input; + if(inputRef) inputRef(input); + }, + value: this.state.value, + onChange: this.handleInputChange, + onCompositionStart: this.handleComposition, + onCompositionEnd: this.handleComposition, + }); } }