It took me a while to figure this out, but I still feel like my implementation is not ideal. I'm confused as to why things break when I remove the
checkedActivities.has(activity) ? "checked" : "unchecked"
, because I thought TypeScript was supposed to handle this functionality. Any advice on this would be greatly appreciated.
<script context="module" lang="ts">
import userActivities from "../userActivities.json";
const activityArray: string[] = userActivities.activities.map(
([name, icon]) => name
);
export let selectedActivities: String[] = activityArray.slice(0, 3);
let checkedActivities = new Set(activityArray.slice(0, 3));
function toggleActivitySelection(activity: string) {
let div = document.getElementById(activity);
if (checkedActivities.has(activity)) {
checkedActivities.delete(activity);
div?.classList.remove("checked");
div?.classList.add("unchecked");
} else {
checkedActivities.add(activity);
div?.classList.remove("unchecked");
div?.classList.add("checked");
}
console.log(checkedActivities);
}
</script>
<div>
<div class="heading-bar">
<span>Saved activities</span>
<form action="/activities">
<button on:click={() => console.log(selectedActivities)}
>Done</button
>
</form>
</div>
<div class="bg-container">
{#each activityArray as activity}
<div
class={checkedActivities.has(activity) ? "checked" : "unchecked"}
id={activity}
>
<label>
<input
class="input_checkbox"
id="input_checkbox"
type="checkbox"
bind:group={selectedActivities}
value={activity}
on:change={() => {
toggleActivitySelection(activity);
}}
/>
{activity}
</label>
</div>
{/each}
</div>
</div>
<style>
input[type="checkbox"] {
width: 100%;
height: 100%;
display: none;
}
label {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
div.checked:hover, div.unchecked:hover {
border-left: 2px solid var(--color-text) !important;
border-bottom: 2px solid var(--color-objects) !important;
border-right: 2px solid var(--color-text) !important;
border-top: 2px solid var(--color-objects) !important;
}
div.checked {
padding-top: 0.5em;
padding-bottom: 0.5em;
border-radius: 20px;
margin: 0.5em 0;
border-left: 2px solid var(--color-text) !important;
border-bottom: 2px solid var(--color-objects) !important;
border-right: 2px solid var(--color-text) !important;
border-top: 2px solid var(--color-objects) !important;
}
div.unchecked {
padding-top: 0.5em;
padding-bottom: 0.5em;
border-radius: 20px;
margin: 0.5em 0;
border-left: 2px solid var(--color-objects) !important;
border-bottom: 2px solid var(--color-objects) !important;
border-right: 2px solid var(--color-objects) !important;
border-top: 2px solid var(--color-objects) !important;
}
.heading-bar {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-top: 1.8em;
margin-bottom: 0.6em;
}
.bg-container {
border-radius: 12px;
background-color: var(--color-objects);
padding: 1em 1.4em;
}
#item {
display: flex;
align-items: center;
justify-content: center;
}
button {
background-color: var(--color-objects);
border: none;
color: var(--color-text);
font-size: 1em;
border-radius: 10px;
padding: 0.5em 0.5em;
}
</style>