From 6dc6794881846f635e496bb14c7acaccc56db986 Mon Sep 17 00:00:00 2001 From: Dave Miller Date: Thu, 7 Jan 2016 07:36:51 -0800 Subject: [PATCH] Add onSelectionChange for Android TextInput Summary: public Add an onSelectionChange method to TextInput that works on Android same as iOS Reviewed By: andreicoman11 Differential Revision: D2780131 fb-gh-sync-id: 9b3b8fbd9ea653d43e3107a338e4bc08bde2e8c6 --- Libraries/Components/TextInput/TextInput.js | 17 ++++++ .../react/views/textinput/ReactEditText.java | 25 +++++++- .../textinput/ReactTextInputManager.java | 43 ++++++++++++++ .../ReactTextInputSelectionEvent.java | 58 +++++++++++++++++++ .../views/textinput/SelectionWatcher.java | 18 ++++++ 5 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputSelectionEvent.java create mode 100644 ReactAndroid/src/main/java/com/facebook/react/views/textinput/SelectionWatcher.java diff --git a/Libraries/Components/TextInput/TextInput.js b/Libraries/Components/TextInput/TextInput.js index 62ff5f2a8..53bc6235d 100644 --- a/Libraries/Components/TextInput/TextInput.js +++ b/Libraries/Components/TextInput/TextInput.js @@ -216,6 +216,10 @@ var TextInput = React.createClass({ * Callback that is called when text input ends. */ onEndEditing: PropTypes.func, + /** + * Callback that is called when the text input selection is changed + */ + onSelectionChange: PropTypes.func, /** * Callback that is called when the text input's submit button is pressed. * Invalid if multiline={true} is specified. @@ -474,6 +478,17 @@ var TextInput = React.createClass({ }, _renderAndroid: function() { + var onSelectionChange; + if (this.props.selectionState || this.props.onSelectionChange) { + onSelectionChange = (event: Event) => { + if (this.props.selectionState) { + var selection = event.nativeEvent.selection; + this.props.selectionState.update(selection.start, selection.end); + } + this.props.onSelectionChange && this.props.onSelectionChange(event); + }; + } + var autoCapitalize = UIManager.UIText.AutocapitalizationType[this.props.autoCapitalize]; var textAlign = UIManager.AndroidTextInput.Constants.TextAlign[this.props.textAlign]; @@ -489,6 +504,7 @@ var TextInput = React.createClass({ if (childCount > 1) { children = {children}; } + var textContainer = { + + private static final String EVENT_NAME = "topSelectionChange"; + + private int mSelectionStart; + private int mSelectionEnd; + + public ReactTextInputSelectionEvent( + int viewId, + long timestampMs, + int selectionStart, + int selectionEnd) { + super(viewId, timestampMs); + mSelectionStart = selectionStart; + mSelectionEnd = selectionEnd; + } + + @Override + public String getEventName() { + return EVENT_NAME; + } + + @Override + public void dispatch(RCTEventEmitter rctEventEmitter) { + rctEventEmitter.receiveEvent(getViewTag(), getEventName(), serializeEventData()); + } + + private WritableMap serializeEventData() { + WritableMap eventData = Arguments.createMap(); + + WritableMap selectionData = Arguments.createMap(); + selectionData.putInt("start", mSelectionStart); + selectionData.putInt("end", mSelectionEnd); + + eventData.putMap("selection", selectionData); + return eventData; + } +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/SelectionWatcher.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/SelectionWatcher.java new file mode 100644 index 000000000..f569067ab --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/SelectionWatcher.java @@ -0,0 +1,18 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.views.textinput; + +/** + * Implement this interface to be informed of selection changes in the ReactTextEdit + * This is used by the ReactTextInputManager to forward events from the EditText to JS + */ +interface SelectionWatcher { + public void onSelectionChanged(int start, int end); +}