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

fix(components-angular): broken post-card-control value-accessors #3101

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/strong-countries-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@swisspost/design-system-components-angular': patch
---

Fixed broken value-accessors for post-card-control.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import { APP_INITIALIZER, NgModule } from '@angular/core';
import { defineCustomElements } from '@swisspost/design-system-components/loader';

import { DIRECTIVES } from './stencil-generated';
import { BooleanValueAccessor } from './stencil-generated/boolean-value-accessor';
import { PostCardControlValueAccessorDirective } from './custom/value-accessors/post-card-control-radio-value-accessor';
import { PostCardControlCheckboxValueAccessorDirective } from './custom/value-accessors/post-card-control-checkbox-value-accessor';
import { PostCardControlRadioValueAccessorDirective } from './custom/value-accessors/post-card-control-radio-value-accessor';

const DECLARATIONS = [...DIRECTIVES, BooleanValueAccessor, PostCardControlValueAccessorDirective];
const DECLARATIONS = [
...DIRECTIVES,
PostCardControlCheckboxValueAccessorDirective,
PostCardControlRadioValueAccessorDirective,
];

@NgModule({
declarations: DECLARATIONS,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Directive, ElementRef, HostListener } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';

@Directive({
/* eslint-disable-next-line @angular-eslint/directive-selector */
selector: 'post-card-control[type="checkbox"]',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: PostCardControlCheckboxValueAccessorDirective,
multi: true,
},
],
})
export class PostCardControlCheckboxValueAccessorDirective implements ControlValueAccessor {
private onChange: (value: any) => void = () => {
/**/
};
private onTouched: () => void = () => {
/**/
};
protected lastValue: any;

constructor(protected el: ElementRef) {}

writeValue(value: any) {
this.el.nativeElement.checked = this.lastValue = value === null ? false : value;
}

@HostListener('postChange', ['$event.detail.state'])
handleChangeEvent(value: any) {
this.onChange(value);
}

@HostListener('focusout')
_handleBlurEvent() {
this.onTouched();
}

registerOnChange(fn: (value: any) => void) {
this.onChange = fn;
}
registerOnTouched(fn: () => void) {
this.onTouched = fn;
}

setDisabledState(isDisabled: boolean) {
this.el.nativeElement.disabled = isDisabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: PostCardControlValueAccessorDirective,
useExisting: PostCardControlRadioValueAccessorDirective,
multi: true,
},
],
})
export class PostCardControlValueAccessorDirective implements ControlValueAccessor {
export class PostCardControlRadioValueAccessorDirective implements ControlValueAccessor {
private onChange: (value: any) => void = () => {
/**/
};
Expand All @@ -28,7 +28,7 @@ export class PostCardControlValueAccessorDirective implements ControlValueAccess
this.el.nativeElement.value != value ? false : value;
}

@HostListener('change', ['$event.detail.value'])
@HostListener('postChange', ['$event.detail.value'])
handleChangeEvent(value: any) {
this.onChange(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export { DIRECTIVES } from './lib/stencil-generated';
// Export all custom made components & directives!
// Skipping this step will lead to Angular Ivy errors when building for production.

export { BooleanValueAccessor } from './lib/stencil-generated/boolean-value-accessor';
export { PostCardControlValueAccessorDirective } from './lib/custom/value-accessors/post-card-control-radio-value-accessor';
// export { BooleanValueAccessor } from './lib/stencil-generated/boolean-value-accessor';
oliverschuerch marked this conversation as resolved.
Show resolved Hide resolved
export { PostCardControlCheckboxValueAccessorDirective } from './lib/custom/value-accessors/post-card-control-checkbox-value-accessor';
export { PostCardControlRadioValueAccessorDirective } from './lib/custom/value-accessors/post-card-control-radio-value-accessor';
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h3>Form Builder Form</h3>
</div>

<div class="col-4">
<legend>Native Checkbox</legend>
<legend>Native Radio Group</legend>
<div *ngFor="let option of radioOptions; index as i" class="form-check">
<input
[id]="option"
Expand Down
8 changes: 1 addition & 7 deletions packages/components/.config/bindings.angular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@ import { ValueAccessorConfig } from '@stencil/angular-output-target';

// https://stenciljs.com/docs/v4/angular#valueaccessorconfigs
export const angularValueAccessorBindings: ValueAccessorConfig[] = [
{
elementSelectors: ['post-card-control[type="checkbox"]'],
event: 'change',
targetAttr: 'checked',
type: 'boolean',
},
// a custom post-card-control-value-accessor for post-card-control[type="radio"] has been implemented in the components-angular package
// custom post-card-control-value-accessors for post-card-control have been implemented in the components-angular package
];
Loading