Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Feature ] Checkbox Group + Checkbox #57

Draft
wants to merge 31 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f39baca
add checkbox group
Nov 24, 2021
4007f16
add label to checkbox
Nov 24, 2021
1d19f4c
display selected items
Nov 24, 2021
8e1ba70
add label styles
Nov 24, 2021
04959e0
refactor checkbox logic from items to children
Nov 30, 2021
5eb49d2
add selected items to array
Nov 30, 2021
aca6cd7
refactor item from object to string
Nov 30, 2021
fb9f827
display selected items in story + cleanup
Nov 30, 2021
5956823
add even gaps between checkbox and label
Dec 1, 2021
b49ad38
add indeterminate, disabled, error options
Dec 1, 2021
cd8b836
add gap between items to story controls
Dec 1, 2021
3a7bac6
add disabled cursor, and onclick on whole option
Dec 1, 2021
84d650c
remove context from checkbox group
Dec 22, 2021
2fbe7f6
clean up checkbox
Dec 22, 2021
54e8347
rewrite the story of checkbox group
Dec 22, 2021
8898026
adapt the item to the group without context
Dec 22, 2021
3a31798
add checkbox group to angular
Dec 23, 2021
4a02425
remove placeholder
Dec 23, 2021
abd267d
add checkboxGroup to react
Dec 23, 2021
35e9524
remove placeholder from angular app
Dec 23, 2021
bf24954
improve indentation
Dec 28, 2021
7b0ce0e
improve angular handler
Dec 28, 2021
bce7cad
improve indentation
Dec 28, 2021
94c696f
make one option in story indeterminate
Dec 28, 2021
e241e2b
remove svelte event from checkbox
Dec 28, 2021
fdbbf2f
resolve a bug with a wrong prop
Dec 28, 2021
88b0e9c
save Item interface into separate file
Dec 28, 2021
e98d7af
remove unused interface
Dec 28, 2021
e98b2fd
improve indentation
Dec 28, 2021
353ce34
remove console.log from app in react
Dec 28, 2021
de93f99
resolve the bug with indeterminate checkbox
Dec 28, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Item {
label: string
value: string,
disabled: boolean,
error: boolean,
indeterminate: boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<script>
import { Meta, Template, Story } from '@storybook/addon-svelte-csf'
import '@sprinteins/dpdhl-uilib'

let selected = []

function handleSelectItems(event) {
selected = event.detail;
}
</script>


<Meta title="11_Components/Checkbox Group" component={null} argTypes={{
itemsGap: {
name: "Gap between items [rem]",
},
}}/>

<Template let:args>
</Template>


<Template let:args>
<div style="--checkbox-group__items-gap: {args.itemsGap}rem;">
<dpdhl-checkbox-group on:select={handleSelectItems}>
<dpdhl-checkbox-item label="Option A" value="option-a" />
<dpdhl-checkbox-item label="Option B" value="option-b" disabled={true}/>
<dpdhl-checkbox-item label="Option C" value="option-c" error={true}/>
<dpdhl-checkbox-item label="Option D" value="option-d" indeterminate={true}/>
</dpdhl-checkbox-group>
</div>
<p>Selected Items: {selected.length && selected.join(', ')} </p>
</Template>

<Story name="Primary" args={{
itemsGap: 1.5
}}/>

<style></style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<script lang="ts">
import { onMount } from 'svelte'
import { makeEvent } from '../../x/util/dispatch'
import "../dpdhl-icon"
import { KeyItemAdded } from './dpdhl-checkbox-item.svelte'
import type { Item } from './Item';

let container: HTMLElement
let items: Item[] = []
let selectedItems: string[] = []
let assignedElements: HTMLElement[] = []

onMount(() => {
registerItems();
container.addEventListener(KeyItemAdded, registerItems)
})

function registerItems(){
const slot = container.childNodes[0] as HTMLSlotElement

assignedElements = slot.assignedElements() as HTMLElement[]

assignedElements.forEach( (el, ei) => {
const alreadyRegistered = el.hasAttribute('registered')
if( alreadyRegistered ){
return;
}
el.setAttribute('registered','');
const label = el.getAttribute('label')
const value = el.getAttribute('value')
const disabled = el.getAttribute('disabled') === "true";
const error = el.getAttribute('error') === "true";
const indeterminate = el.getAttribute('indeterminate') === "true";
if(items){
items[ei] = {
label,
value,
disabled,
error,
indeterminate
}
}
});
}

let root: HTMLDivElement;
function onItemClick(item: Item) {
if (selectedItems.includes(item.value)) {
selectedItems = [...selectedItems.filter(i => i !== item.value)]
} else {
selectedItems = [...selectedItems, item.value]
}
root.dispatchEvent(makeEvent('select', selectedItems))
}

</script>
<svelte:options tag="dpdhl-checkbox-group" />

<div bind:this={root} class="root">
{#each items as item}
<dpdhl-checkbox
on:check={() => onItemClick(item)}
checked={selectedItems.includes(item.value)}
value={item.value}
disabled={item.disabled}
error={item.error}
indeterminate={item.indeterminate}>
{item.label}
</dpdhl-checkbox>
{/each}
</div>


<div bind:this={container}>
<slot />
</div>

<style>
.root {
display: flex;
flex-direction: column;
gap: var(--radio-group__items-gap, 1.5rem);
}

</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script context='module' lang='ts'>
export const KeyItemAdded = 'itemadded'
</script>
<script lang='ts'>
import { onMount } from 'svelte'

let contentRef: HTMLElement
function itemAdded(){
if(!contentRef) { return }
contentRef.dispatchEvent(new CustomEvent(KeyItemAdded,{
bubbles: true,
composed: true,
}))
}
onMount(itemAdded);
</script>
<svelte:options tag="dpdhl-checkbox-item" />

<span>
<slot />
</span>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as DPDHLCheckboxGroup } from './dpdhl-checkbox-group.svelte';
export { default as DPDHLCheckboxItem } from './dpdhl-checkbox-item.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script lang="ts">
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import "@sprinteins/dpdhl-uilib";

$: checked = true;
</script>

<Meta title="11_Components/Checkbox" component={null} />

<Template let:args />

<Story
name="Primary"
args={{
disabled: false,
indeterminate: false,
error: false,
}}
let:args
>
<main>
<dpdhl-checkbox
on:check={() => checked = !checked}
name="checkbox-example"
disabled={args.disabled}
checked={checked}
indeterminate={args.indeterminate}
error={args.error}>
</dpdhl-checkbox>
<p>Checked: {checked}</p>
</main>
</Story>

<style></style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<svelte:options tag="dpdhl-checkbox" />

<script lang="ts">
import { makeEvent } from "../../x/util/dispatch";

/**
* A Checkbox Component
* @component
*/

export let value;

export let name = "";
$: _name = name;

export let checked = false;
$: _checked = checked;

export let indeterminate = false;
$: _indeterminate = indeterminate;

export let disabled = false;
$: _disabled = disabled;

export let error = false;
$: _error = error;

let root: HTMLDivElement;
function handleClick(event) {
if (_disabled) {
event.preventDefault();
return;
}
if (_indeterminate) {
_indeterminate = false;
}
root.dispatchEvent(makeEvent('check', value ))
}

</script>

<span class="container" class:disabled={_disabled} on:click={handleClick} bind:this={root}>
<input
{value}
id={_name}
name={_name}
type="checkbox"
bind:checked={_checked}
bind:indeterminate={_indeterminate}
class:disabled={_disabled}
class:indeterminate={_indeterminate}
class:error={_error}
/>
<span class="label">
<slot></slot>
</span>
</span>

<style>

.container {
display: flex;
flex-direction: row;
cursor: pointer;
}

.container.disabled {
cursor: not-allowed;
}

.label {
margin-left: 1rem;
line-height: 24px;
padding-top: 0.34rem;
word-wrap: normal;
}
input {
position: relative;
cursor: pointer;
height: 1.5rem;
-moz-appearance: none;
margin-right: 1rem;
}

/* box */
input:before {
content: "";
display: block;
position: absolute;
width: 1.5rem;
height: 1.5rem;
top: 0;
left: 0;
border: 1px solid var(--color-gray45);
border-radius: 3px;
background-color: white;
}

/* box checked */
input:checked:before {
border: 1px solid var(--color-postyellow);
background-color: var(--color-postyellow);
}
/* check */
input:checked:after {
content: "";
display: block;
width: 0.4375rem;
height: 0.625rem;
border: solid var(--color-white);
border-width: 0 1px 1px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
position: absolute;
top: 0.3125rem;
left: 0.5625rem;
}

/* box disabled */
input.disabled:not(:checked):before {
background-color: var(--color-gray10);
border: 1px solid var(--color-gray10);
color: grey;
}
/* box checked disabled */
input.disabled:checked:before {
background-color: var(--color-gray10);
color: grey;
border: 1px solid var(--color-gray10);
}
/* check disabled */
input.disabled:checked:after {
content: "";
display: block;
width: 0.4375rem;
height: 0.625rem;
border: solid var(--color-gray60);
border-width: 0 1px 1px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
position: absolute;
top: 0.3125rem;
left: 0.5625rem;
}

input.indeterminate:before {
border: 1px solid var(--color-postyellow);
background-color: var(--color-postyellow);
}

/* indeterminate */
input.indeterminate:after {
content: "";
display: block;
width: 0.5rem;
height: 0.2rem;
border: solid var(--color-white);
border-width: 1px 0 0 0;
position: absolute;
top: 0.8rem;
left: 0.5625rem;
}

/* indeterminate */
input.indeterminate.error:after {
content: "";
display: block;
width: 0.5rem;
height: 0.2rem;
border: solid var(--color-dhlred);
border-width: 1px 0 0 0;
position: absolute;
top: 0.8rem;
left: 0.5625rem;
}

/* error */
input.error:before {
border: 1px solid var(--color-dhlred);
background-color: var(--color-dhlred-light);
}
input.error:checked:after {
content: "";
display: block;
width: 0.4375rem;
height: 0.6rem;
border: solid var(--color-dhlred);
border-width: 0 1px 1px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
position: absolute;
top: 0.3125rem;
left: 0.5625rem;
}
</style>
Loading