From 675c742d693f19a0000f7fed24d552ed7a823a93 Mon Sep 17 00:00:00 2001 From: Adam Lorek Date: Thu, 6 Jun 2019 13:56:02 +0200 Subject: [PATCH] dice to component --- src/components/Dice.tsx | 81 ++++++++++++++++++++++++++++++++++++++ src/screens/DiceScreen.tsx | 81 ++------------------------------------ 2 files changed, 84 insertions(+), 78 deletions(-) create mode 100644 src/components/Dice.tsx diff --git a/src/components/Dice.tsx b/src/components/Dice.tsx new file mode 100644 index 0000000..4de3247 --- /dev/null +++ b/src/components/Dice.tsx @@ -0,0 +1,81 @@ +import React, { useState } from 'react'; +import { View } from 'react-native'; +import { + Button, Text, Switch, Colors, +} from 'react-native-paper'; +import NumberInput from './NumberInput'; + +interface State { + max: number; + min: number; + roll: number | null; + minInput: boolean; +} + +const initialState = { + max: 6, + min: 1, + roll: null, + minInput: false, +}; + +export default function Dice() { + const [max, setMax] = useState(initialState.max); + const [min, setMin] = useState(initialState.min); + const [roll, setRoll] = useState(initialState.roll); + const [minInput, setMinInput] = useState(initialState.minInput); + + function updateMax(max: State['max']) { + setMax(max); + } + + function updateMin(min: State['min']) { + setMin(min); + } + + function updateRoll() { + setRoll(Math.floor(Math.random() * (max - min + 1)) + min); + } + + function updateMinInput() { + setMinInput(!minInput); + } + + function resetAll() { + setMax(initialState.max); + setMin(initialState.min); + setRoll(initialState.roll); + setMinInput(initialState.minInput); + } + + return ( + + + + + {minInput && ( + + Set minimum value + + + )} + Set maximum value + + {roll && ( + + You rolled: + {roll} + + )} + + ); +} diff --git a/src/screens/DiceScreen.tsx b/src/screens/DiceScreen.tsx index 02785ea..a9b1c47 100644 --- a/src/screens/DiceScreen.tsx +++ b/src/screens/DiceScreen.tsx @@ -1,81 +1,6 @@ -import React, { useState } from 'react'; -import { View } from 'react-native'; -import { - Button, Text, Switch, Colors, -} from 'react-native-paper'; -import NumberInput from '../components/NumberInput'; - -interface State { - max: number; - min: number; - roll: number | null; - minInput: boolean; -} - -const initialState = { - max: 6, - min: 1, - roll: null, - minInput: false, -}; +import React from 'react'; +import Dice from '../components/Dice'; export default function DiceScreen() { - const [max, setMax] = useState(initialState.max); - const [min, setMin] = useState(initialState.min); - const [roll, setRoll] = useState(initialState.roll); - const [minInput, setMinInput] = useState(initialState.minInput); - - function updateMax(max: State['max']) { - setMax(max); - } - - function updateMin(min: State['min']) { - setMin(min); - } - - function updateRoll() { - setRoll(Math.floor(Math.random() * (max - min + 1)) + min); - } - - function updateMinInput() { - setMinInput(!minInput); - } - - function resetAll() { - setMax(initialState.max); - setMin(initialState.min); - setRoll(initialState.roll); - setMinInput(initialState.minInput); - } - - return ( - - - - - {minInput && ( - - Set minimum value - - - )} - Set maximum value - - {roll && ( - - You rolled: - {roll} - - )} - - ); + return ; }