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

refactor(checkbox, radio, switch): standardize controls #70

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
438 changes: 219 additions & 219 deletions projects/cli/documentation.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<input
#inputElement
[attr.aria-checked]="checked()"
[checked]="checked()"
[attr.aria-checked]="value()"
[checked]="value()"
[attr.aria-disabled]="zenDisabledDirective.disabledBoolean()"
[disabled]="zenDisabledDirective.disabledBoolean()"
(change)="onToggle()"
(keydown.enter)="onToggle()"
(change)="setValue()"
(keydown.enter)="setValue()"
type="checkbox"
/>
@switch (checked()) {
@switch (value()) {
@case (false) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
ElementRef,
forwardRef,
inject,
model,
Renderer2,
signal,
viewChild,
} from '@angular/core';
import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
Expand Down Expand Up @@ -62,13 +62,13 @@ export class ZenCheckboxComponent
implements ControlValueAccessor, AfterViewInit
{
/** Model for the checked state of the checkbox. */
readonly checked = model<CheckedState>(false);
readonly value = signal<CheckedState>(false);

/** @ignore */
readonly zenDisabledDirective = inject(ZenDisabledDirective, { self: true });

/** @ignore */
private readonly checked$ = toObservable(this.checked);
private readonly checked$ = toObservable(this.value);

/** @ignore */
private readonly destroyRef = inject(DestroyRef);
Expand Down Expand Up @@ -96,7 +96,7 @@ export class ZenCheckboxComponent
* Writes a new value to the component.
*/
writeValue(value: boolean): void {
this.checked.set(value);
this.value.set(value);
}

/**
Expand Down Expand Up @@ -124,11 +124,11 @@ export class ZenCheckboxComponent
* Toggles the checkbox value and notifies the change.
* If the component is disabled, no action is performed.
*/
onToggle(): void {
setValue(): void {
if (this.zenDisabledDirective.disabledBoolean()) return;

this.checked.update(value => !value);
this.onChange(this.checked());
this.value.update(value => !value);
this.onChange(this.value());
this.onTouched();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<input
[attr.aria-checked]="checked()"
[checked]="checked()"
[attr.aria-checked]="value()"
[checked]="value()"
[attr.aria-disabled]="zenDisabledDirective.disabledBoolean()"
[disabled]="zenDisabledDirective.disabledBoolean()"
[attr.id]="id()"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ZenRadioComponent implements ControlValueAccessor {
readonly id = input<string | undefined>();

/** Model for the checked state of the checkbox. */
readonly checked = model<boolean>(false);
readonly value = model<boolean>(false);

/** @ignore */
readonly zenDisabledDirective = inject(ZenDisabledDirective, { self: true });
Expand All @@ -47,7 +47,7 @@ export class ZenRadioComponent implements ControlValueAccessor {
* Writes a new value to the component.
*/
writeValue(value: boolean): void {
this.checked.set(value);
this.value.set(value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<button
class="switch"
[attr.aria-checked]="checked()"
[attr.aria-checked]="value()"
[attr.aria-disabled]="zenDisabledDirective.disabledBoolean()"
[disabled]="zenDisabledDirective.disabledBoolean()"
(click)="onToggle()"
(click)="setValue()"
(keydown)="onKeyDown($event)"
role="switch"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Component,
forwardRef,
inject,
model,
signal,
} from '@angular/core';
import {
ControlValueAccessor,
Expand Down Expand Up @@ -50,7 +50,7 @@ type OnTouchedFn = () => void;
})
export class ZenSwitchComponent implements ControlValueAccessor {
/** Model for the checked state of the switch. */
checked = model<boolean>(false);
value = signal<boolean>(false);

/** @ignore */
readonly zenDisabledDirective = inject(ZenDisabledDirective, { self: true });
Expand All @@ -65,7 +65,7 @@ export class ZenSwitchComponent implements ControlValueAccessor {
* @ignore
*/
writeValue(value: boolean): void {
this.checked.set(value);
this.value.set(value);
}

/**
Expand Down Expand Up @@ -95,13 +95,13 @@ export class ZenSwitchComponent implements ControlValueAccessor {
/**
* Toggles the switch value and notifies the change.
*/
onToggle(check?: boolean): void {
setValue(value?: boolean): void {
if (this.zenDisabledDirective.disabledBoolean()) return;

const value = check ?? !this.checked();
const _value = value ?? !this.value();

this.checked.set(value);
this.onChange(value);
this.value.set(_value);
this.onChange(_value);
this.onTouched();
}

Expand All @@ -113,15 +113,15 @@ export class ZenSwitchComponent implements ControlValueAccessor {
case 'Enter':
case 'Space': {
event.preventDefault();
this.onToggle();
this.setValue();
break;
}
case 'ArrowRight': {
this.onToggle(true);
this.setValue(true);
break;
}
case 'ArrowLeft': {
this.onToggle(false);
this.setValue(false);
break;
}
}
Expand Down
8 changes: 4 additions & 4 deletions projects/cli/stories/components/checkbox.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export const Default: Story = {
render: () => ({
template: `
<zen-checkbox />
<zen-checkbox [checked]="true" />
<zen-checkbox checked="mixed" />
<zen-checkbox [value]="true" />
<zen-checkbox value="mixed" />
`,
}),
};
Expand All @@ -24,8 +24,8 @@ export const Disabled: Story = {
render: () => ({
template: `
<zen-checkbox disabled="true"/>
<zen-checkbox [checked]="true" disabled="true"/>
<zen-checkbox checked="mixed" disabled="true"/>
<zen-checkbox [value]="true" disabled="true"/>
<zen-checkbox value="mixed" disabled="true"/>
`,
}),
};
4 changes: 2 additions & 2 deletions projects/cli/stories/components/switch.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const Default: Story = {

export const Disabled: Story = {
render: () => ({
template: `<zen-switch [disabled]="true" />`,
template: `<zen-switch disabled="true" />`,
}),
};

Expand All @@ -36,7 +36,7 @@ export const Checked: Story = {
template: `
<zen-switch [ngModel]="true" />
<br/>
<zen-switch [checked]="true" />
<zen-switch />
`,
}),
};
Loading