Search code examples
angularangular-materialangular8controlvalueaccessor

How to mat-button-toggle by default selected in angular 8 using ControlValueAccessor


I'm trying to create an custom element with Angulars ControlValueAccessor. The goal is a switch with three states, true, false and null. Null should be selected by default, I tried some solutions from other posts, but they didn't work.
I tried adding "value=null" in the mat-button-toggle-group, aswell as add the "checked" attribute in the null mat-button-toggle itself.

HTML:

<div class="nullableBoolWrapper" fxLayout="row">
<mat-label class="centred-vertically">{{ label }}</mat-label>
<mat-button-toggle-group class="selection-button"
[disabled]="isDisabled"
[(ngModel)]="selectedValue"
(change)="changed($event)">
    <mat-button-toggle ngDefaultControl value=true>{{ descriptionList[0].description }}</mat-button-toggle>
    <mat-button-toggle ngDefaultControl value=false>{{ descriptionList[1].description }}</mat-button-toggle>
    <mat-button-toggle ngDefaultControl value=null [disabled]="!selectableNull">{{ descriptionList[2].description }}</mat-button-toggle>
</mat-button-toggle-group>

TS:

import { Description } from './../core/models/description';
import { Component, ViewEncapsulation, forwardRef, Input, OnInit } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

export const NULLABLE_BOOL_VALUE_ACCESSOR: any = {
  provide: NG_VALUE_ACCESSOR,
  useExisting: forwardRef(() => NullableBoolComponent),
  multi: true
};

@Component({
  selector: 'nullable-bool',
  templateUrl: './nullable-bool.component.html',
  styleUrls: ['./nullable-bool.component.scss'],
  providers: [NULLABLE_BOOL_VALUE_ACCESSOR]
})
export class NullableBoolComponent implements ControlValueAccessor, OnInit {

  constructor() { }

  @Input('descriptionList') descriptionList: Description[];
  @Input('selectableNull') selectableNull = false;
  @Input('label') label: string;

  public selectedValue: boolean | null = null;
  public isDisabled = false;

  private defaultList: Description[] = [{ description: 'Yes' }, { description: 'No' }, { description: 'Not set' }];
  private onChange: any = () => {};
  private onTouch: any = () => {};

  writeValue(val: boolean | null): void { this.selectedValue = val; }

  registerOnChange(fn: any): void { this.onChange = fn; }

  registerOnTouched(fn: any): void { this.onTouch = fn; }

  setDisabledState?(isDisabled: boolean): void { this.isDisabled = isDisabled; }

  ngOnInit() { this.descriptionList === undefined ? this.descriptionList = this.defaultList : null; }

  changed($event: any): void {
    this.onTouch();
    this.onChange($event);
  }
}

Solution:

<div class="nullableBoolWrapper" fxLayout="row">
    <mat-label class="centred-vertically">{{ label }}</mat-label>
    <mat-button-toggle-group class="selection-button"
    [disabled]="isDisabled"
    (change)="changed($event)" #gro="matButtonToggleGroup" value="null">
        <mat-button-toggle ngDefaultControl [value]="true">{{ descriptionList[0].description }}</mat-button-toggle>
        <mat-button-toggle ngDefaultControl [value]="false">{{ descriptionList[1].description }}</mat-button-toggle>
        <mat-button-toggle ngDefaultControl value="null" [disabled]="!selectableNull">{{ descriptionList[2].description }}</mat-button-toggle>
    </mat-button-toggle-group>
</div>

In my case I had to remove the ngModel.

Greetings


Solution

  • So I did some research and the problem is that the [value]="null" will assign null to the value attribute unlike if you have value=null then it assigns "null" instead of null.

    and "null" is not equal to null so it won't get selected.

    It has 2 solutions.

    I have made you an example in stackblitz

    Hope you understand what I mean.