src/app/core/common-components/entity-form/current-record-state.ts

Description

Gives a field access to the current state of the record being edited, for fields that depend on the values of other fields of that record (like the selectionCondition of an entity field).

Provided by the component rendering the field (EntityFieldEditComponent), so every field component below it can ask for it with an optional inject().

Relationships

Depends on

No results matching.

Index

Properties

Properties

Abstract Readonly currentRecord
Type : Signal<Entity | undefined>

The record including the current, possibly unsaved values of its form, updated with every change. undefined if no single record is being edited.

import { computed, inject, Signal } from "@angular/core";
import { AbstractControl, FormGroup } from "@angular/forms";
import { Entity } from "../../entity/model/entity";

/**
 * Gives a field access to the current state of the record being edited,
 * for fields that depend on the values of other fields of that record
 * (like the `selectionCondition` of an entity field).
 *
 * Provided by the component rendering the field (`EntityFieldEditComponent`),
 * so every field component below it can ask for it with an optional `inject()`.
 */
export abstract class CurrentRecordContext {
  /**
   * The record including the current, possibly unsaved values of its form, updated with every change.
   * `undefined` if no single record is being edited.
   */
  abstract readonly currentRecord: Signal<Entity | undefined>;
}

/**
 * Copy of the record with the current, possibly unsaved values of its form applied
 * (including disabled and hidden fields).
 */
export function createCurrentRecordState<T extends Entity>(
  entity: T,
  form: FormGroup,
): T {
  const state = entity.copy();
  Object.assign(state, form.getRawValue());
  return state;
}

/**
 * The record with the current values of the form the given control belongs to
 * (for validators, which only get their control),
 * or the record itself if the control is not part of a form.
 *
 * Validators build this themselves instead of reading `EntityForm.currentRecord`:
 * they run before the form notifies of the change that triggered them.
 */
export function getCurrentRecordOfControl<T extends Entity>(
  control: AbstractControl,
  record: T | undefined,
): T | undefined {
  return record && control.parent instanceof FormGroup
    ? createCurrentRecordState(record, control.parent)
    : record;
}

/**
 * The current state of the record a field belongs to (see {@link CurrentRecordContext}),
 * followed only while `follow()` is true, otherwise `undefined`.
 * Must be called in an injection context (e.g. as a field initializer of a component).
 */
export function currentRecordSignal<T extends Entity = Entity>(
  follow: () => boolean,
): Signal<T | undefined> {
  const context = inject(CurrentRecordContext, { optional: true });
  return computed(() =>
    follow() ? (context?.currentRecord() as T | undefined) : undefined,
  );
}

results matching ""

    No results matching ""