From 9db3bd7e67abe7cf268921d55bfb90648f8980ef Mon Sep 17 00:00:00 2001 From: Nicolas Gallagher Date: Thu, 27 Oct 2016 22:17:59 -0700 Subject: [PATCH] [add] TextInput support for onKeyPress Fix #215 --- docs/components/TextInput.md | 5 +++++ src/components/TextInput/index.js | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/docs/components/TextInput.md b/docs/components/TextInput.md index f1ffdd76..4c5064bb 100644 --- a/docs/components/TextInput.md +++ b/docs/components/TextInput.md @@ -87,6 +87,11 @@ as an argument to the callback handler. Callback that is called when the text input is focused. +**onKeyPress**: function + +Callback that is called when a key is pressed. Pressed key value is passed as +an argument to the callback handler. Fires before `onChange` callbacks. + (web) **onSelectionChange**: function Callback that is called when the text input's selection changes. The following diff --git a/src/components/TextInput/index.js b/src/components/TextInput/index.js index 3b16b121..8485fc96 100644 --- a/src/components/TextInput/index.js +++ b/src/components/TextInput/index.js @@ -35,6 +35,7 @@ class TextInput extends Component { onChange: PropTypes.func, onChangeText: PropTypes.func, onFocus: PropTypes.func, + onKeyPress: PropTypes.func, onSelectionChange: PropTypes.func, placeholder: PropTypes.string, placeholderTextColor: PropTypes.string, @@ -87,6 +88,7 @@ class TextInput extends Component { maxNumberOfLines, multiline, numberOfLines, + onKeyPress, onLayout, onSelectionChange, placeholder, @@ -139,6 +141,7 @@ class TextInput extends Component { onBlur: this._handleBlur, onChange: this._handleChange, onFocus: this._handleFocus, + onKeyPress, onSelect: onSelectionChange && this._handleSelectionChange, readOnly: !editable, ref: this._setInputRef, @@ -200,6 +203,7 @@ class TextInput extends Component { _handleChange = (e) => { const { onChange, onChangeText } = this.props; const text = e.target.value; + e.nativeEvent.text = text; this.setState({ showPlaceholder: text === '' }); if (onChange) { onChange(e); } if (onChangeText) { onChangeText(text); }