Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Feature/design system checkboxes - ticket#85 #65

Merged
merged 4 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/data/test/mockDSData.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ let ethnicity=[
{id:"asian",value:"asian",label:"Asian"},
]

export {ethnicity}
let checkboxData=[
{id:"laptop", value:"laptop", label:"Laptop"},
{id:"mobile", value:"mobile", label:"Mobile"},
{id:"tablet", value:"tablet", label:"Tablet"},
]
export {ethnicity,checkboxData}
89 changes: 79 additions & 10 deletions src/routes/DesignSystemApp.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { ethnicity } from "../data/test/mockDSData";
import { ethnicity, checkboxData } from "../data/test/mockDSData";
import ONSCensusApp from "../ui/ons/ONSCensusApp.svelte";
import ONSAccordion from "../ui/ons/ONSAccordion.svelte";
import ONSAccordionPanel from "../ui/ons/partials/ONSAccordionPanel.svelte";
Expand Down Expand Up @@ -28,6 +28,8 @@
let radioValue = "none";
let textFieldValue = "";
let inputErrorValue = "";
let selections = [];
let errorSelection = [];
</script>

<svelte:head>
Expand Down Expand Up @@ -87,16 +89,83 @@

<DesignSystemPanel
title="Checkboxes"
code={`<ONSCheckboxes>
<ONSCheckbox id="mobile">Mobile</ONSCheckbox>
<ONSCheckbox id="laptop">Laptop</ONSCheckbox>
<ONSCheckbox id="tablet">Tablet</ONSCheckbox>
</ONSCheckboxes>`}
code={`<ONSCheckboxes
name="devices"
title="What devices do you own?"
checkboxesLabel="Select all that apply"
id="1-fieldset"
legendIsQuestionTitle
>
{#each checkboxData as option}
<ONSCheckbox
id={option.id}
value={option.value}
labelText={option.label}
bind:bindGroup={selections}
onChange={(selections) => console.log("User selections: ", selections.join(", "))}
/>
{/each}
</ONSCheckboxes>`}
>
<ONSCheckboxes>
<ONSCheckbox id="mobile">Mobile</ONSCheckbox>
<ONSCheckbox id="laptop">Laptop</ONSCheckbox>
<ONSCheckbox id="tablet">Tablet</ONSCheckbox>
<ONSCheckboxes
name="devices"
title="What devices do you own?"
checkboxesLabel="Select all that apply"
id="1-fieldset"
legendIsQuestionTitle
>
{#each checkboxData as option}
<ONSCheckbox
id={option.id}
value={option.value}
labelText={option.label}
bind:bindGroup={selections}
onChange={(selections) => console.log("User selections: ", selections.join(", "))}
/>
{/each}
</ONSCheckboxes>
<br />
<p>Checkboxes: <strong>{selections}</strong></p>
</DesignSystemPanel>

<DesignSystemPanel
title="Checkboxes - error message"
code={`<ONSCheckboxes
name="devices"
title="What devices do you own?"
checkboxesLabel="Select all that apply"
id="2-fieldset"
legendIsQuestionTitle
renderError
>
{#each checkboxData as option}
<ONSCheckbox
id={option.id}
value={option.value}
labelText={option.label}
bind:bindGroup={errorSelection}
onChange={(errorSelection) => console.log("User selections: ", errorSelection.join(", "))}
/>
{/each}
</ONSCheckboxes>`}
>
<ONSCheckboxes
name="devices"
title="What devices do you own?"
checkboxesLabel="Select all that apply"
id="2-fieldset"
legendIsQuestionTitle
renderError
>
{#each checkboxData as option}
<ONSCheckbox
id={option.id}
value={option.value}
labelText={option.label}
bind:bindGroup={errorSelection}
onChange={(errorSelection) => console.log("User selections: ", errorSelection.join(", "))}
/>
{/each}
</ONSCheckboxes>
</DesignSystemPanel>

Expand Down
30 changes: 25 additions & 5 deletions src/ui/ons/ONSCheckboxes.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<div class="ons-input-items">
<p class="ons-checkboxes__label">Select all that apply</p>
<div class="ons-checkboxes__items">
<slot />
<script>
import { setContext } from "svelte";
import ONSError from "./partials/ONSError.svelte";
export let checkboxesLabel, title, id, name, renderError;
setContext("name", name);
export let legendIsQuestionTitle = false;
export let errorText = "Select [whatever it is]";
</script>

<ONSError {errorText} id={name} {renderError}>
<div class="ons-question ons-u-mt-no">
<fieldset {id} class="ons-fieldset">
{#if legendIsQuestionTitle}
<legend class="ons-fieldset__legend">
<h1 id="{id}-legend-title" class="ons-fieldset__legend-title ">{title}</h1>
</legend>
{/if}
<div class="ons-input-items">
<p class="ons-checkboxes__label">{checkboxesLabel}</p>
<div class="ons-checkboxes__items">
<slot />
</div>
</div>
</fieldset>
</div>
</div>
</ONSError>
36 changes: 29 additions & 7 deletions src/ui/ons/partials/ONSCheckbox.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
<script>
export let id;
export let value;
export let checked = false;
import { getContext } from "svelte";
export let id, value, labelText, onChange;
let name = getContext("name");
export let bindGroup = [];

function onChangeBindGroup({ target }) {
const { value, checked } = target;
if (checked) {
bindGroup = [...bindGroup, value];
} else {
bindGroup = bindGroup.filter((item) => item !== value);
}
}
</script>

<p class="ons-checkboxes__item">
<span class="ons-checkboxes__item">
<span class="ons-checkbox ">
<input type="checkbox" {id} class="ons-checkbox__input ons-js-checkbox " bind:checked />
<label class="ons-checkbox__label" for={id} id="{id}-label"><slot /></label>
<input
type="checkbox"
{id}
class="ons-checkbox__input ons-js-checkbox "
{value}
{name}
checked={bindGroup.includes(value)}
on:change={(e) => {
onChangeBindGroup(e);
onChange(bindGroup);
}}
/>
<label class="ons-checkbox__label " for={id} id="{id}-label">{labelText} </label>
</span>
</p>
</span>
<br />