StartGameScreen.js
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useState } from "react";
import { TextInput, View, StyleSheet, Alert } from "react-native";
import PrimaryButton from "../components/ui/PrimaryButton";
import Title from "../components/ui/Title";
import Colors from "../constants/colors";
import Card from "../components/ui/Card";
import InstructionText from "../components/ui/InstructionText";
function StartGameScreen({ onPickNumber }) {
const [enteredNumber, setEnteredNumber] = useState("");
function numberInputHandler(enteredText) {
setEnteredNumber(enteredText);
}
function resetInputHandler() {
setEnteredNumber("");
}
function confirmInputHandler() {
const chosenNumber = parseInt(enteredNumber);
if (isNaN(chosenNumber) || chosenNumber <= 0 || chosenNumber > 99) {
Alert.alert(
"Invalid number!",
"Number has to be a number between 1 and 99.",
[{ text: "Okay", style: "destructive", onPress: resetInputHandler }]
);
return;
}
onPickNumber(chosenNumber);
}
return (
<View style={styles.rootContainer}>
<Title>Guess My Number</Title>
<Card>
<InstructionText>Enter a Number</InstructionText>
<TextInput
style={styles.numberInput}
maxLength={2}
keyboardType="number-pad"
autoCapitalize="none"
autoCorrect={false}
onChangeText={numberInputHandler}
value={enteredNumber}
/>
<View style={styles.buttonsContainer}>
<View style={styles.buttonContainer}>
<PrimaryButton onPress={resetInputHandler}>Reset</PrimaryButton>
</View>
<View style={styles.buttonContainer}>
<PrimaryButton onPress={confirmInputHandler}>Confirm</PrimaryButton>
</View>
</View>
</Card>
</View>
);
}
export default StartGameScreen;
const styles = StyleSheet.create({
rootContainer: {
flex: 1,
marginTop: 100,
alignItems: "center",
},
numberInput: {
height: 50,
width: 50,
fontSize: 32,
borderBottomColor: Colors.accent500,
borderBottomWidth: 2,
color: Colors.accent500,
marginVertical: 8,
fontWeight: "bold",
textAlign: "center",
},
buttonsContainer: {
flexDirection: "row",
},
buttonContainer: {
flex: 1,
},
});