代码之家  ›  专栏  ›  技术社区  ›  Nimish Bansal

在焦点上对外部库中的本机textfield组件作出反应

  •  0
  • Nimish Bansal  · 技术社区  · 6 年前

    我有一个寄存器组件,它有来自外部库的phoneinput组件 https://github.com/thegamenicorus/react-native-phone-input 问题是,我希望在PhoneInput内的文本字段集中后立即执行一些代码。

    在检查库的文档时。我看到一个名为textcomponent的属性。可用于对PhoneInput组件中的文本字段进行聚焦吗?

    import React from 'react';
    import {Image, StyleSheet, Text, View} from 'react-native';
    import PhoneInput from 'react-native-phone-input'
    
    export default class Register extends React.Component {
        constructor(props){
            super(props);
            this.state = {"phone": ""};
            this.countriesList =  [
                {
                    "name": "India (भारत)",
                    "iso2": "in",
                    "dialCode": "91",
                    "priority": 0,
                    "areaCodes": null
                }];
            this.phoneTextField=null;
        }
        componentDidMount() {
            console.log('GrandChild did mount.');
            // console.log(this.phoneTextField);
        }
        render() {
            console.log(this.phoneTextField);
            console.log("Register Screen");
            return (
                <View style={{borderColor:'red', borderWidth:0, height:'100%' }}>
                    <View style={{borderColor:'green', borderWidth:0, height:'20%' }}>
                        <View style={{borderColor:'blue', borderWidth:0, resizeMode:'contain', width:'50%', height:'100%'}}>
                            <Image
                                style={{width: '100%', height: '100%' }}
                                source={ require("../assets/logo.png") }
                            />
                        </View>
                    </View>
                    <View style={{borderColor:'green', borderWidth:0, height:'40%', marginLeft:'1%', marginTop:'5%' }}>
                        <View style={{borderColor:'blue', borderWidth:0, resizeMode:'contain', width:'100%', height:'100%'}}>
                            <Image
                                style={{width: '100%', height: '100%' }}
                                source={ require("../assets/welcomescreen.jpg") }
                            />
                        </View>
                    </View>
                    <View style={{ margin : '7%'}}>
                        <Text style={{ fontSize:20, fontWeight:'bold' }}>Save your time</Text>
                        <Text style={{ fontSize:14, color:'grey', paddingTop:'5%' }}>Please Enter your mobile number to proceed</Text>
                        <PhoneInput ref={ref => {
                                this.phoneTextField = ref;
                        }}
                                    style={{paddingTop:'10%'}}
                                    countriesList={this.countriesList}
                                    initialCountry='in'
                                    onChangePhoneNumber={(phone)=>{
                                        console.log(phone);
                                    }}
                        />
                    </View>
                </View>
    
    
            );
        }
    }
    
    const styles = StyleSheet.create({
        container: {
            flex: 1,
            backgroundColor: '#fff',
            alignItems: 'center',
            justifyContent: 'center',
        },
    });
    

    我的App.JS

    import React from 'react';
    import { StyleSheet, Text, View } from 'react-native';
    import Register from "./screens/Register";
    
    export default class App extends React.Component {
      render() {
        console.log("hello bros");
        return (
          <Register/>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#fff',
        alignItems: 'center',
        justifyContent: 'center',
      },
    });
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   RegularGuy    6 年前

    我不熟悉这个库,但是,textcomponent接受一个函数,所以您应该能够这样做。

    textComponent={
      ()=>{
      return(<TextInput 
      ....some props here 
      onFocus={()=>console.log('focused')})
      }}
    
    推荐文章