mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Bring missing changes to 0.58-stable branch
This reverts commit b864e7e63e.
This commit is contained in:
@@ -34,6 +34,8 @@ const warning = require('fbjs/lib/warning');
|
||||
import type {TextStyleProp, ViewStyleProp} from 'StyleSheet';
|
||||
import type {ColorValue} from 'StyleSheetTypes';
|
||||
import type {ViewProps} from 'ViewPropTypes';
|
||||
import type {SyntheticEvent, ScrollEvent} from 'CoreEventTypes';
|
||||
import type {PressEvent} from 'CoreEventTypes';
|
||||
|
||||
let AndroidTextInput;
|
||||
let RCTMultilineTextInputView;
|
||||
@@ -55,11 +57,73 @@ const onlyMultiline = {
|
||||
children: true,
|
||||
};
|
||||
|
||||
type Event = Object;
|
||||
type Selection = {
|
||||
export type ChangeEvent = SyntheticEvent<
|
||||
$ReadOnly<{|
|
||||
eventCount: number,
|
||||
target: number,
|
||||
text: string,
|
||||
|}>,
|
||||
>;
|
||||
|
||||
export type TextInputEvent = SyntheticEvent<
|
||||
$ReadOnly<{|
|
||||
eventCount: number,
|
||||
previousText: string,
|
||||
range: $ReadOnly<{|
|
||||
start: number,
|
||||
end: number,
|
||||
|}>,
|
||||
target: number,
|
||||
text: string,
|
||||
|}>,
|
||||
>;
|
||||
|
||||
export type ContentSizeChangeEvent = SyntheticEvent<
|
||||
$ReadOnly<{|
|
||||
target: number,
|
||||
contentSize: $ReadOnly<{|
|
||||
width: number,
|
||||
height: number,
|
||||
|}>,
|
||||
|}>,
|
||||
>;
|
||||
|
||||
type TargetEvent = SyntheticEvent<
|
||||
$ReadOnly<{|
|
||||
target: number,
|
||||
|}>,
|
||||
>;
|
||||
|
||||
export type BlurEvent = TargetEvent;
|
||||
export type FocusEvent = TargetEvent;
|
||||
|
||||
type Selection = $ReadOnly<{|
|
||||
start: number,
|
||||
end?: number,
|
||||
};
|
||||
end: number,
|
||||
|}>;
|
||||
|
||||
export type SelectionChangeEvent = SyntheticEvent<
|
||||
$ReadOnly<{|
|
||||
selection: Selection,
|
||||
target: number,
|
||||
|}>,
|
||||
>;
|
||||
|
||||
export type KeyPressEvent = SyntheticEvent<
|
||||
$ReadOnly<{|
|
||||
key: string,
|
||||
target?: ?number,
|
||||
eventCount?: ?number,
|
||||
|}>,
|
||||
>;
|
||||
|
||||
export type EditingEvent = SyntheticEvent<
|
||||
$ReadOnly<{|
|
||||
eventCount: number,
|
||||
text: string,
|
||||
target: number,
|
||||
|}>,
|
||||
>;
|
||||
|
||||
const DataDetectorTypes = [
|
||||
'phoneNumber',
|
||||
@@ -184,17 +248,17 @@ type Props = $ReadOnly<{|
|
||||
returnKeyType?: ?ReturnKeyType,
|
||||
maxLength?: ?number,
|
||||
multiline?: ?boolean,
|
||||
onBlur?: ?Function,
|
||||
onFocus?: ?Function,
|
||||
onChange?: ?Function,
|
||||
onChangeText?: ?Function,
|
||||
onContentSizeChange?: ?Function,
|
||||
onTextInput?: ?Function,
|
||||
onEndEditing?: ?Function,
|
||||
onSelectionChange?: ?Function,
|
||||
onSubmitEditing?: ?Function,
|
||||
onKeyPress?: ?Function,
|
||||
onScroll?: ?Function,
|
||||
onBlur?: ?(e: BlurEvent) => void,
|
||||
onFocus?: ?(e: FocusEvent) => void,
|
||||
onChange?: ?(e: ChangeEvent) => void,
|
||||
onChangeText?: ?(text: string) => void,
|
||||
onContentSizeChange?: ?(e: ContentSizeChangeEvent) => void,
|
||||
onTextInput?: ?(e: TextInputEvent) => void,
|
||||
onEndEditing?: ?(e: EditingEvent) => void,
|
||||
onSelectionChange?: ?(e: SelectionChangeEvent) => void,
|
||||
onSubmitEditing?: ?(e: EditingEvent) => void,
|
||||
onKeyPress?: ?(e: KeyPressEvent) => void,
|
||||
onScroll?: ?(e: ScrollEvent) => void,
|
||||
placeholder?: ?Stringish,
|
||||
placeholderTextColor?: ?ColorValue,
|
||||
secureTextEntry?: ?boolean,
|
||||
@@ -792,7 +856,7 @@ const TextInput = createReactClass({
|
||||
'oneTimeCode',
|
||||
]),
|
||||
},
|
||||
getDefaultProps(): Object {
|
||||
getDefaultProps() {
|
||||
return {
|
||||
allowFontScaling: true,
|
||||
underlineColorAndroid: 'transparent',
|
||||
@@ -1108,7 +1172,7 @@ const TextInput = createReactClass({
|
||||
);
|
||||
},
|
||||
|
||||
_onFocus: function(event: Event) {
|
||||
_onFocus: function(event: FocusEvent) {
|
||||
if (this.props.onFocus) {
|
||||
this.props.onFocus(event);
|
||||
}
|
||||
@@ -1118,16 +1182,16 @@ const TextInput = createReactClass({
|
||||
}
|
||||
},
|
||||
|
||||
_onPress: function(event: Event) {
|
||||
_onPress: function(event: PressEvent) {
|
||||
if (this.props.editable || this.props.editable === undefined) {
|
||||
this.focus();
|
||||
}
|
||||
},
|
||||
|
||||
_onChange: function(event: Event) {
|
||||
_onChange: function(event: ChangeEvent) {
|
||||
// Make sure to fire the mostRecentEventCount first so it is already set on
|
||||
// native when the text value is set.
|
||||
if (this._inputRef) {
|
||||
if (this._inputRef && this._inputRef.setNativeProps) {
|
||||
this._inputRef.setNativeProps({
|
||||
mostRecentEventCount: event.nativeEvent.eventCount,
|
||||
});
|
||||
@@ -1147,7 +1211,7 @@ const TextInput = createReactClass({
|
||||
this.forceUpdate();
|
||||
},
|
||||
|
||||
_onSelectionChange: function(event: Event) {
|
||||
_onSelectionChange: function(event: SelectionChangeEvent) {
|
||||
this.props.onSelectionChange && this.props.onSelectionChange(event);
|
||||
|
||||
if (!this._inputRef) {
|
||||
@@ -1188,7 +1252,11 @@ const TextInput = createReactClass({
|
||||
nativeProps.selection = this.props.selection;
|
||||
}
|
||||
|
||||
if (Object.keys(nativeProps).length > 0 && this._inputRef) {
|
||||
if (
|
||||
Object.keys(nativeProps).length > 0 &&
|
||||
this._inputRef &&
|
||||
this._inputRef.setNativeProps
|
||||
) {
|
||||
this._inputRef.setNativeProps(nativeProps);
|
||||
}
|
||||
|
||||
@@ -1211,11 +1279,11 @@ const TextInput = createReactClass({
|
||||
}
|
||||
},
|
||||
|
||||
_onTextInput: function(event: Event) {
|
||||
_onTextInput: function(event: TextInputEvent) {
|
||||
this.props.onTextInput && this.props.onTextInput(event);
|
||||
},
|
||||
|
||||
_onScroll: function(event: Event) {
|
||||
_onScroll: function(event: ScrollEvent) {
|
||||
this.props.onScroll && this.props.onScroll(event);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @emails oncall+react_native
|
||||
* @format
|
||||
* @flow-strict
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const React = require('React');
|
||||
const ReactTestRenderer = require('react-test-renderer');
|
||||
const TextInput = require('TextInput');
|
||||
|
||||
import Component from '@reactions/component';
|
||||
|
||||
const {enter} = require('ReactNativeTestTools');
|
||||
|
||||
jest.unmock('TextInput');
|
||||
|
||||
describe('TextInput tests', () => {
|
||||
let input;
|
||||
let onChangeListener;
|
||||
let onChangeTextListener;
|
||||
const initialValue = 'initialValue';
|
||||
beforeEach(() => {
|
||||
onChangeListener = jest.fn();
|
||||
onChangeTextListener = jest.fn();
|
||||
const renderTree = ReactTestRenderer.create(
|
||||
<Component initialState={{text: initialValue}}>
|
||||
{({setState, state}) => (
|
||||
<TextInput
|
||||
value={state.text}
|
||||
onChangeText={text => {
|
||||
onChangeTextListener(text);
|
||||
setState({text});
|
||||
}}
|
||||
onChange={event => {
|
||||
onChangeListener(event);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Component>,
|
||||
);
|
||||
input = renderTree.root.findByType(TextInput);
|
||||
});
|
||||
it('has expected instance functions', () => {
|
||||
expect(input.instance.isFocused).toBeInstanceOf(Function); // Would have prevented S168585
|
||||
expect(input.instance.clear).toBeInstanceOf(Function);
|
||||
expect(input.instance.focus).toBeInstanceOf(Function);
|
||||
expect(input.instance.blur).toBeInstanceOf(Function);
|
||||
expect(input.instance.setNativeProps).toBeInstanceOf(Function);
|
||||
expect(input.instance.measure).toBeInstanceOf(Function);
|
||||
expect(input.instance.measureInWindow).toBeInstanceOf(Function);
|
||||
expect(input.instance.measureLayout).toBeInstanceOf(Function);
|
||||
});
|
||||
it('calls onChange callbacks', () => {
|
||||
expect(input.props.value).toBe(initialValue);
|
||||
const message = 'This is a test message';
|
||||
enter(input, message);
|
||||
expect(input.props.value).toBe(message);
|
||||
expect(onChangeTextListener).toHaveBeenCalledWith(message);
|
||||
expect(onChangeListener).toHaveBeenCalledWith({
|
||||
nativeEvent: {text: message},
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user