RN: Delete TextInput.selectionState Prop

Summary:
Deletes the `selectionState` prop from `TextInput`.

It does not provide meaningful value over `onBlur`, `onFocus`, and `selectionState`.

Changelog:
[Breaking][TextInput] Removing `selectionState` prop, use `onBlur`, `onFocus`, and `onUpdate` instead.

Reviewed By: zackargyle, TheSavior

Differential Revision: D17879667

fbshipit-source-id: 03a4e239406932adad898d6d2a092e3bc2e6b064
This commit is contained in:
Tim Yung
2019-10-12 15:47:17 -07:00
committed by Facebook Github Bot
parent 63769518e0
commit 2becdfd404
3 changed files with 1 additions and 198 deletions
+1 -31
View File
@@ -10,7 +10,6 @@
'use strict';
const DeprecatedTextInputPropTypes = require('../../DeprecatedPropTypes/DeprecatedTextInputPropTypes');
const DocumentSelectionState = require('../../vendor/document/selection/DocumentSelectionState');
const NativeMethodsMixin = require('../../Renderer/shims/NativeMethodsMixin');
const Platform = require('../../Utilities/Platform');
const React = require('react');
@@ -221,23 +220,6 @@ type IOSProps = $ReadOnly<{|
*/
enablesReturnKeyAutomatically?: ?boolean,
/**
* An instance of `DocumentSelectionState`, this is some state that is responsible for
* maintaining selection information for a document.
*
* Some functionality that can be performed with this instance is:
*
* - `blur()`
* - `focus()`
* - `update()`
*
* > You can reference `DocumentSelectionState` in
* > [`vendor/document/selection/DocumentSelectionState.js`](https://github.com/facebook/react-native/blob/master/Libraries/vendor/document/selection/DocumentSelectionState.js)
*
* @platform ios
*/
selectionState?: ?DocumentSelectionState,
/**
* When the clear button should appear on the right side of the text view.
* This property is supported only for single-line TextInput component.
@@ -1115,10 +1097,6 @@ const TextInput = createReactClass({
if (this.props.onFocus) {
this.props.onFocus(event);
}
if (this.props.selectionState) {
this.props.selectionState.focus();
}
},
_onPress: function(event: PressEvent) {
@@ -1161,7 +1139,7 @@ const TextInput = createReactClass({
this._lastNativeSelection = event.nativeEvent.selection;
if (this.props.selection || this.props.selectionState) {
if (this.props.selection) {
this.forceUpdate();
}
},
@@ -1198,10 +1176,6 @@ const TextInput = createReactClass({
) {
this._inputRef.setNativeProps(nativeProps);
}
if (this.props.selectionState && selection) {
this.props.selectionState.update(selection.start, selection.end);
}
},
_onBlur: function(event: BlurEvent) {
@@ -1211,10 +1185,6 @@ const TextInput = createReactClass({
if (this.props.onBlur) {
this.props.onBlur(event);
}
if (this.props.selectionState) {
this.props.selectionState.blur();
}
},
_onTextInput: function(event: TextInputEvent) {
@@ -12,7 +12,6 @@
const DeprecatedColorPropType = require('./DeprecatedColorPropType');
const DeprecatedViewPropTypes = require('./DeprecatedViewPropTypes');
const DocumentSelectionState = require('../vendor/document/selection/DocumentSelectionState');
const PropTypes = require('prop-types');
const Text = require('../Text/Text');
@@ -388,22 +387,6 @@ module.exports = {
* The highlight and cursor color of the text input.
*/
selectionColor: DeprecatedColorPropType,
/**
* An instance of `DocumentSelectionState`, this is some state that is responsible for
* maintaining selection information for a document.
*
* Some functionality that can be performed with this instance is:
*
* - `blur()`
* - `focus()`
* - `update()`
*
* > You can reference `DocumentSelectionState` in
* > [`vendor/document/selection/DocumentSelectionState.js`](https://github.com/facebook/react-native/blob/master/Libraries/vendor/document/selection/DocumentSelectionState.js)
*
* @platform ios
*/
selectionState: (PropTypes.instanceOf(DocumentSelectionState): void),
/**
* The start and end of the text input's selection. Set start and end to
* the same value to position the cursor.
@@ -1,150 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @typechecks
*/
'use strict';
const mixInEventEmitter = require('../../emitter/mixInEventEmitter');
/**
* DocumentSelectionState is responsible for maintaining selection information
* for a document.
*
* It is intended for use by AbstractTextEditor-based components for
* identifying the appropriate start/end positions to modify the
* DocumentContent, and for programmatically setting browser selection when
* components re-render.
*/
class DocumentSelectionState {
/**
* @param {number} anchor
* @param {number} focus
*/
constructor(anchor, focus) {
this._anchorOffset = anchor;
this._focusOffset = focus;
this._hasFocus = false;
}
/**
* Apply an update to the state. If either offset value has changed,
* set the values and emit the `change` event. Otherwise no-op.
*
* @param {number} anchor
* @param {number} focus
*/
update(anchor, focus) {
if (this._anchorOffset !== anchor || this._focusOffset !== focus) {
this._anchorOffset = anchor;
this._focusOffset = focus;
this.emit('update');
}
}
/**
* Given a max text length, constrain our selection offsets to ensure
* that the selection remains strictly within the text range.
*
* @param {number} maxLength
*/
constrainLength(maxLength) {
this.update(
Math.min(this._anchorOffset, maxLength),
Math.min(this._focusOffset, maxLength),
);
}
focus() {
if (!this._hasFocus) {
this._hasFocus = true;
this.emit('focus');
}
}
blur() {
if (this._hasFocus) {
this._hasFocus = false;
this.emit('blur');
}
}
/**
* @return {boolean}
*/
hasFocus() {
return this._hasFocus;
}
/**
* @return {boolean}
*/
isCollapsed() {
return this._anchorOffset === this._focusOffset;
}
/**
* @return {boolean}
*/
isBackward() {
return this._anchorOffset > this._focusOffset;
}
/**
* @return {?number}
*/
getAnchorOffset() {
return this._hasFocus ? this._anchorOffset : null;
}
/**
* @return {?number}
*/
getFocusOffset() {
return this._hasFocus ? this._focusOffset : null;
}
/**
* @return {?number}
*/
getStartOffset() {
return this._hasFocus
? Math.min(this._anchorOffset, this._focusOffset)
: null;
}
/**
* @return {?number}
*/
getEndOffset() {
return this._hasFocus
? Math.max(this._anchorOffset, this._focusOffset)
: null;
}
/**
* @param {number} start
* @param {number} end
* @return {boolean}
*/
overlaps(start, end) {
return (
this.hasFocus() &&
this.getStartOffset() <= end &&
start <= this.getEndOffset()
);
}
}
mixInEventEmitter(DocumentSelectionState, {
blur: true,
focus: true,
update: true,
});
module.exports = DocumentSelectionState;