有没有更好的方法实时替换TextInput?

时间:2018-06-01 03:45:37

标签: react-native

这是我能够提出的最好的,但用户有时能够比代码能够更快地输入内容。

有更好的方法吗?

  • 实例< 的 Replace Illegal Characters in Realtime
  • 代码

    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,
      },
    });
    

0 个答案:

没有答案