aboutsummaryrefslogtreecommitdiffstats
path: root/src/angular/form-elements/radios/radio-buttons-group.component.ts
blob: 800d8b0c8a62cec820c27b4cdda55dc5f5b6db04 (plain)
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
import { Component, Input, Output, ViewEncapsulation, EventEmitter, HostBinding } from "@angular/core";
import { Direction, IOptionGroup, IRadioButtonModel } from "./radio-button.model";
import template from './radio-buttons-group.component.html';

@Component({
    selector: 'sdc-radio-group',
    template: template,
    encapsulation: ViewEncapsulation.None
})
export class RadioGroupComponent {

    private _direction: Direction = Direction.vertical;
    private _selectedValue: string;

    @HostBinding('class') classes = 'sdc-radio-group';

    @Input() public legend: string;
    @Input() public options: IOptionGroup;
    @Input() public disabled: boolean;

    @Input()
    get value(): string {
        return this._selectedValue;
    }
    set value(value: string) {
        if (this.isOptionExists(value)) {
            this._selectedValue = value;
        }
    }

    @Output() public valueChange: EventEmitter<string> = new EventEmitter<string>();

    @Input()
    get direction(): string {
        return Direction[this._direction];
    }
    set direction(direction: string) {
        this._direction = (direction === 'horizontal' ? Direction.horizontal : Direction.vertical);
    }

    public onValueChanged(value): void {
        this.valueChange.emit(value);
    }

    private isOptionExists(value) {
        const exist = this.options.items.find((item: IRadioButtonModel) => {
            return item.value === value;
        });
        return exist !== undefined;
    }

}