ํ์ฌ ์ธํ๋ฐ์์ ๊ฐ์๋ฅผ ๋ค์ผ๋ฉด์ React-Native์ typescript๋ฅผ ๊ณต๋ถํ๊ณ ์์ต๋๋ค.
์ค๋์ ๋ก๊ทธ์ธ, ํ์๊ฐ์ ํ์ด์ง๋ฅผ ๋ง๋ค์ด๋ณด๋ฉด์ Style๊ณผ TexInput์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์์ต๋๋ค.
RN - TextInput ๊ณต์๋ฌธ์
https://reactnative.dev/docs/textinput
import React, {useCallback, useState, useRef} from 'react';
import {
View,
Text,
Pressable,
StyleSheet,
TextInput,
Alert,
KeyboardAvoidingView
} from 'react-native';
import {RootStackParamList} from '../../App';
function SignIn({navigation}: RootStackParamList) {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const canGoNext = !!email && !!password;
const emailRef = useRef<TextInput | null>(null);
const passwordRef = useRef<TextInput | null>(null);
const changeEmail = useCallback(text => {
setEmail(text);
}, []);
const changePassword = useCallback(text => {
setPassword(text);
}, []);
const onSubmit = useCallback(() => {
if (canGoNext) {
Alert.alert('Hi', 'Hello');
} else {
Alert.alert('no!', 'GoodBye');
}
}, [email, password]);
const goToSignUp = useCallback(() => {
navigation.navigate('SignUp');
}, [navigation]);
return (
<KeyboardAvoidingView behaivor="padding">
<View style={styles.inputZoone}>
<View>
<Text style={styles.label}>์ด๋ฉ์ผ</Text>
<TextInput
style={styles.textInput}
value={email}
autoFocus
autoComplete={'email'}
textContentType="emailAddress"
importantForAutofill="yes"
onChangeText={changeEmail}
placeholder="์ด๋ฉ์ผ์ ์
๋ ฅ"
blurOnSubmit={false}
keyboardType="email-address"
onSubmitEditing={() => {
passwordRef.current?.focus();
}}
ref={emailRef}
/>
</View>
<View>
<Text style={styles.label}>๋น๋ฐ๋ฒํธ</Text>
<TextInput
style={styles.textInput}
value={password}
secureTextEntry
importantForAutofill="yes"
onChangeText={changePassword}
placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅ"
ref={passwordRef}
onSubmitEditing={() => {
onSubmit();
}}
/>
</View>
</View>
<View style={styles.ButtonZone}>
<Pressable style={styles.loginButton} onPress={onSubmit}>
<Text style={styles.loginButtonFont}>๋ก๊ทธ์</Text>
</Pressable>
<Pressable style={styles.signupButton} onPress={goToSignUp}>
<Text style={styles.loginButtonFont}>ํ์๊ฐ์
</Text>
</Pressable>
</View>
</KeyboardAvoidingView>
);
}
const styles = StyleSheet.create({
loginButton: {
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 5,
marginBottom: 10,
},
signupButton: {
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 5,
},
loginButtonFont: {
fontSize: 14,
color: '#0ba2ba',
},
ButtonZone: {
alignItems: 'center',
},
textInput: {
marginHorizontal: 10,
fontSize: 15,
margin: 10,
paddingVertical: 20,
},
label: {
fontWeight: '600',
color: 'black',
fontSize: 17,
marginHorizontal: 10,
},
inputZoone: {
paddingHorizontal: 20,
paddingTop: 40,
paddingBottom: 20,
},
});
export default SignIn;
๋ก๊ทธ์ธ ํ๋ฉด
import React, {useCallback, useState, useRef} from 'react';
import {
View,
Text,
Pressable,
StyleSheet,
TextInput,
Alert,
KeyboardAvoidingView,
} from 'react-native';
import {RootStackParamList} from '../../App';
function Signup({navigation}: RootStackParamList) {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [name, setName] = useState('');
const canGoNext = !!email && !!password;
const emailRef = useRef<TextInput | null>(null);
const nameRef = useRef<TextInput | null>(null);
const passwordRef = useRef<TextInput | null>(null);
const changeEmail = useCallback(text => {
setEmail(text);
}, []);
const changeName = useCallback(text => {
setName(text);
}, []);
const changePassword = useCallback(text => {
setPassword(text);
}, []);
const onSubmit = useCallback(() => {
if (canGoNext) {
Alert.alert('Hi', 'dsds');
} else {
Alert.alert('no!', 'dsds');
}
}, [email, password, name]);
return (
<KeyboardAvoidingView behavior="padding">
<View style={styles.inputZoone}>
<View>
<Text style={styles.label}>์ด๋ฉ์ผ</Text>
<TextInput
style={styles.textInput}
value={email}
autoFocus
autoComplete={'email'}
textContentType="emailAddress"
importantForAutofill="yes"
onChangeText={changeEmail}
placeholder="์ด๋ฉ์ผ์ ์
๋ ฅ"
blurOnSubmit={false}
keyboardType="email-address"
onSubmitEditing={() => {
nameRef.current?.focus();
}}
ref={emailRef}
/>
</View>
<View>
<Text style={styles.label}>์ด๋ฆ</Text>
<TextInput
style={styles.textInput}
value={name}
textContentType="emailAddress"
importantForAutofill="yes"
onChangeText={changeName}
placeholder="์ด๋ฆ์ ์
๋ ฅ"
blurOnSubmit={false}
onSubmitEditing={() => {
passwordRef.current?.focus();
}}
ref={nameRef}
/>
</View>
<View>
<Text style={styles.label}>๋น๋ฐ๋ฒํธ</Text>
<TextInput
style={styles.textInput}
value={password}
secureTextEntry
importantForAutofill="yes"
onChangeText={changePassword}
placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅ"
ref={passwordRef}
onSubmitEditing={() => {
onSubmit();
}}
/>
</View>
</View>
<View style={styles.ButtonZone}>
<Pressable style={styles.signupButton} onPress={onSubmit}>
<Text style={styles.loginButtonFont}>ํ์๊ฐ์
</Text>
</Pressable>
</View>
</KeyboardAvoidingView>
);
}
const styles = StyleSheet.create({
loginButton: {
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 5,
marginBottom: 10,
},
signupButton: {
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 5,
},
loginButtonFont: {
fontSize: 14,
color: '#0ba2ba',
},
ButtonZone: {
alignItems: 'center',
},
textInput: {
marginHorizontal: 10,
fontSize: 15,
margin: 10,
paddingVertical: 20,
},
label: {
fontWeight: '600',
color: 'black',
fontSize: 17,
marginHorizontal: 10,
},
inputZoone: {
paddingHorizontal: 20,
paddingTop: 40,
paddingBottom: 20,
},
});
export default Signup;
ํ์๊ฐ์ ํ์ด์ง