import { Component, ChangeDetectionStrategy, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { CheckboxComponent } from '../../../../../ui-essentials/src/lib/components/forms/checkbox/checkbox.component';
@Component({
selector: 'ui-checkbox-demo',
standalone: true,
imports: [
CommonModule,
FormsModule,
CheckboxComponent
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
Checkbox Component Showcase
Interactive Controls
@if (lastSelection()) {
Last selection: {{ lastSelection() }}
}
Code Examples
Basic Usage:
<ui-checkbox
label="Accept terms"
[(ngModel)]="isAccepted"
(checkboxChange)="onAccept($event)">
</ui-checkbox>
With Description:
<ui-checkbox
label="Marketing emails"
description="Receive promotional content"
variant="success"
size="lg"
[(ngModel)]="emailOptIn">
</ui-checkbox>
Required Field:
<ui-checkbox
label="I agree to terms"
[required]="true"
state="error"
[(ngModel)]="agreedToTerms">
</ui-checkbox>
`,
styles: [`
h2 {
color: hsl(279, 14%, 11%);
font-size: 2rem;
margin-bottom: 2rem;
border-bottom: 2px solid hsl(258, 100%, 47%);
padding-bottom: 0.5rem;
}
h3 {
color: hsl(279, 14%, 25%);
font-size: 1.5rem;
margin-bottom: 1rem;
}
h4 {
color: hsl(279, 14%, 35%);
font-size: 1.2rem;
margin-bottom: 0.5rem;
}
pre {
margin: 0;
font-family: 'Courier New', monospace;
font-size: 0.9rem;
line-height: 1.4;
}
code {
color: hsl(279, 14%, 15%);
}
section {
border: 1px solid #e9ecef;
padding: 1.5rem;
border-radius: 8px;
background: #ffffff;
}
button:hover {
opacity: 0.9;
transform: translateY(-1px);
}
`]
})
export class CheckboxDemoComponent {
// Basic checkboxes
basicChecked1 = signal(false);
basicChecked2 = signal(true);
basicChecked3 = signal(false);
// Size variants
sizeSmall = signal(false);
sizeMedium = signal(true);
sizeLarge = signal(false);
// Color variants
variantPrimary = signal(true);
variantSecondary = signal(false);
variantSuccess = signal(true);
variantWarning = signal(false);
variantDanger = signal(false);
// States
stateDisabledUnchecked = signal(false);
stateDisabledChecked = signal(true);
stateRequired = signal(false);
stateError = signal(false);
// With descriptions
descChecked1 = signal(false);
descChecked2 = signal(true);
descChecked3 = signal(true);
// Control states
globalDisabled = signal(false);
lastSelection = signal('');
onCheckboxChange(checkboxId: string, checked: boolean): void {
this.lastSelection.set(`${checkboxId}: ${checked ? 'checked' : 'unchecked'}`);
console.log(`Checkbox ${checkboxId} changed:`, checked);
}
checkAll(): void {
// Update all non-disabled checkboxes to checked
this.basicChecked1.set(true);
this.basicChecked2.set(true);
this.basicChecked3.set(true);
this.sizeSmall.set(true);
this.sizeMedium.set(true);
this.sizeLarge.set(true);
this.variantPrimary.set(true);
this.variantSecondary.set(true);
this.variantSuccess.set(true);
this.variantWarning.set(true);
this.variantDanger.set(true);
this.stateRequired.set(true);
this.stateError.set(true);
this.descChecked1.set(true);
this.descChecked2.set(true);
this.descChecked3.set(true);
this.lastSelection.set('All checkboxes checked');
}
uncheckAll(): void {
// Update all non-disabled checkboxes to unchecked
this.basicChecked1.set(false);
this.basicChecked2.set(false);
this.basicChecked3.set(false);
this.sizeSmall.set(false);
this.sizeMedium.set(false);
this.sizeLarge.set(false);
this.variantPrimary.set(false);
this.variantSecondary.set(false);
this.variantSuccess.set(false);
this.variantWarning.set(false);
this.variantDanger.set(false);
this.stateRequired.set(false);
this.stateError.set(false);
this.descChecked1.set(false);
this.descChecked2.set(false);
this.descChecked3.set(false);
this.lastSelection.set('All checkboxes unchecked');
}
toggleDisabled(): void {
this.globalDisabled.update(disabled => !disabled);
this.lastSelection.set(`Global disabled: ${this.globalDisabled() ? 'enabled' : 'disabled'}`);
}
getCurrentValues(): string {
const values = {
basic: {
checked1: this.basicChecked1(),
checked2: this.basicChecked2(),
checked3: this.basicChecked3()
},
sizes: {
small: this.sizeSmall(),
medium: this.sizeMedium(),
large: this.sizeLarge()
},
variants: {
primary: this.variantPrimary(),
secondary: this.variantSecondary(),
success: this.variantSuccess(),
warning: this.variantWarning(),
danger: this.variantDanger()
},
states: {
disabledUnchecked: this.stateDisabledUnchecked(),
disabledChecked: this.stateDisabledChecked(),
required: this.stateRequired(),
error: this.stateError()
},
descriptions: {
desc1: this.descChecked1(),
desc2: this.descChecked2(),
desc3: this.descChecked3()
},
controls: {
globalDisabled: this.globalDisabled(),
lastSelection: this.lastSelection()
}
};
return JSON.stringify(values, null, 2);
}
}