[2022-02-22 ๐Ÿค TIL ]

Burkeyยท2022๋…„ 2์›” 22์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
1/157

ํ˜„์žฌ ์ธํ”„๋Ÿฐ์—์„œ ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉด์„œ 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;

ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€

profile
์Šคํƒฏ ์˜ฌ๋ฆฌ๋Š” ์ค‘

0๊ฐœ์˜ ๋Œ“๊ธ€