这是我能够提出的最好的,但用户有时能够比代码能够更快地输入内容。
有更好的方法吗?
代码
import React, { Component } from 'react';
import { Text, TextInput, View, StyleSheet } from 'react-native';
export default class App extends Component {
state = {
inputText: '',
};
replaceIllegalCharsWithDash = (value) => {
const illegalCharacters = ' ,' // space and comma.
const re = new RegExp(`[${illegalCharacters}]+`, 'g');
this.setState({ inputText: value.replace(re, '-') })
}
render() {
return (
<View style={styles.container}>
<Text>Spaces and commas will be replaced with dashes.</Text>
<TextInput
style={styles.inputInternalContainer}
value={this.state.inputText}
onChangeText={this.replaceIllegalCharsWithDash}
autoFocus
blurOnSubmit={false}
returnKeyType="next"
enablesReturnKeyAutomatically
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ecf0f1',
},
inputInternalContainer: {
backgroundColor: 'rgba(0, 0, 0, 0.1)',
height: 40,
width: '100%',
fontSize: 20,
textAlign: 'center',
textAlignVertical: 'center',
marginTop: 10,
},
});