Adjust how I track tiles in React

This commit is contained in:
Joel Therrien 2023-08-07 21:41:46 -07:00
parent f8e99ea02f
commit 69eb2060fc
3 changed files with 34 additions and 12 deletions

View file

@ -1,6 +1,17 @@
import * as React from "react"; import * as React from "react";
import {Letter as LetterData} from "word_grid"; import {Letter as LetterData} from "word_grid";
export enum LocationType {
GRID,
TRAY
}
export interface CoordinateData {
location: LocationType;
index: number;
}
export type PlayableLetterData = LetterData & CoordinateData;
export function Letter(props: { data: LetterData }): React.JSX.Element { export function Letter(props: { data: LetterData }): React.JSX.Element {
return <div className="letter"> return <div className="letter">
@ -9,16 +20,21 @@ export function Letter(props: { data: LetterData }): React.JSX.Element {
</div> </div>
} }
export function TileTray(props: { letters: Array<LetterData> }): React.JSX.Element {
let elements = props.letters.map((ld, i) => { export function TileTray(props: { letters: Array<PlayableLetterData>, trayLength: number }): React.JSX.Element {
if (ld === undefined) {
return <div key={i}></div>; let elements: JSX.Element[] = [];
} else { for (let i=0; i<props.trayLength; i++) {
return <Letter data={ld} key={i}/> elements.push(
<div key={"empty" + i}></div>
);
}
for (let letter of props.letters) {
if (letter.location === LocationType.TRAY) {
elements[letter.index] = <Letter data={letter} key={"letter" + letter.index} />
}
} }
})
.concat();
return ( return (
<div className="tray"> <div className="tray">

View file

@ -1,5 +1,5 @@
import init, {greet, GameWasm} from '../node_modules/word_grid/word_grid.js'; import init, {greet, GameWasm} from '../node_modules/word_grid/word_grid.js';
import {TileTray} from "./elements"; import {LocationType, PlayableLetterData, TileTray} from "./elements";
import {createRoot} from "react-dom/client"; import {createRoot} from "react-dom/client";
import {Tray} from "word_grid"; import {Tray} from "word_grid";
@ -37,12 +37,17 @@ async function run() {
let game = new GameWasm(BigInt(1234)); let game = new GameWasm(BigInt(1234));
let tray: Tray = game.get_tray(); let tray: Tray = game.get_tray();
// initial state
let letters: PlayableLetterData[] = tray.letters.map((ld, i) => {
ld["location"] = LocationType.TRAY;
ld["index"] = i;
return ld as PlayableLetterData;
})
console.log({tray}); console.log({tray});
const root = createRoot(document.getElementById("root")); const root = createRoot(document.getElementById("root"));
root.render(<TileTray letters={tray.letters}/>); root.render(<TileTray letters={letters} trayLength={7}/>);
// And afterwards we can use all the functionality defined in wasm.
} }

View file

@ -14,6 +14,7 @@
.letter { .letter {
background-color: #e5cca9; background-color: #e5cca9;
position: relative; // Used for the positioning of the sub-components position: relative; // Used for the positioning of the sub-components
user-select: none;
.text { .text {
position: absolute; position: absolute;