-
-
Save adamski/d16895ded0d4a9e2e7df8594b5e9d3ab to your computer and use it in GitHub Desktop.
Terrible TextInput extends that doesn't steal all touches.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import {TextInput, TouchableWithoutFeedback, StyleSheet} from 'react-native'; | |
const Platform = require('Platform'); | |
const requireNativeComponent = require('requireNativeComponent'); | |
const emptyFunction = require('fbjs/lib/emptyFunction'); | |
if (Platform.OS === 'android') { | |
var AndroidTextInput = requireNativeComponent('AndroidTextInput', null); | |
} else if (Platform.OS === 'ios') { | |
var RCTTextView = requireNativeComponent('RCTTextView', null); | |
var RCTTextField = requireNativeComponent('RCTTextField', null); | |
} | |
const onlyMultiline = { | |
onTextInput: true, | |
children: true, | |
}; | |
class SlideTextInput extends TextInput { | |
constructor() { | |
super(); | |
this._renderIOS = this._renderIOSWithoutEventSteal; | |
} | |
_renderIOSWithoutEventSteal() { | |
var textContainer; | |
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 props = Object.assign({}, this.props); | |
props.style = [styles.input, this.props.style]; | |
if (!props.multiline) { | |
if (__DEV__) { | |
for (var propKey in onlyMultiline) { | |
if (props[propKey]) { | |
const error = new Error( | |
'TextInput prop `' + propKey + '` is only supported with multiline.' | |
); | |
warning(false, '%s', error.stack); | |
} | |
} | |
} | |
textContainer = | |
<RCTTextField | |
ref={this._setNativeRef} | |
{...props} | |
onFocus={this._onFocus} | |
onBlur={this._onBlur} | |
onChange={this._onChange} | |
onSelectionChange={onSelectionChange} | |
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue} | |
text={this._getText()} | |
/>; | |
} else { | |
var children = props.children; | |
var childCount = 0; | |
ReactChildren.forEach(children, () => ++childCount); | |
invariant( | |
!(props.value && childCount), | |
'Cannot specify both value and children.' | |
); | |
if (childCount >= 1) { | |
children = <Text style={props.style}>{children}</Text>; | |
} | |
if (props.inputView) { | |
children = [children, props.inputView]; | |
} | |
textContainer = | |
<RCTTextView | |
ref={this._setNativeRef} | |
{...props} | |
children={children} | |
onFocus={this._onFocus} | |
onBlur={this._onBlur} | |
onChange={this._onChange} | |
onContentSizeChange={this.props.onContentSizeChange} | |
onSelectionChange={onSelectionChange} | |
onTextInput={this._onTextInput} | |
onSelectionChangeShouldSetResponder={emptyFunction.thatReturnsTrue} | |
text={this._getText()} | |
/>; | |
} | |
return ( | |
<TouchableWithoutFeedback | |
onLayout={props.onLayout} | |
onPress={this._onPress} | |
rejectResponderTermination={false} | |
accessible={props.accessible} | |
accessibilityLabel={props.accessibilityLabel} | |
accessibilityTraits={props.accessibilityTraits} | |
testID={props.testID}> | |
{textContainer} | |
</TouchableWithoutFeedback> | |
); | |
} | |
} | |
var styles = StyleSheet.create({ | |
input: { | |
alignSelf: 'stretch', | |
}, | |
}); | |
export default SlideTextInput; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment