/** * Copyright (c) 2015-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow */ 'use strict'; var React = require('react'); var ReactNative = require('react-native'); var { Text, TextInput, View, StyleSheet, Slider, Switch, } = ReactNative; class TextEventsExample extends React.Component<{}, $FlowFixMeState> { state = { curText: '', prevText: '', prev2Text: '', prev3Text: '', }; updateText = (text) => { this.setState((state) => { return { curText: text, prevText: state.curText, prev2Text: state.prevText, prev3Text: state.prev2Text, }; }); }; render() { return ( this.updateText('onFocus')} onBlur={() => this.updateText('onBlur')} onChange={(event) => this.updateText( 'onChange text: ' + event.nativeEvent.text )} onContentSizeChange={(event) => this.updateText( 'onContentSizeChange size: ' + event.nativeEvent.contentSize )} onEndEditing={(event) => this.updateText( 'onEndEditing text: ' + event.nativeEvent.text )} onSubmitEditing={(event) => this.updateText( 'onSubmitEditing text: ' + event.nativeEvent.text )} onKeyPress={(event) => this.updateText( 'onKeyPress key: ' + event.nativeEvent.key )} style={styles.singleLine} /> {this.state.curText}{'\n'} (prev: {this.state.prevText}){'\n'} (prev2: {this.state.prev2Text}){'\n'} (prev3: {this.state.prev3Text}) ); } } class RewriteExample extends React.Component<$FlowFixMeProps, $FlowFixMeState> { constructor(props) { super(props); this.state = {text: ''}; } render() { var limit = 20; var remainder = limit - this.state.text.length; var remainderColor = remainder > 5 ? 'blue' : 'red'; return ( { text = text.replace(/ /g, '_'); this.setState({text}); }} style={styles.default} value={this.state.text} /> {remainder} ); } } class TokenizedTextExample extends React.Component<$FlowFixMeProps, $FlowFixMeState> { constructor(props) { super(props); this.state = {text: 'Hello #World'}; } render() { //define delimiter let delimiter = /\s+/; //split string let _text = this.state.text; let token, index, parts = []; while (_text) { delimiter.lastIndex = 0; token = delimiter.exec(_text); if (token === null) { break; } index = token.index; if (token[0].length === 0) { index = 1; } parts.push(_text.substr(0, index)); parts.push(token[0]); index = index + token[0].length; _text = _text.slice(index); } parts.push(_text); //highlight hashtags parts = parts.map((text) => { if (/^#/.test(text)) { return {text}; } else { return text; } }); return ( { this.setState({text}); }}> {parts} ); } } class BlurOnSubmitExample extends React.Component<{}> { focusNextField = (nextField) => { this.refs[nextField].focus(); }; render() { return ( this.focusNextField('2')} /> this.focusNextField('3')} /> this.focusNextField('4')} /> this.focusNextField('5')} /> ); } } class ToggleDefaultPaddingExample extends React.Component<$FlowFixMeProps, $FlowFixMeState> { constructor(props) { super(props); this.state = {hasPadding: false}; } render() { return ( this.setState({hasPadding: !this.state.hasPadding})}> Toggle padding ); } } type SelectionExampleState = { selection: { start: number; end: number; }; value: string; }; class SelectionExample extends React.Component<$FlowFixMeProps, SelectionExampleState> { _textInput: any; constructor(props) { super(props); this.state = { selection: {start: 0, end: 0}, value: props.value }; } onSelectionChange({nativeEvent: {selection}}) { this.setState({selection}); } getRandomPosition() { var length = this.state.value.length; return Math.round(Math.random() * length); } select(start, end) { this._textInput.focus(); this.setState({selection: {start, end}}); } selectRandom() { var positions = [this.getRandomPosition(), this.getRandomPosition()].sort(); this.select(...positions); } placeAt(position) { this.select(position, position); } placeAtRandom() { this.placeAt(this.getRandomPosition()); } render() { var length = this.state.value.length; return ( this.setState({value})} onSelectionChange={this.onSelectionChange.bind(this)} ref={textInput => (this._textInput = textInput)} selection={this.state.selection} style={this.props.style} value={this.state.value} /> selection = {JSON.stringify(this.state.selection)} Place at Start (0, 0) Place at End ({length}, {length}) Place at Random Select All Select Random ); } } class AutogrowingTextInputExample extends React.Component<{}> { constructor(props) { super(props); this.state = { width: 100, multiline: true, text: '', contentSize: { width: 0, height: 0, }, }; } UNSAFE_componentWillReceiveProps(props) { this.setState({ multiline: props.multiline, }); } render() { var {style, multiline, ...props} = this.props; return ( Width: this.setState({width: value})} /> Multiline: this.setState({multiline: value})} /> TextInput: this.setState({text: value})} onContentSizeChange={(event) => this.setState({contentSize: event.nativeEvent.contentSize})} {...props} /> Plain text value representation: {this.state.text} Content Size: {JSON.stringify(this.state.contentSize)} ); } } var styles = StyleSheet.create({ multiline: { height: 60, fontSize: 16, }, eventLabel: { margin: 3, fontSize: 12, }, singleLine: { fontSize: 16, }, singleLineWithHeightTextInput: { height: 30, }, hashtag: { color: 'blue', fontWeight: 'bold', }, }); exports.title = ''; exports.description = 'Single and multi-line text inputs.'; exports.examples = [ { title: 'Auto-focus', render: function() { return ( ); } }, { title: "Live Re-Write ( -> '_')", render: function() { return ; } }, { title: 'Auto-capitalize', render: function() { var autoCapitalizeTypes = [ 'none', 'sentences', 'words', 'characters', ]; var examples = autoCapitalizeTypes.map((type) => { return ( ); }); return {examples}; } }, { title: 'Auto-correct', render: function() { return ( ); } }, { title: 'Keyboard types', render: function() { var keyboardTypes = [ 'default', 'email-address', 'numeric', 'phone-pad', ]; var examples = keyboardTypes.map((type) => { return ( ); }); return {examples}; } }, { title: 'Blur on submit', render: function(): React.Element { return ; }, }, { title: 'Event handling', render: function(): React.Element { return ; }, }, { title: 'Colors and text inputs', render: function() { return ( Darker backgroundColor ); } }, { title: 'Text input, themes and heights', render: function() { return ( ); } }, { title: 'fontFamily, fontWeight and fontStyle', render: function() { return ( ); } }, { title: 'letterSpacing', render: function() { return ( ); } }, { title: 'Passwords', render: function() { return ( ); } }, { title: 'Editable', render: function() { return ( ); } }, { title: 'Multiline', render: function() { return ( multiline with children, aligned bottom-right ); } }, { title: 'Fixed number of lines', platform: 'android', render: function() { return ( ); } }, { title: 'Auto-expanding', render: function() { return ( generic generic generic small small small small small small regular regular huge huge huge huge huge generic generic generic ); } }, { title: 'Attributed text', render: function() { return ; } }, { title: 'Return key', render: function() { var returnKeyTypes = [ 'none', 'go', 'search', 'send', 'done', 'previous', 'next', ]; var returnKeyLabels = [ 'Compile', 'React Native', ]; var examples = returnKeyTypes.map((type) => { return ( ); }); var types = returnKeyLabels.map((type) => { return ( ); }); return {examples}{types}; } }, { title: 'Inline Images', render: function() { return ( ); } }, { title: 'Toggle Default Padding', render: function(): React.Element { return ; }, }, { title: 'Text selection & cursor placement', render: function() { return ( ); } }, ];