2015-09-14 14:35:58 +00:00
|
|
|
/**
|
|
|
|
* 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-native');
|
|
|
|
var {
|
|
|
|
Text,
|
|
|
|
TextInput,
|
|
|
|
View,
|
|
|
|
StyleSheet,
|
|
|
|
} = React;
|
|
|
|
|
|
|
|
var TextEventsExample = React.createClass({
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
curText: '<No Event>',
|
|
|
|
prevText: '<No Event>',
|
|
|
|
prev2Text: '<No Event>',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
updateText: function(text) {
|
|
|
|
this.setState((state) => {
|
|
|
|
return {
|
|
|
|
curText: text,
|
|
|
|
prevText: state.curText,
|
|
|
|
prev2Text: state.prevText,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
autoCapitalize="none"
|
|
|
|
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
|
|
|
|
)}
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
<Text style={styles.eventLabel}>
|
|
|
|
{this.state.curText}{'\n'}
|
|
|
|
(prev: {this.state.prevText}){'\n'}
|
|
|
|
(prev2: {this.state.prev2Text})
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-25 13:45:44 +00:00
|
|
|
class AutoExpandingTextInput extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {text: '', height: 0};
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
{...this.props}
|
|
|
|
multiline={true}
|
|
|
|
onChange={(event) => {
|
|
|
|
this.setState({
|
|
|
|
text: event.nativeEvent.text,
|
|
|
|
height: event.nativeEvent.contentSize.height,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
style={[styles.default, {height: Math.max(35, this.state.height)}]}
|
|
|
|
value={this.state.text}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 14:35:58 +00:00
|
|
|
class RewriteExample extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {text: ''};
|
|
|
|
}
|
|
|
|
render() {
|
2015-11-06 21:25:05 +00:00
|
|
|
var limit = 20;
|
|
|
|
var remainder = limit - this.state.text.length;
|
|
|
|
var remainderColor = remainder > 5 ? 'blue' : 'red';
|
2015-09-14 14:35:58 +00:00
|
|
|
return (
|
2015-11-06 21:25:05 +00:00
|
|
|
<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}
|
|
|
|
</Text>
|
|
|
|
</View>
|
2015-09-14 14:35:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 16:22:22 +00:00
|
|
|
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 key={text} style={styles.hashtag}>{text}</Text>;
|
|
|
|
} else {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
multiline={true}
|
|
|
|
style={styles.multiline}
|
|
|
|
onChangeText={(text) => {
|
|
|
|
this.setState({text});
|
|
|
|
}}>
|
|
|
|
<Text>{parts}</Text>
|
|
|
|
</TextInput>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 15:06:37 +00:00
|
|
|
var BlurOnSubmitExample = React.createClass({
|
|
|
|
focusNextField(nextField) {
|
|
|
|
this.refs[nextField].focus();
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
ref="1"
|
|
|
|
style={styles.singleLine}
|
|
|
|
placeholder="blurOnSubmit = false"
|
|
|
|
returnKeyType="next"
|
|
|
|
blurOnSubmit={false}
|
|
|
|
onSubmitEditing={() => this.focusNextField('2')}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
ref="2"
|
|
|
|
style={styles.singleLine}
|
|
|
|
keyboardType="email-address"
|
|
|
|
placeholder="blurOnSubmit = false"
|
|
|
|
returnKeyType="next"
|
|
|
|
blurOnSubmit={false}
|
|
|
|
onSubmitEditing={() => this.focusNextField('3')}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
ref="3"
|
|
|
|
style={styles.singleLine}
|
|
|
|
keyboardType="url"
|
|
|
|
placeholder="blurOnSubmit = false"
|
|
|
|
returnKeyType="next"
|
|
|
|
blurOnSubmit={false}
|
|
|
|
onSubmitEditing={() => this.focusNextField('4')}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
ref="4"
|
|
|
|
style={styles.singleLine}
|
|
|
|
keyboardType="numeric"
|
|
|
|
placeholder="blurOnSubmit = false"
|
|
|
|
blurOnSubmit={false}
|
|
|
|
onSubmitEditing={() => this.focusNextField('5')}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
ref="5"
|
|
|
|
style={styles.singleLine}
|
|
|
|
keyboardType="numbers-and-punctuation"
|
|
|
|
placeholder="blurOnSubmit = true"
|
|
|
|
returnKeyType="done"
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-14 14:35:58 +00:00
|
|
|
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,
|
|
|
|
},
|
2015-11-06 16:22:22 +00:00
|
|
|
hashtag: {
|
|
|
|
color: 'blue',
|
|
|
|
fontWeight: 'bold',
|
|
|
|
},
|
2015-09-14 14:35:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
exports.title = '<TextInput>';
|
|
|
|
exports.description = 'Single and multi-line text inputs.';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'Auto-focus',
|
|
|
|
render: function() {
|
2016-02-04 13:12:36 +00:00
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
autoFocus={true}
|
|
|
|
style={styles.singleLine}
|
|
|
|
accessibilityLabel="I am the accessibility label for text input"
|
|
|
|
/>
|
|
|
|
);
|
2015-09-14 14:35:58 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Live Re-Write (<sp> -> '_')",
|
|
|
|
render: function() {
|
|
|
|
return <RewriteExample />;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Auto-capitalize',
|
|
|
|
render: function() {
|
|
|
|
var autoCapitalizeTypes = [
|
|
|
|
'none',
|
|
|
|
'sentences',
|
|
|
|
'words',
|
|
|
|
'characters',
|
|
|
|
];
|
|
|
|
var examples = autoCapitalizeTypes.map((type) => {
|
|
|
|
return (
|
|
|
|
<TextInput
|
2015-10-09 16:31:51 +00:00
|
|
|
key={type}
|
2015-09-14 14:35:58 +00:00
|
|
|
autoCapitalize={type}
|
|
|
|
placeholder={'autoCapitalize: ' + type}
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return <View>{examples}</View>;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Auto-correct',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
autoCorrect={true}
|
|
|
|
placeholder="This has autoCorrect"
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
autoCorrect={false}
|
|
|
|
placeholder="This does not have autoCorrect"
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Keyboard types',
|
|
|
|
render: function() {
|
|
|
|
var keyboardTypes = [
|
|
|
|
'default',
|
|
|
|
'email-address',
|
|
|
|
'numeric',
|
2016-01-14 19:41:10 +00:00
|
|
|
'phone-pad',
|
2015-09-14 14:35:58 +00:00
|
|
|
];
|
|
|
|
var examples = keyboardTypes.map((type) => {
|
|
|
|
return (
|
|
|
|
<TextInput
|
2015-10-09 16:31:51 +00:00
|
|
|
key={type}
|
2015-09-14 14:35:58 +00:00
|
|
|
keyboardType={type}
|
|
|
|
placeholder={'keyboardType: ' + type}
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
return <View>{examples}</View>;
|
|
|
|
}
|
|
|
|
},
|
2016-03-02 15:06:37 +00:00
|
|
|
{
|
|
|
|
title: 'Blur on submit',
|
|
|
|
render: function(): ReactElement { return <BlurOnSubmitExample />; },
|
|
|
|
},
|
2015-09-14 14:35:58 +00:00
|
|
|
{
|
|
|
|
title: 'Event handling',
|
|
|
|
render: function(): ReactElement { return <TextEventsExample />; },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Colors and text inputs',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
style={[styles.singleLine]}
|
|
|
|
defaultValue="Default color text"
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={[styles.singleLine, {color: 'green'}]}
|
|
|
|
defaultValue="Green Text"
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
placeholder="Default placeholder text color"
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
placeholder="Red placeholder text color"
|
|
|
|
placeholderTextColor="red"
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
placeholder="Default underline color"
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
placeholder="Blue underline color"
|
|
|
|
style={styles.singleLine}
|
|
|
|
underlineColorAndroid="blue"
|
|
|
|
/>
|
2015-09-29 16:12:19 +00:00
|
|
|
<TextInput
|
|
|
|
defaultValue="Same BackgroundColor as View "
|
|
|
|
style={[styles.singleLine, {backgroundColor: 'rgba(100, 100, 100, 0.3)'}]}>
|
|
|
|
<Text style={{backgroundColor: 'rgba(100, 100, 100, 0.3)'}}>
|
|
|
|
Darker backgroundColor
|
|
|
|
</Text>
|
|
|
|
</TextInput>
|
2016-02-03 13:48:31 +00:00
|
|
|
<TextInput
|
|
|
|
defaultValue="Highlight Color is red"
|
|
|
|
selectionColor={'red'}
|
|
|
|
style={styles.singleLine}>
|
|
|
|
</TextInput>
|
2015-09-14 14:35:58 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Text input, themes and heights',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
placeholder="If you set height, beware of padding set from themes"
|
|
|
|
style={[styles.singleLineWithHeightTextInput]}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Passwords',
|
|
|
|
render: function() {
|
|
|
|
return (
|
2016-03-21 20:47:23 +00:00
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
defaultValue="iloveturtles"
|
|
|
|
secureTextEntry={true}
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
2016-04-06 16:20:39 +00:00
|
|
|
<TextInput
|
2016-03-21 20:47:23 +00:00
|
|
|
secureTextEntry={true}
|
|
|
|
style={[styles.singleLine, {color: 'red'}]}
|
|
|
|
placeholder="color is supported too"
|
|
|
|
placeholderTextColor="red"
|
|
|
|
/>
|
|
|
|
</View>
|
2015-09-14 14:35:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Editable',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
defaultValue="Can't touch this! (>'-')> ^(' - ')^ <('-'<) (>'-')> ^(' - ')^"
|
|
|
|
editable={false}
|
|
|
|
style={styles.singleLine}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Multiline',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput
|
|
|
|
autoCorrect={true}
|
|
|
|
placeholder="multiline, aligned top-left"
|
|
|
|
placeholderTextColor="red"
|
|
|
|
multiline={true}
|
2016-01-21 19:07:50 +00:00
|
|
|
style={[styles.multiline, {textAlign: "left", textAlignVertical: "top"}]}
|
2015-09-14 14:35:58 +00:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
autoCorrect={true}
|
|
|
|
placeholder="multiline, aligned center"
|
|
|
|
placeholderTextColor="green"
|
|
|
|
multiline={true}
|
2016-01-21 19:07:50 +00:00
|
|
|
style={[styles.multiline, {textAlign: "center", textAlignVertical: "center"}]}
|
2015-09-14 14:35:58 +00:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
autoCorrect={true}
|
|
|
|
multiline={true}
|
2016-01-21 19:07:50 +00:00
|
|
|
style={[styles.multiline, {color: 'blue'}, {textAlign: "right", textAlignVertical: "bottom"}]}>
|
2015-09-14 14:35:58 +00:00
|
|
|
<Text style={styles.multiline}>multiline with children, aligned bottom-right</Text>
|
|
|
|
</TextInput>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Fixed number of lines',
|
|
|
|
platform: 'android',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TextInput numberOfLines={2}
|
|
|
|
multiline={true}
|
|
|
|
placeholder="Two line input"
|
|
|
|
/>
|
|
|
|
<TextInput numberOfLines={5}
|
|
|
|
multiline={true}
|
|
|
|
placeholder="Five line input"
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2016-01-25 13:45:44 +00:00
|
|
|
{
|
|
|
|
title: 'Auto-expanding',
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<AutoExpandingTextInput
|
|
|
|
placeholder="height increases with content"
|
|
|
|
enablesReturnKeyAutomatically={true}
|
|
|
|
returnKeyType="done"
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
2015-11-06 16:22:22 +00:00
|
|
|
{
|
|
|
|
title: 'Attributed text',
|
|
|
|
render: function() {
|
|
|
|
return <TokenizedTextExample />;
|
|
|
|
}
|
|
|
|
},
|
2015-09-14 14:35:58 +00:00
|
|
|
];
|