import * as React from 'react' import {useState, useEffect, useRef} from 'react' import {View, Text, TouchableOpacity, TextInput, StyleSheet, Keyboard} from 'react-native' import { checkPhoneNumberInput } from '../utils/number.js' import Theme from './theme.style.js' export default function CustomInput(props){ const [focus, setfocus] = useState(false) const [activefield, setactivefield] = useState(null) const [activeInput, setactiveinput] = useState(null) const [value, setvalue] = useState(null) const inputs = {} let [cn1, setcn1] = useState("") let [cn2, setcn2] = useState("") let [cn3, setcn3] = useState("") let [cn4, setcn4] = useState("") let [mobilenumber, setmobilenumber] = useState("+63") const send = () => { props.onChangeText(value) } const onFocus = (key) => { setactiveinput(key) setfocus(true) } const onSubmitEditing = (key) => { inputs[key].focus() } if(props.isMobileNumber) { return ( { nativeEvent.key === 'Backspace' && mobilenumber.length === 3 }} onChangeText={(value) => { const checker = checkPhoneNumberInput(value); if(!checker) return; if(value.substring(0, 3) == "+63") { setmobilenumber(value) props.onChangeText(value) } }} onSubmitEditing={Keyboard.dismiss} style={[{...styles.input, borderBottomColor: focus && activeInput == 1 ? Theme.colors.accent : "gray", borderBottomWidth: focus && activeInput == 1 ? 1.5 : 1, textAlign: 'left' }, { color: props.textColor || Theme.colors.black}]} /> ) } return ( inputs['field1'] = input} returnKeyType={ 'next' } onSubmitEditing={() => { onSubmitEditing('field2'); }} blurOnSubmit={ false } keyboardType="numeric" maxLength={4} onFocus={() => onFocus(1)} onKeyPress={({ nativeEvent }) => { if(cn1.length == 4 && nativeEvent.key != "Backspace") onSubmitEditing("field2") }} value={cn1} onChangeText={(value) => { setcn1(value) // setvalue(value + cn2 + cn3 + cn4) // send() props.onChangeText(value + cn2 + cn3 + cn4) if(value.length == 4) onSubmitEditing("field2") }} style={[{...styles.input, borderBottomColor: focus && activeInput == 1 ? Theme.colors.accent : "gray", borderBottomWidth: focus && activeInput == 1 ? 1.5 : 1 }, { color: props.textColor || Theme.colors.black}]} /> inputs['field2'] = input} returnKeyType={ 'next' } onSubmitEditing={() => { onSubmitEditing('field3'); }} keyboardType="numeric" maxLength={4} blurOnSubmit={ false } onFocus={() => onFocus(2)} value={cn2} onKeyPress={({ nativeEvent }) => { nativeEvent.key === 'Backspace' && cn2 === "" ? onSubmitEditing("field1") : null if(cn2.length == 4 && nativeEvent.key != "Backspace") onSubmitEditing("field3") }} onChangeText={(value) => { setcn2(value) // setvalue(cn1 + value + cn3 + cn4) // send() props.onChangeText(cn1 + value + cn3 + cn4) if(value.length == 4) onSubmitEditing("field3") else if(value == "" || value == "" && cn2 == "") onSubmitEditing("field1") }} style={[{...styles.input, borderBottomColor: focus && activeInput == 2 ? Theme.colors.accent : "gray", borderBottomWidth: focus && activeInput == 2 ? 1.5 : 1 }, { color: props.textColor || Theme.colors.black}]} /> inputs['field3'] = input} returnKeyType={ 'next' } onSubmitEditing={() => { onSubmitEditing('field4'); }} keyboardType="numeric" maxLength={4} blurOnSubmit={ false } onFocus={() => onFocus(3)} value={cn3} onKeyPress={({ nativeEvent }) => { nativeEvent.key === 'Backspace' && cn3 === "" ? onSubmitEditing("field2") : null if(cn3.length == 4 && nativeEvent.key != "Backspace") onSubmitEditing("field4") }} onChangeText={(value) => { setcn3(value) // setvalue(cn1 + cn2 + value + cn4) // send() props.onChangeText(cn1 + cn2 + value + cn4) if(value.length == 4) onSubmitEditing("field4") else if(value == "") onSubmitEditing("field2") }} style={[{...styles.input, borderBottomColor: focus && activeInput == 3 ? Theme.colors.accent : "gray", borderBottomWidth: focus && activeInput == 3 ? 1.5 : 1 }, { color: props.textColor || Theme.colors.black}]} /> inputs['field4'] = input} keyboardType="numeric" maxLength={4} blurOnSubmit={ false } onFocus={() => onFocus(4)} value={cn4} onKeyPress={({ nativeEvent }) => { nativeEvent.key === 'Backspace' && cn4 === "" ? onSubmitEditing("field3") : null }} onChangeText={(val) => { setcn4(val) // setvalue(cn1 + cn2 + cn3 + cn4) // send() props.onChangeText(cn1 + cn2 + cn3 + val) if(val == "") onSubmitEditing("field3") if(val.length == 4){ // setvalue(value + val) // alert(value + " / " + val) // alert(value) // console.log(value) // props.onDone(value + val) } // else{ // setvalue(cn1 + cn2 + cn3 + cn4) // } // if(value.length == 4) props.onDone() }} style={[{...styles.input, borderBottomColor: focus && activeInput == 4 ? Theme.colors.accent : "gray", borderBottomWidth: focus && activeInput == 4 ? 1.5 : 1 }, { color: props.textColor || Theme.colors.black}]} /> ) } const styles = StyleSheet.create({ input: { flex: 1, padding: 0, margin: 5, fontSize: 18, textAlign: 'center', borderBottomColor: Theme.colors.accent, borderBottomWidth: 1.5, color: Theme.colors.white } });