/** * Copyright (c) 2013-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. * * The examples provided by Facebook are for non-commercial testing and * evaluation purposes only. * * Facebook reserves all rights not expressly granted. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * @flow */ 'use strict'; var React = require('react'); var ReactNative = require('react-native'); var { Text, TextInput, View, StyleSheet, } = ReactNative; class TextEventsExample extends React.Component { state = { curText: '', prevText: '', prev2Text: '', }; updateText = (text) => { this.setState((state) => { return { curText: text, prevText: state.curText, prev2Text: state.prevText, }; }); }; render() { return ( this.updateText('onFocus')} onBlur={() => this.updateText('onBlur')} onChange={(event) => this.updateText( 'onChange text: ' + event.nativeEvent.text )} onEndEditing={(event) => this.updateText( 'onEndEditing text: ' + event.nativeEvent.text )} onSubmitEditing={(event) => this.updateText( 'onSubmitEditing text: ' + event.nativeEvent.text )} style={styles.singleLine} /> {this.state.curText}{'\n'} (prev: {this.state.prevText}){'\n'} (prev2: {this.state.prev2Text}) ); } } class AutoExpandingTextInput extends React.Component { constructor(props) { super(props); this.state = { text: 'React Native enables you to build world-class application experiences on native platforms using a consistent developer experience based on JavaScript and React. The focus of React Native is on developer efficiency across all the platforms you care about — learn once, write anywhere. Facebook uses React Native in multiple production apps and will continue investing in React Native.', height: 0, }; } render() { return ( { this.setState({height: event.nativeEvent.contentSize.height}); }} onChangeText={(text) => { this.setState({text}); }} style={[styles.default, {height: Math.max(35, this.state.height)}]} value={this.state.text} /> ); } } class RewriteExample extends React.Component { 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 { 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 { 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 { state: 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 ); } } var styles = StyleSheet.create({ multiline: { height: 60, fontSize: 16, padding: 4, marginBottom: 10, }, eventLabel: { margin: 3, fontSize: 12, }, singleLine: { fontSize: 16, padding: 4, }, 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: '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 ( ); } }, { 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 ( ); } }, ];