Skip to content

Commit

Permalink
feat(checkbox): refactor checkbox component (#409)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Kieran Nichols <kieran.nichols@tylertech.com>
  • Loading branch information
samrichardsontylertech and DRiFTy17 committed Oct 31, 2023
1 parent 8d70fde commit 3ae5c0f
Show file tree
Hide file tree
Showing 47 changed files with 2,423 additions and 2,480 deletions.
46 changes: 29 additions & 17 deletions src/dev/pages/checkbox/checkbox.ejs
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
<div class="vert">
<div class="vert" id="container">
<div>
<h3 class="forge-typography--heading2">Default</h3>
<forge-checkbox>
<input type="checkbox" id="default-checkbox" checked>
<label for="default-checkbox" class="forge-typography--label">Checkbox label</label>
<forge-checkbox>Default</forge-checkbox>
</div>

<div>
<h3 class="forge-typography--heading2">Exposed input</h3>
<forge-checkbox id="exposed-input-checkbox">
<span>Exposed input</span>
</forge-checkbox>
</div>

<div>
<h3 class="forge-typography--heading2">Dense</h3>
<forge-checkbox dense>
<input type="checkbox" id="dense-checkbox" checked>
<label for="dense-checkbox" class="forge-typography--label">Dense checkbox</label>
</forge-checkbox>
<forge-checkbox dense>Dense</forge-checkbox>
</div>

<div>
<h3 class="forge-typography--heading2">Indeterminate</h3>
<forge-checkbox >
<input type="checkbox" id="indeterminate-checkbox" checked>
<label for="indeterminate-checkbox" class="forge-typography--label">Indeterminate checkbox</label>
</forge-checkbox>
<h3 class="forge-typography--heading2">Readonly</h3>
<forge-checkbox readonly>Readonly</forge-checkbox>
</div>

<div>
<h3 class="forge-typography--heading2">Disabled</h3>
<forge-checkbox>
<input type="checkbox" id="disabled-checkbox" checked disabled>
<label for="disabled-checkbox" class="forge-typography--label">Disabled checkbox</label>
</forge-checkbox>
<forge-checkbox disabled>Disabled</forge-checkbox>
</div>

<div>
<h3 class="forge-typography--heading2">Start label</h3>
<forge-checkbox label-position="start">Start label</forge-checkbox>
</div>

<div>
<h3 class="forge-typography--heading2">Prevent default</h3>
<forge-checkbox id="prevent-checkbox">Prevent default</forge-checkbox>
</div>

<div>
<h3 class="forge-typography--heading2">Form associated</h3>
<form name="test-form">
<forge-checkbox name="test-checkbox">Form associated</forge-checkbox>
</form>
</div>
</div>

Expand Down
7 changes: 6 additions & 1 deletion src/dev/pages/checkbox/checkbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
include('./src/partials/page.ejs', {
page: {
title: 'Checkbox',
includePath: './pages/checkbox/checkbox.ejs'
includePath: './pages/checkbox/checkbox.ejs',
options: [
{ type: 'switch', label: 'Checked', id: 'opt-checked' },
{ type: 'switch', label: 'Indeterminate', id: 'opt-indeterminate' },
{ type: 'switch', label: 'Expose input', id: 'opt-expose-input' }
]
}
})
%>
38 changes: 36 additions & 2 deletions src/dev/pages/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import '$src/shared';
import '@tylertech/forge/checkbox';
import './checkbox.scss';
import { ICheckboxComponent, ISwitchComponent } from '@tylertech/forge';

const indeterminateCheckbox = document.getElementById('indeterminate-checkbox') as HTMLInputElement;
indeterminateCheckbox.indeterminate = true;
const checkboxesCheckedToggle = document.getElementById('opt-checked') as ISwitchComponent;
const checkboxesIndeterminateToggle = document.getElementById('opt-indeterminate') as ISwitchComponent;
const exposeInputToggle = document.getElementById('opt-expose-input') as ISwitchComponent;

checkboxesCheckedToggle.addEventListener('forge-switch-change', ({ detail: selected }) => {
const checkboxes = document.querySelectorAll('forge-checkbox') as NodeListOf<ICheckboxComponent>;
checkboxes.forEach(checkbox => checkbox.checked = selected);
});

checkboxesIndeterminateToggle.addEventListener('forge-switch-change', ({ detail: selected }) => {
const checkboxes = document.querySelectorAll('forge-checkbox') as NodeListOf<ICheckboxComponent>;
checkboxes.forEach(checkbox => checkbox.indeterminate = selected);
});

exposeInputToggle.addEventListener('forge-switch-change', ({ detail: selected }) => {
const checkbox = document.getElementById('exposed-input-checkbox') as ICheckboxComponent;
if (selected) {
const input = document.createElement('input');
input.type = 'checkbox';
input.slot = 'input';
input.setAttribute('aria-labelledby', 'exposed-input-checkbox');
checkbox.append(input);
} else {
checkbox.querySelector('input')?.remove();
}
});

document.querySelectorAll('forge-checkbox').forEach(checkbox => {
checkbox.addEventListener('change', evt => console.log(evt));
});

const preventCheckbox = document.getElementById('prevent-checkbox') as ICheckboxComponent;
preventCheckbox.addEventListener('change', (evt: Event) => {
evt.preventDefault();
});
23 changes: 13 additions & 10 deletions src/dev/pages/switch/switch.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
<forge-switch disabled selected>off/on</forge-switch>
</div>

<div>
<h3 class="forge-typography--heading2">Readonly</h3>
<forge-switch readonly>off/on</forge-switch>
</div>

<div>
<h3 class="forge-typography--heading2">Custom icons</h3>
<forge-switch icon="both">
Expand All @@ -35,16 +40,6 @@
</forge-switch>
</div>

<form id="test-form">
<h3 class="forge-typography--heading2">Form associated</h3>
<label class="form-switch">
<span class="forge-typography--label">off/on</span>
<forge-switch id="form-switch" name="test-switch" aria-label="Aria active"></forge-switch>
</label>
<button type="reset">Reset</button>
<button type="submit">Submit</button>
</form>

<div>
<h3 class="forge-typography--heading2">Custom styling</h3>
<forge-switch class="custom-switch" label-position="start">off/on</forge-switch>
Expand All @@ -54,6 +49,14 @@
<h3 class="forge-typography--heading2">Prevent default</h3>
<forge-switch id="prevent-switch">off/on</forge-switch>
</div>

<div>
<h3 class="forge-typography--subtitle1-secondary">Exposed input</h3>
<forge-switch id="exposed-input">
<input id="light-input" slot="input" type="checkbox" aria-labelledby="exposed-input" />
<span>off/on</span>
</forge-switch>
</div>
</div>

<script type="module" src="switch.ts"></script>
6 changes: 0 additions & 6 deletions src/dev/pages/switch/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,3 @@ IconRegistry.define([
const preventSwitch = document.getElementById('prevent-switch') as ISwitchComponent;

preventSwitch.addEventListener('forge-switch-change', (evt: CustomEvent) => evt.preventDefault());

const testForm = document.getElementById('test-form') as HTMLFormElement;
testForm.addEventListener('submit', (evt: Event) => {
evt.preventDefault();
console.log('[submit] switch value:', new FormData(testForm).get('test-switch'));
});
6 changes: 0 additions & 6 deletions src/lib/checkbox/_checkbox-custom-properties.scss

This file was deleted.

Loading

0 comments on commit 3ae5c0f

Please sign in to comment.