File

projects/ng-dynamic-forms/core/src/lib/model/dynamic-option-control.model.ts

Extends

DynamicFormValueControlModel

Index

Properties
Methods
Accessors

Constructor

Protected constructor(config: DynamicOptionControlModelConfig, layout?: DynamicFormControlLayout)
Parameters :
Name Type Optional
config DynamicOptionControlModelConfig<T> No
layout DynamicFormControlLayout Yes

Properties

Private _options
Type : DynamicFormOption<T>[]
Default value : []
Decorators :
@serializable('options')
options$
Type : Observable<DynamicFormOption[]>
Private _value
Type : T | null
Decorators :
@serializable('value')
additional
Type : literal type | null
Decorators :
@serializable()
hint
Type : string | null
Decorators :
@serializable()
required
Type : boolean
Decorators :
@serializable()
tabIndex
Type : number | null
Decorators :
@serializable()
Private Readonly value$
Type : BehaviorSubject<T>
Readonly valueChanges
Type : Observable<T>
_disabled
Type : boolean
Decorators :
@serializable('disabled')
asyncValidators
Type : DynamicValidatorsConfig | null
Decorators :
@serializable()
controlTooltip
Type : string | null
Decorators :
@serializable()
Private Readonly disabled$
Type : BehaviorSubject<boolean>
Readonly disabledChanges
Type : Observable<boolean>
errorMessages
Type : DynamicValidatorsConfig | null
Decorators :
@serializable()
hidden
Type : boolean
Decorators :
@serializable()
id
Type : string
Decorators :
@serializable()
label
Type : string | null
Decorators :
@serializable()
labelTooltip
Type : string | null
Decorators :
@serializable()
layout
Type : DynamicFormControlLayout | null
Decorators :
@serializable()
name
Type : string
Decorators :
@serializable()
parent
Type : DynamicPathable | null
Default value : null
relations
Type : DynamicFormControlRelation[]
Decorators :
@serializable()
Abstract Readonly type
Type : string
updateOn
Type : DynamicFormHook | null
Decorators :
@serializable()
validators
Type : DynamicValidatorsConfig | null
Decorators :
@serializable()
_disabled
Type : boolean
Decorators :
@serializable('disabled')
Inherited from DynamicFormControlModel
asyncValidators
Type : DynamicValidatorsConfig | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel
controlTooltip
Type : string | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel
Private Readonly disabled$
Type : BehaviorSubject<boolean>
Inherited from DynamicFormControlModel
Readonly disabledChanges
Type : Observable<boolean>
Inherited from DynamicFormControlModel
errorMessages
Type : DynamicValidatorsConfig | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel
hidden
Type : boolean
Decorators :
@serializable()
Inherited from DynamicFormControlModel
id
Type : string
Decorators :
@serializable()
Inherited from DynamicFormControlModel
label
Type : string | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel
labelTooltip
Type : string | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel
layout
Type : DynamicFormControlLayout | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel
name
Type : string
Decorators :
@serializable()
Inherited from DynamicFormControlModel
parent
Type : DynamicPathable | null
Default value : null
Inherited from DynamicFormControlModel
relations
Type : DynamicFormControlRelation[]
Decorators :
@serializable()
Inherited from DynamicFormControlModel
Abstract Readonly type
Type : string
Inherited from DynamicFormControlModel
updateOn
Type : DynamicFormHook | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel
validators
Type : DynamicValidatorsConfig | null
Decorators :
@serializable()
Inherited from DynamicFormControlModel

Methods

add
add(optionConfig: DynamicFormOptionConfig)
Parameters :
Name Type Optional
optionConfig DynamicFormOptionConfig<T> No
get
get(index: number)
Parameters :
Name Type Optional
index number No
insert
insert(index: number, optionConfig: DynamicFormOptionConfig)
Parameters :
Name Type Optional
index number No
optionConfig DynamicFormOptionConfig<T> No
remove
remove(...indices: number[])
Parameters :
Name Type Optional
indices number[] No
Returns : void
Abstract select
select(...indices: number[])
Parameters :
Name Type Optional
indices number[] No
Returns : void
Private updateOptions$
updateOptions$()
Returns : void
getAdditional
getAdditional(key: string, defaultValue?: any | null)
Parameters :
Name Type Optional
key string No
defaultValue any | null Yes
Returns : any
toJSON
toJSON()
Returns : any
toJSON
toJSON()
Inherited from DynamicFormControlModel
Returns : any

Accessors

options
getoptions()
setoptions(options: any)
Parameters :
Name Type Optional
options any No
Returns : void
import { Observable, isObservable, of } from "rxjs";
import { map } from "rxjs/operators";
import { DynamicFormValueControlModel, DynamicFormValueControlModelConfig } from "./dynamic-form-value-control.model";
import { DynamicFormControlLayout } from "./misc/dynamic-form-control-layout.model";
import { serializable, serialize } from "../decorator/serializable.decorator";
import { isBoolean } from "../utils/core.utils";

export interface DynamicFormOptionConfig<T> {

    disabled?: boolean;
    label?: string;
    value: T;
}

export class DynamicFormOption<T> {

    @serializable() disabled: boolean;
    @serializable() label: string | null;
    @serializable() value: T;

    constructor(config: DynamicFormOptionConfig<T>) {

        this.disabled = isBoolean(config.disabled) ? config.disabled : false;
        this.label = config.label || null;
        this.value = config.value;
    }

    get text() {
        return this.label;
    }

    set text(text: string | null) {
        this.label = text;
    }

    toJSON() {
        return serialize(this);
    }
}

export interface DynamicOptionControlModelConfig<T> extends DynamicFormValueControlModelConfig<T | T[]> {

    options?: DynamicFormOptionConfig<T>[] | Observable<DynamicFormOptionConfig<T>[]>;
}

export abstract class DynamicOptionControlModel<T> extends DynamicFormValueControlModel<T | T[]> {

    @serializable("options") private _options: DynamicFormOption<T>[] = [];
    options$: Observable<DynamicFormOption<T>[]>;

    protected constructor(config: DynamicOptionControlModelConfig<T>, layout?: DynamicFormControlLayout) {

        super(config, layout);

        this.options = config.options;
    }

    private updateOptions$(): void {
        this.options$ = of(this.options);
    }

    set options(options: any) {

        if (Array.isArray(options)) {

            this._options = (options as DynamicFormOptionConfig<T>[]).map(optionConfig => new DynamicFormOption<T>(optionConfig));

            this.updateOptions$();

        } else if (isObservable(options)) {

            this.options$ = (options as Observable<DynamicFormOptionConfig<T>[]>).pipe(
                map(optionsConfig => {

                    this._options = optionsConfig.map(optionConfig => new DynamicFormOption<T>(optionConfig));

                    return this._options;
                }));

        } else {

            this.updateOptions$();
        }
    }

    get options(): any {
        return this._options;
    }

    add(optionConfig: DynamicFormOptionConfig<T>): DynamicFormOption<T> {
        return this.insert(this.options.length, optionConfig);
    }

    get(index: number): DynamicFormOption<T> {
        return this.options[index];
    }

    insert(index: number, optionConfig: DynamicFormOptionConfig<T>): DynamicFormOption<T> {

        const option = new DynamicFormOption(optionConfig);

        this.options.splice(index, 0, option);
        this.updateOptions$();

        return option;
    }

    remove(...indices: number[]): void {

        indices.forEach(index => this.options.splice(index, 1));
        this.updateOptions$();
    }

    abstract select(...indices: number[]): void;
}

result-matching ""

    No results matching ""