All files / lib/dynamic-form-element/dynamic-form-items dynamic-form-items.ts

100% Statements 15/15
100% Branches 9/9
100% Functions 8/8
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58                                                16x     15x 9x     13x 13x       2x 1x         16x 14x 14x         13x 16x 16x 16x 16x        
import { DynamicForm } from '../../dynamic-form/dynamic-form';
import { DynamicFormBuilder } from '../../dynamic-form/dynamic-form.builder';
import { DynamicFormElement } from '../dynamic-form-element';
import { DynamicFormElementType } from '../dynamic-form-element-type';
import { DynamicFormItem } from './dynamic-form-item';
import { DynamicFormItemsDefinition } from './dynamic-form-items-definition';
import { DynamicFormItemsTemplate } from './dynamic-form-items-template';
 
export class DynamicFormItems<
  Template extends DynamicFormItemsTemplate = DynamicFormItemsTemplate,
  Definition extends DynamicFormItemsDefinition<Template> = DynamicFormItemsDefinition<Template>,
  Item extends DynamicFormItem = DynamicFormItem
> extends DynamicFormElement<Template, Definition, Item> {
 
  protected _selectedIndex: number;
  protected _selectedItem: Item;
 
  constructor(
    builder: DynamicFormBuilder,
    root: DynamicForm,
    parent: DynamicFormElement,
    definition: Definition,
    type: DynamicFormElementType,
  ) {
    super(builder, root, parent, definition, type);
  }
 
  get selectedIndex(): number { return this._selectedIndex; }
  get selectedItem(): Item { return this._selectedItem; }
 
  override init(): void {
    super.init();
    this.selectItem(0);
  }
 
  check(): void {
    if (this._selectedItem && this._selectedItem.disabled) {
      this.selectItem(0);
    }
  }
 
  selectItem(index: number): void {
    if (index >= 0 && index < this._children.length && !this._children[index].disabled) {
      this._selectedIndex = index;
      this._selectedItem = this._children[index];
    }
  }
 
  protected override getChildren(): Item[] {
    return (this.definition.children || []).map((childDefinition, index) => {
      const itemDefinition = { ...this._builder.getDefinition(childDefinition, this.root), index };
      const item = new DynamicFormItem(this._builder, this.root, this.parent, itemDefinition);
      item.init();
      return item as Item;
    });
  }
}