src/app/core/default-values/x-static/admin-default-value-static/admin-default-value-static.component.ts
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.
CustomFormControlDirective<DefaultValueConfigStatic>
| changeDetection | ChangeDetectionStrategy.OnPush |
| providers |
AdminDefaultValueStaticComponent
|
| selector | app-admin-default-value-static |
| standalone | true |
| imports | |
| styleUrls | ./admin-default-value-static.component.scss |
| templateUrl | ./admin-default-value-static.component.html |
| styleUrl | ./admin-default-value-static.component.scss |
Properties |
Methods |
Inputs |
Outputs |
constructor()
|
| entitySchemaField | |
Type : EntitySchemaField
|
|
| Required : true | |
| aria-describedby | |
Type : string
|
|
|
Inherited from
CustomFormControlDirective
|
|
| disabled | |
Type : boolean
|
|
|
Inherited from
CustomFormControlDirective
|
|
| ngControl | |
Type : any
|
|
Default value : inject(NgControl, { optional: true, self: true })
|
|
|
Inherited from
CustomFormControlDirective
|
|
| placeholder | |
Type : string
|
|
|
Inherited from
CustomFormControlDirective
|
|
| required | |
Type : boolean
|
|
|
Inherited from
CustomFormControlDirective
|
|
| value | |
Type : T
|
|
|
Inherited from
CustomFormControlDirective
|
|
| valueChange | |
Type : EventEmitter
|
|
|
Inherited from
CustomFormControlDirective
|
|
| writeValue | ||||||||||||
writeValue(value: DefaultValueConfigStatic, notifyFormControl: unknown)
|
||||||||||||
|
Inherited from
CustomFormControlDirective
|
||||||||||||
|
Parameters :
Returns :
void
|
| blur |
blur()
|
|
Inherited from
CustomFormControlDirective
|
|
Returns :
void
|
| focus |
focus()
|
|
Inherited from
CustomFormControlDirective
|
|
Returns :
void
|
| onContainerClick | ||||||
onContainerClick(event: MouseEvent)
|
||||||
|
Inherited from
CustomFormControlDirective
|
||||||
|
Parameters :
Returns :
void
|
| registerOnChange | ||||||
registerOnChange(fn: any)
|
||||||
|
Inherited from
CustomFormControlDirective
|
||||||
|
Parameters :
Returns :
void
|
| registerOnTouched | ||||||
registerOnTouched(fn: any)
|
||||||
|
Inherited from
CustomFormControlDirective
|
||||||
|
Parameters :
Returns :
void
|
| setDescribedByIds | ||||||
setDescribedByIds(ids: string[])
|
||||||
|
Inherited from
CustomFormControlDirective
|
||||||
|
Parameters :
Returns :
void
|
| setDisabledState | ||||||
setDisabledState(isDisabled: boolean)
|
||||||
|
Inherited from
CustomFormControlDirective
|
||||||
|
Parameters :
Returns :
void
|
| 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>
|
| controlType |
Type : string
|
Default value : "custom-control"
|
|
Inherited from
CustomFormControlDirective
|
| elementRef |
Type : unknown
|
Default value : inject<ElementRef<HTMLElement>>(ElementRef)
|
|
Inherited from
CustomFormControlDirective
|
| Readonly enabled |
Type : Signal<boolean>
|
Default value : computed(() => !this._disabled())
|
|
Inherited from
CustomFormControlDirective
|
|
Whether the control is currently enabled, as a signal (tracks disabled). |
| errorStateMatcher |
Type : unknown
|
Default value : inject(ErrorStateMatcher)
|
|
Inherited from
CustomFormControlDirective
|
| id |
Type : unknown
|
Default value : `custom-form-control-${CustomFormControlDirective.nextId++}`
|
|
Inherited from
CustomFormControlDirective
|
| Static nextId |
Type : number
|
Default value : 0
|
|
Inherited from
CustomFormControlDirective
|
| onChange |
Type : unknown
|
Default value : () => {...}
|
|
Inherited from
CustomFormControlDirective
|
| onTouched |
Type : unknown
|
Default value : () => {...}
|
|
Inherited from
CustomFormControlDirective
|
| parentForm |
Type : unknown
|
Default value : inject(NgForm, { optional: true })
|
|
Inherited from
CustomFormControlDirective
|
| parentFormGroup |
Type : unknown
|
Default value : inject(FormGroupDirective, { optional: true })
|
|
Inherited from
CustomFormControlDirective
|
| stateChanges |
Type : unknown
|
Default value : new Subject<void>()
|
|
Inherited from
CustomFormControlDirective
|
| Readonly valueSignal |
Type : Signal<T>
|
Default value : computed(() => this._value())
|
|
Inherited from
CustomFormControlDirective
|
|
The current value of the control as a signal.
Authoritative in both modes: it reflects the bound |
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;
}
}
}