/**
* 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.
*
* @format
* @flow
*/
'use strict';
const Button = require('Button');
const InputAccessoryView = require('InputAccessoryView');
var React = require('react');
var ReactNative = require('react-native');
var {Text, TextInput, View, StyleSheet, Slider, Switch} = ReactNative;
class WithLabel extends React.Component<$FlowFixMeProps> {
render() {
return (
{this.props.label}
{this.props.children}
);
}
}
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)
}
onEndEditing={event =>
this.updateText('onEndEditing text: ' + event.nativeEvent.text)
}
onSubmitEditing={event =>
this.updateText('onSubmitEditing text: ' + event.nativeEvent.text)
}
onSelectionChange={event =>
this.updateText(
'onSelectionChange range: ' +
event.nativeEvent.selection.start +
',' +
event.nativeEvent.selection.end,
)
}
onKeyPress={event => {
this.updateText('onKeyPress key: ' + event.nativeEvent.key);
}}
style={styles.default}
/>
{this.state.curText}
{'\n'}
(prev: {this.state.prevText}){'\n'}
(prev2: {this.state.prev2Text}){'\n'}
(prev3: {this.state.prev3Text})
);
}
}
class TextInputAccessoryViewExample extends React.Component<{}, *> {
constructor(props) {
super(props);
this.state = {text: 'Placeholder Text'};
}
render() {
const inputAccessoryViewID = 'inputAccessoryView1';
return (
this.setState({text})}
value={this.state.text}
/>
);
}
}
class RewriteExample extends React.Component<$FlowFixMeProps, any> {
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 RewriteExampleInvalidCharacters extends React.Component<
$FlowFixMeProps,
any,
> {
constructor(props) {
super(props);
this.state = {text: ''};
}
render() {
return (
{
this.setState({text: text.replace(/\s/g, '')});
}}
style={styles.default}
value={this.state.text}
/>
);
}
}
class RewriteExampleKana extends React.Component<$FlowFixMeProps, any> {
constructor(props) {
super(props);
this.state = {text: ''};
}
render() {
return (
{
this.setState({text: text.replace(/ひ/g, '日')});
}}
style={styles.default}
value={this.state.text}
/>
);
}
}
class SecureEntryExample extends React.Component<$FlowFixMeProps, any> {
constructor(props) {
super(props);
this.state = {text: ''};
}
render() {
return (
this.setState({text})}
value={this.state.text}
/>
Current text is: {this.state.text}
);
}
}
class TokenizedTextExample extends React.Component<$FlowFixMeProps, any> {
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')}
/>
);
}
}
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(
(a, b) => a - b,
);
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<
$FlowFixMeProps,
$FlowFixMeState,
> {
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({
page: {
paddingBottom: 300,
},
default: {
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#0f0f0f',
flex: 1,
fontSize: 13,
padding: 4,
},
multiline: {
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#0f0f0f',
flex: 1,
fontSize: 13,
height: 50,
padding: 4,
marginBottom: 4,
},
multilineExpandable: {
height: 'auto',
maxHeight: 100,
},
multilineWithFontStyles: {
color: 'blue',
fontWeight: 'bold',
fontSize: 18,
fontFamily: 'Cochin',
height: 60,
},
multilineChild: {
width: 50,
height: 40,
position: 'absolute',
right: 5,
backgroundColor: 'red',
},
eventLabel: {
margin: 3,
fontSize: 12,
},
labelContainer: {
flexDirection: 'row',
marginVertical: 2,
flex: 1,
},
label: {
width: 115,
alignItems: 'flex-end',
marginRight: 10,
paddingTop: 2,
},
rewriteContainer: {
flexDirection: 'row',
alignItems: 'center',
},
remainder: {
textAlign: 'right',
width: 24,
},
hashtag: {
color: 'blue',
fontWeight: 'bold',
},
});
exports.displayName = (undefined: ?string);
exports.title = '';
exports.description = 'Single and multi-line text inputs.';
exports.examples = [
{
title: 'Auto-focus',
render: function() {
return (
);
},
},
{
title: "Live Re-Write ( -> '_') + maxLength",
render: function() {
return ;
},
},
{
title: 'Live Re-Write (no spaces allowed)',
render: function() {
return ;
},
},
{
title: 'Live Re-Write (ひ -> 日)',
render: function() {
return ;
},
},
{
title: 'Keyboard Accessory View',
render: function() {
return ;
},
},
{
title: 'Auto-capitalize',
render: function() {
return (
);
},
},
{
title: 'Auto-correct',
render: function() {
return (
);
},
},
{
title: 'Nested content and `value` property',
render: function() {
return (
(first raw text node)
(internal raw text node)
(last raw text node)
(first raw text node)
(internal raw text node)
(last raw text node)
);
},
},
{
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 (
);
});
return {examples};
},
},
{
title: 'Keyboard appearance',
render: function() {
var keyboardAppearance = ['default', 'light', 'dark'];
var examples = keyboardAppearance.map(type => {
return (
);
});
return {examples};
},
},
{
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 (
);
});
return {examples};
},
},
{
title: 'Enable return key automatically',
render: function() {
return (
);
},
},
{
title: 'Secure text entry',
render: function() {
return ;
},
},
{
title: 'Event handling',
render: function(): React.Element {
return ;
},
},
{
title: 'Colored input text',
render: function() {
return (
);
},
},
{
title: 'Colored highlight/cursor for text input',
render: function() {
return (
);
},
},
{
title: 'Clear button mode',
render: function() {
return (
);
},
},
{
title: 'Clear and select',
render: function() {
return (
);
},
},
{
title: 'Blur on submit',
render: function(): React.Element {
return ;
},
},
{
title: 'Multiline blur on submit',
render: function() {
return (
alert(event.nativeEvent.text)}
/>
);
},
},
{
title: 'Multiline',
render: function() {
return (
);
},
},
{
title: 'TextInput Intrinsic Size',
render: function() {
return (
Singleline TextInput
Multiline TextInput
);
},
},
{
title: 'Auto-expanding',
render: function() {
return (
);
},
},
{
title: 'Auto-expanding',
render: function() {
return (
huge
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: 'Text selection & cursor placement',
render: function() {
return (
);
},
},
{
title: 'TextInput maxLength',
render: function() {
return (
);
},
},
];