Skip to content

Commit

Permalink
fix: ControlValueAccessors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikarin committed Jun 10, 2024
1 parent a9df72c commit 8952509
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 23 deletions.
4 changes: 4 additions & 0 deletions apps/web/src/app/docs/components/example/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export class Example implements OnInit {
async fetchFiles() {
const files: File[] = [];

if (!this.files()) {
return [];
}

// TODO: finish this
for (const file of Object.keys(this.files()!)) {
if (this.files()![file] === FileType.Component) {
Expand Down
2 changes: 1 addition & 1 deletion libs/xui/src/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class XuiCheckbox implements ControlValueAccessor {
}

effect(() => this.disabled() && this._disabled.set(this.disabled()!), { allowSignalWrites: true });
effect(() => this.onChange?.(this.value()!));
effect(() => this.value() != undefined && this.onChange?.(this.value()!));
}

writeValue(source: boolean) {
Expand Down
4 changes: 2 additions & 2 deletions libs/xui/src/input/input.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div [ngClass]="_styles()">
<div [ngClass]="_styles()" [class.x-input-error]="_showError">
<ng-content select="[xuiPrefix]" />
<input
[tabindex]="_disabled() ? -1 : 0"
Expand All @@ -12,7 +12,7 @@
<ng-content select="[xuiPostfix]" />
</div>

<div class="x-input-error-text" *ngIf="showError">
<div class="x-input-error-text" *ngIf="_showError">
<ng-container *ngIf="control?.hasError('message')">{{ errorMessage }}</ng-container>
<ng-container *ngIf="control?.hasError('required')">This field is required!</ng-container>
<ng-container *ngIf="control?.hasError('email')">Email expected</ng-container>
Expand Down
13 changes: 5 additions & 8 deletions libs/xui/src/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class XuiInput implements ControlValueAccessor {
_styles = computed(() => {
const ret: { [klass: string]: boolean } = {
'x-input': true,
'x-input-error': this.showError
'x-input-error': this._showError
};

ret[`x-input-${this.color()}`] = true;
Expand All @@ -69,20 +69,17 @@ export class XuiInput implements ControlValueAccessor {
}

effect(() => this.disabled() && this._disabled.set(this.disabled()!), { allowSignalWrites: true });
effect(() => this.onChange?.(this.value()!));
effect(() => this.value() != undefined && this.onChange?.(this.value()!));
}

get invalid(): boolean {
return !!this.control?.invalid;
}

get showError(): boolean {
get _showError(): boolean {
if (!this.control) {
return false;
}

const invalid = !!this.control.invalid;
const { dirty, touched } = this.control;
return this.invalid ? (dirty ?? false) || (touched ?? false) : false;
return invalid ? (dirty ?? false) || (touched ?? false) : false;
}

writeValue(source: string) {
Expand Down
2 changes: 1 addition & 1 deletion libs/xui/src/radio-list/radio-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class XuiRadioList implements RadioListAccessor, ControlValueAccessor {
}

effect(() => this.disabled() && this._disabled.set(this.disabled()!), { allowSignalWrites: true });
effect(() => this.onChange?.(this.value()!));
effect(() => this.value() != undefined && this.onChange?.(this.value()!));
}

writeValue(source: RadioListValue) {
Expand Down
2 changes: 1 addition & 1 deletion libs/xui/src/radio/radio-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class XuiRadioGroup implements RadioGroupAccessor, ControlValueAccessor {
}

effect(() => this.disabled() && this._disabled.set(this.disabled()!), { allowSignalWrites: true });
effect(() => this.onChange?.(this.value()!));
effect(() => this.value() != undefined && this.onChange?.(this.value()!));
}

writeValue(source: RadioValue) {
Expand Down
2 changes: 1 addition & 1 deletion libs/xui/src/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class XuiSelect implements SelectAccessor, ControlValueAccessor {
}

effect(() => this.disabled() && this._disabled.set(this.disabled()!), { allowSignalWrites: true });
effect(() => this.onChange?.(this.value()!));
effect(() => this.value() != undefined && this.onChange?.(this.value()!));
}

open() {
Expand Down
4 changes: 2 additions & 2 deletions libs/xui/src/textarea/textarea.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div [ngClass]="_styles()" [class.x-textarea-error]="showError">
<div [ngClass]="_styles()" [class.x-textarea-error]="_showError">
<textarea
[disabled]="_disabled()"
[readOnly]="readOnly()"
Expand All @@ -12,7 +12,7 @@
<div *ngIf="maxLength()" class="x-textarea-word-counter">{{ _worldCount() }}</div>
</div>

<div class="x-textarea-error-text" *ngIf="showError">
<div class="x-textarea-error-text" *ngIf="_showError">
<ng-container *ngIf="control?.hasError('message')">{{ errorMessage }}</ng-container>
<ng-container *ngIf="control?.hasError('required')">This field is required!</ng-container>
<ng-container *ngIf="control?.hasError('email')">Email expected</ng-container>
Expand Down
11 changes: 4 additions & 7 deletions libs/xui/src/textarea/textarea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,17 @@ export class XuiTextarea implements ControlValueAccessor {
}

effect(() => this.disabled() && this._disabled.set(this.disabled()!), { allowSignalWrites: true });
effect(() => this.onChange?.(this.value()!));
effect(() => this.value() != undefined && this.onChange?.(this.value()!));
}

get invalid(): boolean {
return !!this.control?.invalid;
}

get showError(): boolean {
get _showError(): boolean {
if (!this.control) {
return false;
}

const invalid = !!this.control.invalid;
const { dirty, touched } = this.control;
return this.invalid ? (dirty ?? false) || (touched ?? false) : false;
return invalid ? (dirty ?? false) || (touched ?? false) : false;
}

writeValue(source: string) {
Expand Down

0 comments on commit 8952509

Please sign in to comment.