2015-02-20 04:10:52 +00:00
/ * *
2016-07-12 12:51:57 +00:00
* 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 .
*
2015-03-28 05:18:47 +00:00
* The examples provided by Facebook are for non - commercial testing and
* evaluation purposes only .
2015-03-23 20:35:08 +00:00
*
2015-03-28 05:18:47 +00:00
* 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 .
2015-03-23 20:35:08 +00:00
*
2015-03-23 18:36:57 +00:00
* @ flow
2015-02-20 04:10:52 +00:00
* /
'use strict' ;
2016-04-09 03:36:40 +00:00
var React = require ( 'react' ) ;
var ReactNative = require ( 'react-native' ) ;
2015-02-20 04:10:52 +00:00
var {
Text ,
TextInput ,
View ,
StyleSheet ,
2016-04-09 03:36:40 +00:00
} = ReactNative ;
2015-02-20 04:10:52 +00:00
2016-07-26 08:00:02 +00:00
class WithLabel extends React . Component {
render ( ) {
2015-02-20 04:10:52 +00:00
return (
< View style = { styles . labelContainer } >
< View style = { styles . label } >
< Text > { this . props . label } < / T e x t >
< / V i e w >
{ this . props . children }
< / V i e w >
) ;
2016-07-26 08:00:02 +00:00
}
}
2015-02-20 04:10:52 +00:00
2016-07-26 08:00:02 +00:00
class TextEventsExample extends React . Component {
state = {
curText : '<No Event>' ,
prevText : '<No Event>' ,
prev2Text : '<No Event>' ,
prev3Text : '<No Event>' ,
} ;
2015-02-20 04:10:52 +00:00
2016-07-26 08:00:02 +00:00
updateText = ( text ) => {
2015-07-21 19:37:24 +00:00
this . setState ( ( state ) => {
return {
curText : text ,
prevText : state . curText ,
prev2Text : state . prevText ,
2015-11-02 17:13:41 +00:00
prev3Text : state . prev2Text ,
2015-07-21 19:37:24 +00:00
} ;
2015-02-20 04:10:52 +00:00
} ) ;
2016-07-26 08:00:02 +00:00
} ;
2015-02-20 04:10:52 +00:00
2016-07-26 08:00:02 +00:00
render ( ) {
2015-02-20 04:10:52 +00:00
return (
< View >
< TextInput
2015-03-04 22:04:52 +00:00
autoCapitalize = "none"
2015-02-20 04:10:52 +00:00
placeholder = "Enter text to see events"
autoCorrect = { false }
onFocus = { ( ) => 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
) }
2015-11-16 22:01:53 +00:00
onSelectionChange = { ( event ) => this . updateText (
'onSelectionChange range: ' +
event . nativeEvent . selection . start + ',' +
event . nativeEvent . selection . end
) }
2015-11-02 17:13:41 +00:00
onKeyPress = { ( event ) => {
this . updateText ( 'onKeyPress key: ' + event . nativeEvent . key ) ;
} }
2015-02-20 04:10:52 +00:00
style = { styles . default }
/ >
< Text style = { styles . eventLabel } >
{ this . state . curText } { '\n' }
2015-07-21 19:37:24 +00:00
( prev : { this . state . prevText } ) { '\n' }
2015-11-02 17:13:41 +00:00
( prev2 : { this . state . prev2Text } ) { '\n' }
( prev3 : { this . state . prev3Text } )
2015-02-20 04:10:52 +00:00
< / T e x t >
< / V i e w >
) ;
}
2016-07-26 08:00:02 +00:00
}
2015-02-20 04:10:52 +00:00
2016-01-25 13:45:44 +00:00
class AutoExpandingTextInput extends React . Component {
2016-02-25 10:44:58 +00:00
state : any ;
2016-01-25 13:45:44 +00:00
constructor ( props ) {
super ( props ) ;
2016-07-07 15:44:59 +00:00
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 ,
} ;
2016-01-25 13:45:44 +00:00
}
render ( ) {
return (
< TextInput
{ ... this . props }
multiline = { true }
2016-07-07 15:44:59 +00:00
onChangeText = { ( text ) => {
this . setState ( { text } ) ;
} }
onContentSizeChange = { ( event ) => {
this . setState ( { height : event . nativeEvent . contentSize . height } ) ;
2016-01-25 13:45:44 +00:00
} }
style = { [ styles . default , { height : Math . max ( 35 , this . state . height ) } ] }
value = { this . state . text }
/ >
) ;
}
}
2015-07-21 19:37:24 +00:00
class RewriteExample extends React . Component {
2016-02-25 10:44:58 +00:00
state : any ;
2015-07-21 19:37:24 +00:00
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 (
< View style = { styles . rewriteContainer } >
< TextInput
multiline = { false }
maxLength = { limit }
onChangeText = { ( text ) => {
text = text . replace ( / /g , '_' ) ;
this . setState ( { text } ) ;
} }
style = { styles . default }
value = { this . state . text }
/ >
< Text style = { [ styles . remainder , { color : remainderColor } ] } >
{ remainder }
< / T e x t >
< / V i e w >
) ;
}
}
2015-12-14 14:41:45 +00:00
class RewriteExampleInvalidCharacters extends React . Component {
2016-02-25 10:44:58 +00:00
state : any ;
2015-12-14 14:41:45 +00:00
constructor ( props ) {
super ( props ) ;
this . state = { text : '' } ;
}
render ( ) {
return (
< View style = { styles . rewriteContainer } >
< TextInput
multiline = { false }
onChangeText = { ( text ) => {
this . setState ( { text : text . replace ( /\s/g , '' ) } ) ;
} }
style = { styles . default }
value = { this . state . text }
/ >
< / V i e w >
) ;
}
}
2015-11-06 15:25:19 +00:00
class TokenizedTextExample extends React . Component {
2016-02-25 10:44:58 +00:00
state : any ;
2015-11-06 15:25:19 +00:00
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 ) ) {
2015-12-12 05:21:02 +00:00
return < Text key = { text } style = { styles . hashtag } > { text } < / T e x t > ;
2015-11-06 15:25:19 +00:00
} else {
return text ;
}
} ) ;
return (
< View >
< TextInput
multiline = { true }
style = { styles . multiline }
onChangeText = { ( text ) => {
this . setState ( { text } ) ;
} } >
< Text > { parts } < / T e x t >
< / T e x t I n p u t >
< / V i e w >
) ;
}
}
2016-07-26 08:00:02 +00:00
class BlurOnSubmitExample extends React . Component {
focusNextField = ( nextField ) => {
2016-01-12 16:17:49 +00:00
this . refs [ nextField ] . focus ( ) ;
2016-07-26 08:00:02 +00:00
} ;
2015-11-05 05:01:49 +00:00
2016-07-26 08:00:02 +00:00
render ( ) {
2015-11-05 05:01:49 +00:00
return (
< View >
< TextInput
2016-01-12 16:17:49 +00:00
ref = "1"
2015-11-05 05:01:49 +00:00
style = { styles . default }
2016-01-12 16:17:49 +00:00
placeholder = "blurOnSubmit = false"
returnKeyType = "next"
2015-11-05 05:01:49 +00:00
blurOnSubmit = { false }
onSubmitEditing = { ( ) => this . focusNextField ( '2' ) }
/ >
< TextInput
2016-01-12 16:17:49 +00:00
ref = "2"
2015-11-05 05:01:49 +00:00
style = { styles . default }
2016-01-12 16:17:49 +00:00
keyboardType = "email-address"
placeholder = "blurOnSubmit = false"
returnKeyType = "next"
2015-11-05 05:01:49 +00:00
blurOnSubmit = { false }
onSubmitEditing = { ( ) => this . focusNextField ( '3' ) }
/ >
< TextInput
2016-01-12 16:17:49 +00:00
ref = "3"
2015-11-05 05:01:49 +00:00
style = { styles . default }
2016-01-12 16:17:49 +00:00
keyboardType = "url"
placeholder = "blurOnSubmit = false"
returnKeyType = "next"
2015-11-05 05:01:49 +00:00
blurOnSubmit = { false }
onSubmitEditing = { ( ) => this . focusNextField ( '4' ) }
/ >
< TextInput
2016-01-12 16:17:49 +00:00
ref = "4"
2015-11-05 05:01:49 +00:00
style = { styles . default }
2016-01-12 16:17:49 +00:00
keyboardType = "numeric"
placeholder = "blurOnSubmit = false"
2015-11-05 05:01:49 +00:00
blurOnSubmit = { false }
onSubmitEditing = { ( ) => this . focusNextField ( '5' ) }
/ >
< TextInput
2016-01-12 16:17:49 +00:00
ref = "5"
2015-11-05 05:01:49 +00:00
style = { styles . default }
2016-01-12 16:17:49 +00:00
keyboardType = "numbers-and-punctuation"
placeholder = "blurOnSubmit = true"
returnKeyType = "done"
2015-11-05 05:01:49 +00:00
/ >
< / V i e w >
) ;
}
2016-07-26 08:00:02 +00:00
}
2015-11-05 05:01:49 +00:00
2015-02-20 04:10:52 +00:00
var styles = StyleSheet . create ( {
page : {
paddingBottom : 300 ,
} ,
default : {
height : 26 ,
borderWidth : 0.5 ,
borderColor : '#0f0f0f' ,
flex : 1 ,
fontSize : 13 ,
2015-04-29 08:29:00 +00:00
padding : 4 ,
2015-02-20 04:10:52 +00:00
} ,
multiline : {
borderWidth : 0.5 ,
borderColor : '#0f0f0f' ,
flex : 1 ,
fontSize : 13 ,
height : 50 ,
2015-04-29 08:29:00 +00:00
padding : 4 ,
2015-04-29 22:55:36 +00:00
marginBottom : 4 ,
2015-04-29 08:29:00 +00:00
} ,
multilineWithFontStyles : {
color : 'blue' ,
fontWeight : 'bold' ,
fontSize : 18 ,
fontFamily : 'Cochin' ,
2015-04-29 22:55:36 +00:00
height : 60 ,
} ,
multilineChild : {
width : 50 ,
height : 40 ,
position : 'absolute' ,
right : 5 ,
backgroundColor : 'red' ,
2015-02-20 04:10:52 +00:00
} ,
eventLabel : {
margin : 3 ,
fontSize : 12 ,
} ,
labelContainer : {
flexDirection : 'row' ,
marginVertical : 2 ,
flex : 1 ,
} ,
label : {
2015-07-21 19:37:24 +00:00
width : 115 ,
alignItems : 'flex-end' ,
2015-02-20 04:10:52 +00:00
marginRight : 10 ,
paddingTop : 2 ,
} ,
2015-07-21 19:37:24 +00:00
rewriteContainer : {
flexDirection : 'row' ,
alignItems : 'center' ,
} ,
remainder : {
textAlign : 'right' ,
width : 24 ,
} ,
2015-11-06 15:25:19 +00:00
hashtag : {
color : 'blue' ,
fontWeight : 'bold' ,
} ,
2015-02-20 04:10:52 +00:00
} ) ;
2015-06-22 16:43:30 +00:00
exports . displayName = ( undefined : ? string ) ;
2015-02-20 04:10:52 +00:00
exports . title = '<TextInput>' ;
2015-04-29 08:29:00 +00:00
exports . description = 'Single and multi-line text inputs.' ;
2015-02-20 04:10:52 +00:00
exports . examples = [
{
title : 'Auto-focus' ,
render : function ( ) {
2016-02-04 13:12:36 +00:00
return (
< TextInput
autoFocus = { true }
style = { styles . default }
accessibilityLabel = "I am the accessibility label for text input"
/ >
) ;
2015-02-20 04:10:52 +00:00
}
} ,
2015-07-21 19:37:24 +00:00
{
title : "Live Re-Write (<sp> -> '_') + maxLength" ,
render : function ( ) {
return < RewriteExample / > ;
}
} ,
2015-12-14 14:41:45 +00:00
{
2016-01-12 16:17:49 +00:00
title : 'Live Re-Write (no spaces allowed)' ,
2015-12-14 14:41:45 +00:00
render : function ( ) {
return < RewriteExampleInvalidCharacters / > ;
}
} ,
2015-02-20 04:10:52 +00:00
{
title : 'Auto-capitalize' ,
render : function ( ) {
return (
< View >
< WithLabel label = "none" >
< TextInput
2015-03-04 22:04:52 +00:00
autoCapitalize = "none"
2015-02-20 04:10:52 +00:00
style = { styles . default }
/ >
< / W i t h L a b e l >
< WithLabel label = "sentences" >
< TextInput
2015-03-04 22:04:52 +00:00
autoCapitalize = "sentences"
2015-02-20 04:10:52 +00:00
style = { styles . default }
/ >
< / W i t h L a b e l >
< WithLabel label = "words" >
< TextInput
2015-03-04 22:04:52 +00:00
autoCapitalize = "words"
2015-02-20 04:10:52 +00:00
style = { styles . default }
/ >
< / W i t h L a b e l >
< WithLabel label = "characters" >
< TextInput
2015-03-04 22:04:52 +00:00
autoCapitalize = "characters"
2015-02-20 04:10:52 +00:00
style = { styles . default }
/ >
< / W i t h L a b e l >
< / V i e w >
) ;
}
} ,
{
title : 'Auto-correct' ,
render : function ( ) {
return (
< View >
< WithLabel label = "true" >
< TextInput autoCorrect = { true } style = { styles . default } / >
< / W i t h L a b e l >
< WithLabel label = "false" >
< TextInput autoCorrect = { false } style = { styles . default } / >
< / W i t h L a b e l >
< / V i e w >
) ;
}
} ,
2015-03-31 01:25:30 +00:00
{
title : 'Keyboard types' ,
render : function ( ) {
var keyboardTypes = [
'default' ,
'ascii-capable' ,
'numbers-and-punctuation' ,
'url' ,
'number-pad' ,
'phone-pad' ,
'name-phone-pad' ,
'email-address' ,
'decimal-pad' ,
'twitter' ,
'web-search' ,
'numeric' ,
] ;
var examples = keyboardTypes . map ( ( type ) => {
return (
< WithLabel key = { type } label = { type } >
< TextInput
keyboardType = { type }
style = { styles . default }
/ >
< / W i t h L a b e l >
) ;
} ) ;
return < View > { examples } < / V i e w > ;
}
} ,
2015-11-11 13:31:18 +00:00
{
title : 'Keyboard appearance' ,
render : function ( ) {
var keyboardAppearance = [
'default' ,
'light' ,
'dark' ,
] ;
var examples = keyboardAppearance . map ( ( type ) => {
return (
< WithLabel key = { type } label = { type } >
< TextInput
keyboardAppearance = { type }
style = { styles . default }
/ >
< / W i t h L a b e l >
) ;
} ) ;
return < View > { examples } < / V i e w > ;
}
} ,
2015-03-31 01:25:30 +00:00
{
title : 'Return key types' ,
render : function ( ) {
var returnKeyTypes = [
'default' ,
'go' ,
'google' ,
'join' ,
'next' ,
'route' ,
'search' ,
'send' ,
'yahoo' ,
'done' ,
'emergency-call' ,
] ;
var examples = returnKeyTypes . map ( ( type ) => {
return (
< WithLabel key = { type } label = { type } >
< TextInput
returnKeyType = { type }
style = { styles . default }
/ >
< / W i t h L a b e l >
) ;
} ) ;
return < View > { examples } < / V i e w > ;
}
} ,
{
title : 'Enable return key automatically' ,
render : function ( ) {
return (
< View >
< WithLabel label = "true" >
< TextInput enablesReturnKeyAutomatically = { true } style = { styles . default } / >
< / W i t h L a b e l >
< / V i e w >
) ;
}
} ,
{
title : 'Secure text entry' ,
render : function ( ) {
return (
< View >
< WithLabel label = "true" >
2016-06-01 13:57:43 +00:00
< TextInput secureTextEntry = { true } style = { styles . default } defaultValue = "abc" / >
2015-03-31 01:25:30 +00:00
< / W i t h L a b e l >
< / V i e w >
) ;
}
} ,
2015-02-20 04:10:52 +00:00
{
title : 'Event handling' ,
2016-05-25 01:20:12 +00:00
render : function ( ) : ReactElement < any > { return < TextEventsExample / > ; } ,
2015-02-20 04:10:52 +00:00
} ,
{
title : 'Colored input text' ,
render : function ( ) {
return (
< View >
< TextInput
style = { [ styles . default , { color : 'blue' } ] }
2015-07-22 21:03:32 +00:00
defaultValue = "Blue"
2015-02-20 04:10:52 +00:00
/ >
< TextInput
style = { [ styles . default , { color : 'green' } ] }
2015-07-22 21:03:32 +00:00
defaultValue = "Green"
2015-02-20 04:10:52 +00:00
/ >
< / V i e w >
) ;
}
} ,
2016-02-03 08:21:30 +00:00
{
title : 'Colored highlight/cursor for text input' ,
render : function ( ) {
return (
< View >
< TextInput
style = { styles . default }
2016-02-03 13:48:31 +00:00
selectionColor = { "green" }
2016-02-03 08:21:30 +00:00
defaultValue = "Highlight me"
/ >
< TextInput
style = { styles . default }
2016-02-03 13:48:31 +00:00
selectionColor = { "rgba(86, 76, 205, 1)" }
2016-02-03 08:21:30 +00:00
defaultValue = "Highlight me"
/ >
< / V i e w >
) ;
}
} ,
2015-02-27 16:40:52 +00:00
{
title : 'Clear button mode' ,
render : function ( ) {
return (
< View >
< WithLabel label = "never" >
< TextInput
style = { styles . default }
2015-03-04 22:04:52 +00:00
clearButtonMode = "never"
2015-02-27 16:40:52 +00:00
/ >
< / W i t h L a b e l >
< WithLabel label = "while editing" >
< TextInput
style = { styles . default }
2015-03-04 22:04:52 +00:00
clearButtonMode = "while-editing"
2015-02-27 16:40:52 +00:00
/ >
< / W i t h L a b e l >
< WithLabel label = "unless editing" >
< TextInput
style = { styles . default }
2015-03-04 22:04:52 +00:00
clearButtonMode = "unless-editing"
2015-02-27 16:40:52 +00:00
/ >
< / W i t h L a b e l >
< WithLabel label = "always" >
< TextInput
style = { styles . default }
2015-03-04 22:04:52 +00:00
clearButtonMode = "always"
2015-02-27 16:40:52 +00:00
/ >
< / W i t h L a b e l >
< / V i e w >
) ;
}
} ,
2015-04-15 00:51:28 +00:00
{
title : 'Clear and select' ,
2015-04-29 08:29:00 +00:00
render : function ( ) {
2015-04-15 00:51:28 +00:00
return (
< View >
< WithLabel label = "clearTextOnFocus" >
< TextInput
placeholder = "text is cleared on focus"
2015-07-22 21:03:32 +00:00
defaultValue = "text is cleared on focus"
2015-04-15 00:51:28 +00:00
style = { styles . default }
clearTextOnFocus = { true }
/ >
< / W i t h L a b e l >
< WithLabel label = "selectTextOnFocus" >
< TextInput
placeholder = "text is selected on focus"
2015-07-22 21:03:32 +00:00
defaultValue = "text is selected on focus"
2015-04-15 00:51:28 +00:00
style = { styles . default }
selectTextOnFocus = { true }
/ >
< / W i t h L a b e l >
< / V i e w >
) ;
}
} ,
2015-11-05 05:01:49 +00:00
{
title : 'Blur on submit' ,
2016-05-25 01:20:12 +00:00
render : function ( ) : ReactElement < any > { return < BlurOnSubmitExample / > ; } ,
2015-11-05 05:01:49 +00:00
} ,
2015-12-02 15:11:20 +00:00
{
title : 'Multiline blur on submit' ,
render : function ( ) {
return (
< View >
< TextInput
style = { styles . multiline }
2016-01-12 16:17:49 +00:00
placeholder = "blurOnSubmit = true"
returnKeyType = "next"
2015-12-02 15:11:20 +00:00
blurOnSubmit = { true }
multiline = { true }
onSubmitEditing = { event => alert ( event . nativeEvent . text ) }
/ >
< / V i e w >
) ;
}
} ,
2015-11-27 14:52:29 +00:00
{
title : 'Multiline' ,
render : function ( ) {
return (
< View >
< TextInput
placeholder = "multiline text input"
multiline = { true }
style = { styles . multiline }
/ >
< TextInput
placeholder = "multiline text input with font styles and placeholder"
multiline = { true }
clearTextOnFocus = { true }
autoCorrect = { true }
autoCapitalize = "words"
placeholderTextColor = "red"
keyboardType = "url"
style = { [ styles . multiline , styles . multilineWithFontStyles ] }
/ >
2016-07-14 14:39:18 +00:00
< TextInput
placeholder = "multiline text input with max length"
maxLength = { 5 }
multiline = { true }
style = { styles . multiline }
/ >
2015-11-27 14:52:29 +00:00
< TextInput
placeholder = "uneditable multiline text input"
editable = { false }
multiline = { true }
style = { styles . multiline }
/ >
< TextInput
placeholder = "multiline with children"
multiline = { true }
enablesReturnKeyAutomatically = { true }
returnKeyType = "go"
style = { styles . multiline } >
< View style = { styles . multilineChild } / >
< / T e x t I n p u t >
< / V i e w >
) ;
}
} ,
2016-01-25 13:45:44 +00:00
{
title : 'Auto-expanding' ,
render : function ( ) {
return (
< View >
< AutoExpandingTextInput
placeholder = "height increases with content"
enablesReturnKeyAutomatically = { true }
2016-04-15 09:27:26 +00:00
returnKeyType = "default"
2016-01-25 13:45:44 +00:00
/ >
< / V i e w >
) ;
}
} ,
2015-11-27 14:52:29 +00:00
{
title : 'Attributed text' ,
render : function ( ) {
return < TokenizedTextExample / > ;
}
} ,
2015-02-20 04:10:52 +00:00
] ;