From 1eac82f1ad44c72f1972f8450f90e7354d65148a Mon Sep 17 00:00:00 2001 From: gracetxgao <101677420+gracetxgao@users.noreply.github.com> Date: Tue, 17 Dec 2024 21:59:47 -0800 Subject: [PATCH] change value detection in input box --- .../input-error/input-error.component.html | 7 +++- .../input-with-form-field.component.html | 4 +-- .../input-with-form-field.component.ts | 7 +--- .../form/form-field/form-field-control.ts | 4 ++- .../form-field/form-field-theme.scss | 6 ++-- .../form-field/form-field.component.html | 5 +-- .../form-field/form-field.component.spec.ts | 2 +- .../form-field/form-field.component.ts | 32 ++++--------------- .../src/atoms/form/input/input.component.scss | 4 +++ .../src/atoms/form/input/input.component.ts | 6 +++- .../native-select/native-select.component.ts | 5 ++- .../quantity-field.component.ts | 11 ++++++- 12 files changed, 50 insertions(+), 43 deletions(-) diff --git a/libs/design/input/examples/src/input-error/input-error.component.html b/libs/design/input/examples/src/input-error/input-error.component.html index fd0f0dcd2b..f052626424 100644 --- a/libs/design/input/examples/src/input-error/input-error.component.html +++ b/libs/design/input/examples/src/input-error/input-error.component.html @@ -1,6 +1,11 @@ -
Error message goes here.
+ @if (control.errors?.required) { + Email is a required field. + } + @if (control.errors?.email) { + Email is not valid. + }

Value: {{ control.value }}

\ No newline at end of file diff --git a/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.html b/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.html index cfb8badd83..ef1b7b5a94 100644 --- a/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.html +++ b/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.html @@ -1,5 +1,5 @@ - - + + \ No newline at end of file diff --git a/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.ts b/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.ts index cc2060fa98..402922bf67 100644 --- a/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.ts +++ b/libs/design/input/examples/src/input-with-form-field/input-with-form-field.component.ts @@ -2,10 +2,6 @@ import { ChangeDetectionStrategy, Component, } from '@angular/core'; -import { - ReactiveFormsModule, - UntypedFormControl, -} from '@angular/forms'; import { DaffFormFieldModule, @@ -18,8 +14,7 @@ import { templateUrl: './input-with-form-field.component.html', changeDetection: ChangeDetectionStrategy.OnPush, standalone: true, - imports: [DaffFormFieldModule, DaffInputModule, ReactiveFormsModule], + imports: [DaffFormFieldModule, DaffInputModule], }) export class InputWithFormFieldComponent { - control: UntypedFormControl = new UntypedFormControl(); } diff --git a/libs/design/src/atoms/form/form-field/form-field-control.ts b/libs/design/src/atoms/form/form-field/form-field-control.ts index 43581ba7c3..f2792ce5b6 100644 --- a/libs/design/src/atoms/form/form-field/form-field-control.ts +++ b/libs/design/src/atoms/form/form-field/form-field-control.ts @@ -11,7 +11,7 @@ import { NgControl } from '@angular/forms'; * in javascript, they get thrown out by the typescript compiler and cannot * be used for the necessary dependency injection. */ -export abstract class DaffFormFieldControl { +export abstract class DaffFormFieldControl { readonly ngControl: NgControl | null; readonly controlType?: any; @@ -19,4 +19,6 @@ export abstract class DaffFormFieldControl { readonly focused: boolean; abstract focus(event?: Event): void; + + readonly value: T; }; diff --git a/libs/design/src/atoms/form/form-field/form-field/form-field-theme.scss b/libs/design/src/atoms/form/form-field/form-field/form-field-theme.scss index c1d40dbf2d..e24c7cc7fd 100644 --- a/libs/design/src/atoms/form/form-field/form-field/form-field-theme.scss +++ b/libs/design/src/atoms/form/form-field/form-field/form-field-theme.scss @@ -34,12 +34,14 @@ } } - &:has(input:disabled) { + &:has(*:disabled), + &:has(*.isDisabled) { border: 1px solid theming.daff-illuminate($base, $neutral, 4); color: theming.daff-illuminate($base, $neutral, 4); } - input:disabled { + *:disabled, + .isDisabled { background-color: transparent; } } diff --git a/libs/design/src/atoms/form/form-field/form-field/form-field.component.html b/libs/design/src/atoms/form/form-field/form-field/form-field.component.html index 5d477de62d..02f36e314b 100644 --- a/libs/design/src/atoms/form/form-field/form-field/form-field.component.html +++ b/libs/design/src/atoms/form/form-field/form-field/form-field.component.html @@ -4,7 +4,6 @@ [class.daff-error]="isError" [class.daff-valid]="isValid" [class.daff-filled]="isFilled" - [class.daff-disabled]="isDisabled" >
- +@if (_control?.ngControl?.touched) { + +} \ No newline at end of file diff --git a/libs/design/src/atoms/form/form-field/form-field/form-field.component.spec.ts b/libs/design/src/atoms/form/form-field/form-field/form-field.component.spec.ts index 6bfc53a351..c56fc3b187 100644 --- a/libs/design/src/atoms/form/form-field/form-field/form-field.component.spec.ts +++ b/libs/design/src/atoms/form/form-field/form-field/form-field.component.spec.ts @@ -33,7 +33,7 @@ describe('@daffodil/design | DaffFormFieldComponent | Usage', () => { let component: DaffFormFieldComponent; let fixture: ComponentFixture; let formFieldControlElement: HTMLElement; - let control: DaffFormFieldControl; + let control: DaffFormFieldControl; beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ diff --git a/libs/design/src/atoms/form/form-field/form-field/form-field.component.ts b/libs/design/src/atoms/form/form-field/form-field/form-field.component.ts index 94051fc08c..322f4acd7a 100644 --- a/libs/design/src/atoms/form/form-field/form-field/form-field.component.ts +++ b/libs/design/src/atoms/form/form-field/form-field/form-field.component.ts @@ -21,7 +21,7 @@ import { DaffFormFieldMissingControlMessage } from '../form-field-errors'; styleUrls: ['./form-field.component.scss'], encapsulation: ViewEncapsulation.None, }) -export class DaffFormFieldComponent implements DoCheck, AfterContentInit, AfterContentChecked { +export class DaffFormFieldComponent implements AfterContentInit, AfterContentChecked { /** * @docs-private @@ -44,7 +44,7 @@ export class DaffFormFieldComponent implements DoCheck, AfterContentInit, AfterC * * @docs-private */ - @ContentChild(DaffFormFieldControl) _control: DaffFormFieldControl; + @ContentChild(DaffFormFieldControl) _control: DaffFormFieldControl; /** * Tracking property to keep a record of whether or not the @@ -64,12 +64,6 @@ export class DaffFormFieldComponent implements DoCheck, AfterContentInit, AfterC */ isValid = false; - /** - * Tracking property to keep a record of whether or not the - * form field should be marked as disabled. - */ - // isDisabled = false; - /** * @docs * @@ -79,23 +73,6 @@ export class DaffFormFieldComponent implements DoCheck, AfterContentInit, AfterC return this._control?.focused; } - /** - * Keeps the state of the form field consistent with its child DaffFormControl - * - * TODO: consider whether or not this can be refactored to some kind of - * observable to remove unnecessary change detection. - * - * @docs-private - */ - ngDoCheck() { - if(this._control?.ngControl) { - this.isError = this._control.ngControl.errors && (this._control.ngControl.touched); - this.isValid = !this._control.ngControl.errors && this._control.ngControl.touched; - this.isFilled = !!this._control.ngControl.value; - // this.isDisabled = this._control.ngControl.disabled; - } - } - /** * Validate whether or not the FormField is in * a "usable" state. @@ -126,5 +103,10 @@ export class DaffFormFieldComponent implements DoCheck, AfterContentInit, AfterC */ ngAfterContentChecked() { this._validateFormControl(); + if(this._control?.ngControl) { + this.isError = this._control.ngControl.errors && (this._control.ngControl.touched); + this.isValid = !this._control.ngControl.errors && this._control.ngControl.touched; + } + this.isFilled = !!this._control.value; } } diff --git a/libs/design/src/atoms/form/input/input.component.scss b/libs/design/src/atoms/form/input/input.component.scss index 10416d38ec..fce43549f0 100644 --- a/libs/design/src/atoms/form/input/input.component.scss +++ b/libs/design/src/atoms/form/input/input.component.scss @@ -14,4 +14,8 @@ box-shadow: none; outline: none; } + + &:disabled { + cursor: not-allowed; + } } diff --git a/libs/design/src/atoms/form/input/input.component.ts b/libs/design/src/atoms/form/input/input.component.ts index a19241ee4c..42dddd9ed9 100644 --- a/libs/design/src/atoms/form/input/input.component.ts +++ b/libs/design/src/atoms/form/input/input.component.ts @@ -25,7 +25,7 @@ import { DaffFormFieldControl } from '../form-field/form-field-control'; { provide: DaffFormFieldControl, useExisting: DaffInputComponent }, ], }) -export class DaffInputComponent implements DaffFormFieldControl { +export class DaffInputComponent implements DaffFormFieldControl { /** * Has the form been submitted. @@ -59,4 +59,8 @@ export class DaffInputComponent implements DaffFormFieldControl { onFocus() { this._elementRef.nativeElement.focus(); } + + get value() { + return this._elementRef.nativeElement.value; + } } diff --git a/libs/design/src/atoms/form/native-select/native-select.component.ts b/libs/design/src/atoms/form/native-select/native-select.component.ts index 27015edf34..73a26d99a5 100644 --- a/libs/design/src/atoms/form/native-select/native-select.component.ts +++ b/libs/design/src/atoms/form/native-select/native-select.component.ts @@ -26,7 +26,7 @@ import { DaffFormFieldControl } from '../form-field/form-field-control'; ], }) -export class DaffNativeSelectComponent implements DaffFormFieldControl { +export class DaffNativeSelectComponent implements DaffFormFieldControl { /** * @docs-private */ @@ -66,4 +66,7 @@ export class DaffNativeSelectComponent implements DaffFormFieldControl { this._elementRef.nativeElement.focus(); } + get value() { + return this._elementRef.nativeElement.value; + } } diff --git a/libs/design/src/atoms/form/quantity-field/quantity-field.component.ts b/libs/design/src/atoms/form/quantity-field/quantity-field.component.ts index eece940b35..b2d2062b91 100644 --- a/libs/design/src/atoms/form/quantity-field/quantity-field.component.ts +++ b/libs/design/src/atoms/form/quantity-field/quantity-field.component.ts @@ -29,7 +29,7 @@ import { DaffFormFieldControl } from '../form-field/form-field-control'; ], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class DaffQuantityFieldComponent implements ControlValueAccessor, DaffFormFieldControl { +export class DaffQuantityFieldComponent implements ControlValueAccessor, DaffFormFieldControl { @ViewChild(DaffQuantityInputComponent) input: DaffQuantityInputComponent; @ViewChild(DaffQuantitySelectComponent) select: DaffQuantitySelectComponent; @@ -139,4 +139,13 @@ export class DaffQuantityFieldComponent implements ControlValueAccessor, DaffFor this.input.focus(); } } + + get value() { + if(this.select) { + return this.select.value; + } + if(this.input) { + return this.input.value; + } + } }