src/app/core/default-values/x-static/admin-default-value-static/admin-default-value-static.component.ts

Description

UI to edit a static default value in the Admin UI. Note that the Input/Output of values is in database-format (not entity-format) as this is the format how it should be saved in the config.

Extends

CustomFormControlDirective<DefaultValueConfigStatic>

Example

Metadata

Relationships

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor()

Inputs

entitySchemaField
Type : EntitySchemaField
Required :  true
aria-describedby
Type : string
disabled
Type : boolean
ngControl
Type : any
Default value : inject(NgControl, { optional: true, self: true })
placeholder
Type : string
required
Type : boolean
value
Type : T

Outputs

valueChange
Type : EventEmitter

Methods

writeValue
writeValue(value: DefaultValueConfigStatic, notifyFormControl: unknown)
Parameters :
Name Type Optional Default value
value DefaultValueConfigStatic No
notifyFormControl unknown No false
Returns : void
blur
blur()
Returns : void
focus
focus()
Returns : void
onContainerClick
onContainerClick(event: MouseEvent)
Parameters :
Name Type Optional
event MouseEvent No
Returns : void
registerOnChange
registerOnChange(fn: any)
Parameters :
Name Type Optional
fn any No
Returns : void
registerOnTouched
registerOnTouched(fn: any)
Parameters :
Name Type Optional
fn any No
Returns : void
setDescribedByIds
setDescribedByIds(ids: string[])
Parameters :
Name Type Optional
ids string[] No
Returns : void
setDisabledState
setDisabledState(isDisabled: boolean)
Parameters :
Name Type Optional
isDisabled boolean No
Returns : void

Properties

internalControl
Type : unknown
Default value : new FormControl<unknown>(null)
staticvalueForm
Type : EntityForm<Entity>
Default value : { formGroup: new FormGroup({ defaultValueId: this.internalControl, }), } as unknown as EntityForm<Entity>
targetFieldConfig
Type : unknown
Default value : linkedSignal<FormFieldConfig>(() => ({ ...this.entitySchemaField(), id: "defaultValueId", // overwrite the id with a static temporary one for our isolated form control here }))

mapped from the entitySchemaField to use for the entity-field-edit field

controlType
Type : string
Default value : "custom-control"
elementRef
Type : unknown
Default value : inject<ElementRef<HTMLElement>>(ElementRef)
Readonly enabled
Type : Signal<boolean>
Default value : computed(() => !this._disabled())

Whether the control is currently enabled, as a signal (tracks disabled).

errorStateMatcher
Type : unknown
Default value : inject(ErrorStateMatcher)
id
Type : unknown
Default value : `custom-form-control-${CustomFormControlDirective.nextId++}`
Static nextId
Type : number
Default value : 0
onChange
Type : unknown
Default value : () => {...}
onTouched
Type : unknown
Default value : () => {...}
parentForm
Type : unknown
Default value : inject(NgForm, { optional: true })
parentFormGroup
Type : unknown
Default value : inject(FormGroupDirective, { optional: true })
stateChanges
Type : unknown
Default value : new Subject<void>()
Readonly valueSignal
Type : Signal<T>
Default value : computed(() => this._value())

The current value of the control as a signal. Authoritative in both modes: it reflects the bound FormControl (synced in ngDoCheck) as well as [(value)] / writeValue updates.

import { EntityForm } from "#src/app/core/common-components/entity-form/entity-form";
import { EntityFieldEditComponent } from "#src/app/core/entity/entity-field-edit/entity-field-edit.component";
import {
  Component,
  inject,
  ChangeDetectionStrategy,
  input,
  linkedSignal,
} from "@angular/core";
import { FormControl, FormGroup, ReactiveFormsModule } from "@angular/forms";
import { MatFormFieldControl } from "@angular/material/form-field";
import { FormFieldConfig } from "app/core/common-components/entity-form/FormConfig";
import { Entity } from "app/core/entity/model/entity";
import { EntitySchemaField } from "app/core/entity/schema/entity-schema-field";
import { EntitySchemaService } from "app/core/entity/schema/entity-schema.service";
import { CustomFormControlDirective } from "../../../common-components/basic-autocomplete/custom-form-control.directive";
import { DefaultValueConfigStatic } from "../default-value-config-static";

/**
 * UI to edit a static default value in the Admin UI.
 * Note that the Input/Output of values is in database-format (not entity-format)
 * as this is the format how it should be saved in the config.
 */
@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: "app-admin-default-value-static",
  imports: [ReactiveFormsModule, EntityFieldEditComponent],
  templateUrl: "./admin-default-value-static.component.html",
  styleUrl: "./admin-default-value-static.component.scss",
  providers: [
    {
      provide: MatFormFieldControl,
      useExisting: AdminDefaultValueStaticComponent,
    },
  ],
})
export class AdminDefaultValueStaticComponent extends CustomFormControlDirective<DefaultValueConfigStatic> {
  entitySchemaField = input.required<EntitySchemaField>();

  /** mapped from the entitySchemaField to use for the entity-field-edit field */
  targetFieldConfig = linkedSignal<FormFieldConfig>(() => ({
    ...this.entitySchemaField(),
    id: "defaultValueId", // overwrite the id with a static temporary one for our isolated form control here
  }));

  internalControl = new FormControl<unknown>(null);
  staticvalueForm: EntityForm<Entity> = {
    formGroup: new FormGroup({
      defaultValueId: this.internalControl,
    }),
  } as unknown as EntityForm<Entity>;

  private readonly entitySchemaService = inject(EntitySchemaService);

  constructor() {
    super();
    this.internalControl.setValue(this.getInternalValue(this.value), {
      emitEvent: false,
    });

    this.internalControl.valueChanges.subscribe((v) => this.emitNewValue(v));

    this.ngControl?.valueChanges?.subscribe(
      (newValue: DefaultValueConfigStatic) => {
        this.internalControl.setValue(this.getInternalValue(newValue), {
          emitEvent: false,
        });
        setTimeout(() => this.internalControl.updateValueAndValidity(), 0);
      },
    );
  }

  override writeValue(
    value: DefaultValueConfigStatic,
    notifyFormControl = false,
  ) {
    super.writeValue(value, notifyFormControl);

    const internalValue = this.getInternalValue(value);
    if (
      JSON.stringify(this.internalControl.value) !==
      JSON.stringify(internalValue)
    ) {
      this.internalControl.setValue(internalValue, { emitEvent: false });
      setTimeout(() => this.internalControl.updateValueAndValidity(), 0);
    }
  }

  private getInternalValue(defaultValueConfig: DefaultValueConfigStatic) {
    let value = defaultValueConfig?.value ?? null;
    if (value !== null && value !== undefined) {
      value = this.entitySchemaService.valueToEntityFormat(
        value,
        this.entitySchemaField(),
      );
    }
    return value;
  }

  /**
   * Set the CustomFormControl value as output (after transforming it to desired format).
   * @param newValue
   * @private
   */
  private emitNewValue(newValue: any) {
    // to the outside we want to set the value in the database format for simplified storing in the config object
    if (newValue !== null && newValue !== undefined) {
      newValue = this.entitySchemaService.valueToDatabaseFormat(
        newValue,
        this.entitySchemaField(),
      );
    }

    this.value = { value: newValue };
  }
}
<app-entity-field-edit
  [field]="targetFieldConfig()"
  [form]="staticvalueForm"
  [hideLabel]="true"
></app-entity-field-edit>

./admin-default-value-static.component.scss

app-entity-field-edit {
  ::ng-deep .mat-mdc-form-field {
    width: 100%;

    /* Hides the bottom line of the form field */
    .mdc-line-ripple {
      display: none;
    }
    .mat-mdc-form-field-subscript-wrapper {
      height: 5px;
    }
  }
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""