diff --git a/11-DiceRoller/join0life/DiceController.tsx b/11-DiceRoller/join0life/DiceController.tsx new file mode 100644 index 0000000..a4e2662 --- /dev/null +++ b/11-DiceRoller/join0life/DiceController.tsx @@ -0,0 +1,31 @@ +type DiceControllerProps = { + diceCount: number; + onChange: (e: React.ChangeEvent) => void; + onRoll: () => void; +}; + +export default function DiceController({ + diceCount, + onChange, + onRoll, +}: DiceControllerProps) { + const handleSubmit = (e: React.SubmitEvent) => { + e.preventDefault(); + onRoll(); + }; + + return ( +
+ + + +
+ ); +} diff --git a/11-DiceRoller/join0life/DiceRoller.css b/11-DiceRoller/join0life/DiceRoller.css new file mode 100644 index 0000000..6e66186 --- /dev/null +++ b/11-DiceRoller/join0life/DiceRoller.css @@ -0,0 +1,22 @@ +.dice-roller { + width: 100%; + display: flex; + flex-direction: column; + align-items: center; + gap: 1rem; +} + +.dice-controller { + display: flex; + gap: 0.5rem; +} + +.dices { + display: grid; + grid-template-columns: 1fr 1fr 1fr; + gap: 1rem; +} + +.dice { + font-size: 5rem; +} diff --git a/11-DiceRoller/join0life/DiceRoller.tsx b/11-DiceRoller/join0life/DiceRoller.tsx new file mode 100644 index 0000000..8b36836 --- /dev/null +++ b/11-DiceRoller/join0life/DiceRoller.tsx @@ -0,0 +1,33 @@ +import DiceController from "./DiceController"; +import Dices from "./Dices"; +import "./DiceRoller.css"; +import { useState } from "react"; + +export default function DiceRoller() { + const [inputCount, setInputCount] = useState(1); + const [dices, setDices] = useState([]); + + const handleInputChange = (e: React.ChangeEvent) => { + const value = e.target.value; + setInputCount(value === "" ? 1 : Number(value)); + }; + + const handleRoll = () => { + const randomDices = Array.from({ length: inputCount }, () => + Math.floor(Math.random() * 6), + ); + + setDices(randomDices); + }; + + return ( +
+ + +
+ ); +} diff --git a/11-DiceRoller/join0life/Dices.tsx b/11-DiceRoller/join0life/Dices.tsx new file mode 100644 index 0000000..9846c3b --- /dev/null +++ b/11-DiceRoller/join0life/Dices.tsx @@ -0,0 +1,19 @@ +type DicesProps = { + dices: number[]; +}; + +const RANDOM_DICES = ["⚀", "⚁", "⚂", "⚃", "⚄", "⚅"]; + +export default function Dices({ dices }: DicesProps) { + return ( +
+ {dices.map((dice, index) => ( + + ))} +
+ ); +} + +export function Dice({ value }: { value: number }) { + return
{RANDOM_DICES[value]}
; +}