Skip to content

Commit

Permalink
Merge pull request #62 from dxw/render-colour-checkboxes
Browse files Browse the repository at this point in the history
Render colour checkboxes
  • Loading branch information
yndajas authored Feb 20, 2024
2 parents b17f4d9 + 455a8e9 commit d324101
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
55 changes: 55 additions & 0 deletions client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,61 @@ <h2>Players</h2>
<p id="number"></p>
</section>

<section id="colour-section"></section>

<template id="checkbox-template">
<form id="checkbox-form">
<fieldset>
<legend>Choose your colour:</legend>
<div>
<input type="checkbox" id="black" />
<label for="black">black</label>
</div>
<div>
<input type="checkbox" id="blue" />
<label for="blue">blue</label>
</div>
<div>
<input type="checkbox" id="brown" />
<label for="brown">brown</label>
</div>
<div>
<input type="checkbox" id="green" />
<label for="green">green</label>
</div>
<div>
<input type="checkbox" id="grey" />
<label for="grey">grey</label>
</div>
<div>
<input type="checkbox" id="orange" />
<label for="orange">orange</label>
</div>
<div>
<input type="checkbox" id="pink" />
<label for="pink">pink</label>
</div>
<div>
<input type="checkbox" id="purple" />
<label for="purple">purple</label>
</div>
<div>
<input type="checkbox" id="red" />
<label for="red">red</label>
</div>
<div>
<input type="checkbox" id="white" />
<label for="white">white</label>
</div>
<div>
<input type="checkbox" id="yellow" />
<labelfor="yellow">yellow</label>
</div>
</fieldset>
<button type="submit">Submit</button>
</form>
</template>

<script
src="https://cdn.socket.io/4.1.2/socket.io.min.js"
crossorigin="anonymous"
Expand Down
8 changes: 8 additions & 0 deletions client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ const askAQuestion = (data: Question): void => {
numberHtml.innerText = number.toString();
};

const renderColourCheckboxes = (): void => {
const colourSection = getElementById("colour-section");
const template = getElementById<HTMLTemplateElement>("checkbox-template");
const clone = template.content.cloneNode(true) as DocumentFragment;
colourSection.appendChild(clone);
};

const derenderNameForm = (): void => {
getElementById("name-form").remove();
};
Expand Down Expand Up @@ -82,6 +89,7 @@ socket.on("player:set", (data) => {
socket.on("question:get", (data) => {
derenderStartButton();
askAQuestion(data.question);
renderColourCheckboxes();
});

socket.on("game:startable", () => {
Expand Down
4 changes: 2 additions & 2 deletions client/utils/getElementById.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const getElementById = (id: HTMLElement["id"]): HTMLElement => {
const element = document.getElementById(id);
export const getElementById = <T extends HTMLElement>(id: T["id"]): T => {
const element = document.getElementById(id) as T;

if (!element) {
throw new Error(`No element found with ID: ${id}`);
Expand Down

0 comments on commit d324101

Please sign in to comment.