Skip to content

Commit

Permalink
feat: first draft checkbox-group
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Pajung authored and nowseemee committed Jul 19, 2021
1 parent de170f7 commit 0d77ccc
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.checkbox-group {
display: inline-flex;
flex-direction: column;
width: 300px;
}

.checkbox-group__container {
display: flex;
flex-direction: column;
margin-left: 1.5rem;
}

.checkbox-group__checkbox {
margin-top: 0.5rem;
}
110 changes: 110 additions & 0 deletions packages/components/src/components/checkbox-group/checkbox-group.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @license
* Scale https://github.com/telekom/scale
*
* Copyright (c) 2021 Egor Kirpichev and contributors, Deutsche Telekom AG
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { Component, h, Host, Listen, Element } from '@stencil/core';

@Component({
tag: 'scale-checkbox-group',
styleUrl: './checkbox-group.css',
shadow: true,
})
export class CheckboxGroup {
@Element() hostElement: HTMLElement;
@Listen('scaleChange')
scaleChangeHandler(event: CustomEvent<any>) {
console.log('Received the scaleChange event: ', event.detail);
this.setCheckboxes(event);
}

connectedCallback() {
this.setCheckboxes = this.setCheckboxes.bind(this);
}

setCheckboxes(event: CustomEvent<any>) {
const checkboxes = Array.from(
this.hostElement.shadowRoot.querySelectorAll('scale-checkbox')
);
const checked = event.detail.checked;
let countChecked = 0;
let countUnchecked = 0;
if (event.detail.id === 'checkbox1') {
checkboxes.forEach((checkbox) => {
checkbox.checked = checked;
});
} else {
for (let i = 1; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
countChecked = countChecked + 1;
} else {
countUnchecked = countUnchecked + 1;
}
}
}

if (countChecked === checkboxes.length - 1) {
checkboxes[0].checked = true;
return;
}
if (countUnchecked === checkboxes.length - 1) {
checkboxes[0].checked = false;
return;
}
}

render() {
return (
<Host>
<div class="checkbox-group">
<div class="checkbox-group__label">
<scale-checkbox
input-id="checkbox1"
value="1"
label="checkbox"
name="nameOfCheckbox"
helper-text="helperText"
checked
></scale-checkbox>
</div>
<div class="checkbox-group__container">
<div class="checkbox-group__checkbox">
<scale-checkbox
input-id="checkbox2"
value="2"
label="checkbox"
name="nameOfCheckbox"
></scale-checkbox>
</div>
<div class="checkbox-group__checkbox">
<scale-checkbox
input-id="checkbox3"
value="3"
label="checkbox"
name="nameOfCheckbox"
></scale-checkbox>
</div>
<div class="checkbox-group__checkbox">
<scale-checkbox
input-id="checkbox4"
value="4"
label="checkbox"
name="nameOfCheckbox"
checked
></scale-checkbox>
</div>
</div>
</div>
{/* <button class="button" onClick={this.setCheckboxes}>
click
</button>*/}
</Host>
);
}
}
25 changes: 25 additions & 0 deletions packages/components/src/components/checkbox-group/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# scale-checkbox-group



<!-- Auto Generated Below -->


## Dependencies

### Depends on

- [scale-checkbox](../checkbox)

### Graph
```mermaid
graph TD;
scale-checkbox-group --> scale-checkbox
scale-checkbox --> scale-icon-action-success
scale-checkbox --> scale-icon-action-play
style scale-checkbox-group fill:#f9f,stroke:#333,stroke-width:4px
```

----------------------------------------------

*Built with [StencilJS](https://stenciljs.com/)*
14 changes: 7 additions & 7 deletions packages/components/src/components/checkbox/checkbox.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ scale-checkbox {
.checkbox
input:checked:disabled
~ .checkbox__control-wrapper
scale-icon-action-success {
.icon {
color: var(--color-icon-checked-disabled);
}
.checkbox
Expand Down Expand Up @@ -153,11 +153,11 @@ scale-checkbox {
.checkbox
input:checked:not([disabled]):active
~ .checkbox__control-wrapper
scale-icon-action-success,
.icon,
.checkbox
input:checked:not([disabled])
~ .checkbox__control-wrapper:active
scale-icon-action-success {
.icon {
color: var(--color-icon-checked-active);
}
.checkbox
Expand Down Expand Up @@ -208,11 +208,11 @@ scale-checkbox {
.checkbox
input:not([disabled]):active
~ .checkbox__control-wrapper
scale-icon-action-success,
.icon,
.checkbox
input:not([disabled])
~ .checkbox__control-wrapper:active
scale-icon-action-success {
.icon {
color: var(--color-primary-active);
}
.checkbox
Expand All @@ -234,7 +234,7 @@ scale-checkbox {
border: var(--border-width-control) solid var(--color-text);
background: var(--color-standard);
}
.checkbox .checkbox__control-wrapper scale-icon-action-success {
.checkbox .checkbox__control-wrapper .icon {
top: var(--top-icon);
left: var(--left-icon);
width: var(--width-icon);
Expand All @@ -244,7 +244,7 @@ scale-checkbox {
user-select: none;
color: var(--color-standard);
}
.checkbox .checkbox__control-wrapper scale-icon-action-success svg {
.checkbox .checkbox__control-wrapper .icon svg {
width: var(--width-icon);
height: var(--height-icon);
}
Expand Down
14 changes: 12 additions & 2 deletions packages/components/src/components/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export class Checkbox {
@Prop() disabled?: boolean;
/** (optional) Active switch */
@Prop({ reflect: true }) checked?: boolean = false;
/** (optional) indeterminate */
@Prop({ reflect: true }) indeterminate?: boolean = false;
/** (optional) Input value */
@Prop({ mutable: true }) value?: string | number | null = '';
/** (optional) Input checkbox id */
Expand Down Expand Up @@ -73,7 +75,11 @@ export class Checkbox {
onChange={(e: any) => {
this.checked = e.target.checked;
// bubble event through the shadow dom
this.scaleChange.emit({ value: this.checked });
this.scaleChange.emit({
value: this.checked,
id: e.target.id,
checked: e.target.checked,
});
}}
value={this.value}
checked={this.checked}
Expand All @@ -84,11 +90,15 @@ export class Checkbox {
<div class="checkbox__control-wrapper">
<span class="checkbox__control"></span>
{/* Accessibility: rendering the icon *only* when checked, otherwise is always visible in HCM */}
{this.checked && (
{this.checked && !this.indeterminate && (
<scale-icon-action-success
class="icon"
decorative
></scale-icon-action-success>
)}
{this.indeterminate && (
<scale-icon-action-play class="icon"></scale-icon-action-play>
)}
</div>
<span class="checkbox__label">
{this.label ? this.label : <slot></slot>}
Expand Down
27 changes: 16 additions & 11 deletions packages/components/src/components/checkbox/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@

## Properties

| Property | Attribute | Description | Type | Default |
| ------------ | ------------- | ------------------------------ | ------------------ | ----------- |
| `checked` | `checked` | (optional) Active switch | `boolean` | `false` |
| `disabled` | `disabled` | (optional) Input disabled | `boolean` | `undefined` |
| `helperText` | `helper-text` | (optional) Input helper text | `string` | `''` |
| `inputId` | `input-id` | (optional) Input checkbox id | `string` | `undefined` |
| `label` | `label` | (optional) Input label | `string` | `''` |
| `name` | `name` | (optional) Input name | `string` | `''` |
| `status` | `status` | (optional) Input status | `string` | `''` |
| `styles` | `styles` | (optional) Injected CSS styles | `string` | `undefined` |
| `value` | `value` | (optional) Input value | `number \| string` | `''` |
| Property | Attribute | Description | Type | Default |
| ---------------- | ---------------- | ------------------------------ | ------------------ | ----------- |
| `checked` | `checked` | (optional) Active switch | `boolean` | `false` |
| `disabled` | `disabled` | (optional) Input disabled | `boolean` | `undefined` |
| `helperText` | `helper-text` | (optional) Input helper text | `string` | `''` |
| `indeterminated` | `indeterminated` | (optional) indeterminated | `boolean` | `false` |
| `inputId` | `input-id` | (optional) Input checkbox id | `string` | `undefined` |
| `label` | `label` | (optional) Input label | `string` | `''` |
| `name` | `name` | (optional) Input name | `string` | `''` |
| `status` | `status` | (optional) Input status | `string` | `''` |
| `styles` | `styles` | (optional) Injected CSS styles | `string` | `undefined` |
| `value` | `value` | (optional) Input value | `number \| string` | `''` |


## Events
Expand All @@ -31,16 +32,20 @@

### Used by

- [scale-checkbox-group](../checkbox-group)
- [scale-data-grid](../data-grid)

### Depends on

- [scale-icon-action-success](../icons/action-success)
- [scale-icon-action-play](../icons/action-play)

### Graph
```mermaid
graph TD;
scale-checkbox --> scale-icon-action-success
scale-checkbox --> scale-icon-action-play
scale-checkbox-group --> scale-checkbox
scale-data-grid --> scale-checkbox
style scale-checkbox fill:#f9f,stroke:#333,stroke-width:4px
```
Expand Down

0 comments on commit 0d77ccc

Please sign in to comment.