1. ์ฝ๋ ๋ฆฌ๋ทฐ
import React from "react"
import { TouchableOpacity, View, Text } from "react-native"
import styled from "styled-components/native"
const ButtonContainer =styled.View`
flex-direction: row;
width: 100%;
`;
//Button Type : 'reset' | 'Operator' | 'num'
const Button = ({text, onPress, flex, type}) => {
const backgroundColor =
(type === "reset")
? COLOR.RESET
: (type === "operator")
? COLOR.OPERATOR
: (type === "num")
? COLOR.NUM : "transparent"
return (
<TouchableOpacity
onPress ={onPress}
style={{
flex,
backgroundColor,
justifyContent: "center",
alignItems: "center",
height: 50,
borderWidth: 0.5,
borderColor: "black",
}}>
<Text style={{color: "white", fontSize: 15}}>{text}</Text>
</TouchableOpacity>
)
}
const COLOR = {
RESULT : '#4e4c51',
RESET : '#5f5e62',
OPERATOR : '#f39c29',
NUM : '#5c5674',
}
export default ( ) => {
return(
<View style={{flex: 1, width: 250}}>
{/* ๊ฒฐ๊ณผ */}
{/* [AC ~ /] */}
<ButtonContainer>
<Button
type = "reset"
text = "AC"
onPress={() => null}
flex={3}
/>
<Button
type = "operator"
text = "/"
onPress={() => null}
flex={1}
/>
</ButtonContainer>
{/* [7 ~ x] */}
<ButtonContainer>
<Button
type = "num"
text = "7"
onPress={() => null}
flex={1}
/>
<Button
type = "num"
text = "8"
onPress={() => null}
flex={1}
/>
<Button
type = "num"
text = "9"
onPress={() => null}
flex={1}
/>
<Button
type = "operator"
text = "x"
onPress={() => null}
flex={1}
/>
</ButtonContainer>
{/* [4 ~ -] */}
<ButtonContainer>
<Button
type = "num"
text = "4"
onPress={() => null}
flex={1}
/>
<Button
type = "num"
text = "5"
onPress={() => null}
flex={1}
/>
<Button
type = "num"
text = "6"
onPress={() => null}
flex={1}
/>
<Button
type = "operator"
text = "-"
onPress={() => null}
flex={1}
/>
</ButtonContainer>
{/* [1 ~ +] */}
<ButtonContainer>
<Button
type = "num"
text = "1"
onPress={() => null}
flex={1}
/>
<Button
type = "num"
text = "2"
onPress={() => null}
flex={1}
/>
<Button
type = "num"
text = "3"
onPress={() => null}
flex={1}
/>
<Button
type = "operator"
text = "+"
onPress={() => null}
flex={1}
/>
</ButtonContainer>
{/* [0 ~ =] */}
<ButtonContainer>
<Button
type = "num"
text = "0"
onPress={() => null}
flex={3}
/>
<Button
type = "operator"
text = "="
onPress={() => null}
flex={1}
/>
</ButtonContainer>
</View>
)
}
2. ์๊ฒ๋ ์
(1) flex: 1
=> ์์ ์ ๊ณ ์ ํฌ๊ธฐ ์์ด ํ๋ฉด ํฌ๊ธฐ์ ๋ง๊ฒ ๋์ด๋๊ณ ์ค์ด๋ค๊ฒ ๋ค๋ ์๋ฏธ
ํ๋ฉด์ ์ผ๋ง๋ ์ฐจ์งํ ๊ฒ์ธ๊ฐ๋ ํ์ ์ปดํฌ๋ํธ๋ค๊ณผ์ ๋๊ฒฐ
(2) width: "100%"
=> ๋ถ๋ชจ ๋๋น์ 100 ํผ์ผํธ๋ฅผ ์ฐจ์งํ๊ฒ ๋ค.
(3) CSS ์ ๊ฐ๋ค์ ์ธ์๋ก ๋ฐ๋ ๊ฒฝ์ฐ, name : value ํํ๋ก ์ ์จ์ฃผ๊ณ ๊ทธ๋ฅ name๋ง CSS Style์ ์จ์ค๋ ์ ์ฉ์ด ๋๋ค.
const Button = ({text, onPress, flex, type}) => {
const backgroundColor =
(type === "reset")
? COLOR.RESET
: (type === "operator")
? COLOR.OPERATOR
: (type === "num")
? COLOR.NUM : "transparent"
return (
<TouchableOpacity
onPress ={onPress}
style={{
flex,
backgroundColor,
}}>
</TouchableOpacity>
์์ ์์๋ฅผ ๋ณด๋ฉด ๊ตณ์ด {flex: flex, backgroundColor: backGroundColor}๋ผ๊ณ ์ ์จ์คฌ๋ค. ํ๊ทธ ์์ฑ์ ์ด๋ฆ์ CSS ๋ฌธ๋ฒ๊ณผ ๋๊ฐ์ด ํด์ฃผ๋ฉด ์๋์ผ๋ก ๋ค์ด๊ฐ๋ค.
** ์ฃผ์์ ** CSS์ ์์ฑ ์ด๋ฆ๊ณผ ํ๊ทธ์ ์์ฑ ์ด๋ฆ์ ๋๊ฐ์ด ํด์ค์ผ ํ๋ค!