0

I’m trying to apply styles to input by clicking. I need an orange frame around the field when I press it, if I use a focus then it is used for a moment and then disappears as it moves into the calendar field. I solved the problem this way.

        <div class="input-group">
          <input id="dateOfReceipt" class="form-control date calendar_input pl-4" name="dp" ngbDatepicker #dR="ngbDatepicker"
                 [readOnly]="true"
                 [minDate]="dateOfReceiptStart"
                 [markDisabled]="markDisabled"
                 [placeholder]="bus.seat.fields.jobOfferBodyFields.dateOfReceipt ?
                 bus.seat.fields.jobOfferBodyFields.dateOfReceipt : datePickerPlaceholder"
                 [(ngModel)]="bus.seat.fields.jobOfferBodyFields.dateOfReceipt"
                 [disabled]="!bus.isEdit" (click)="dR.toggle(); showDatepickerBorder('dateOfReceipt')"
                 (dateSelect)="onDateOfReceiptDateSelect($event)"
                 [ngClass]="{'focus-border': datePickerInputFocusBorder.dateOfReceipt}">
          <div class="input-group-append">
            <button class="btn calendar-button" type="button"
                    [disabled]="!bus.isEdit" (click)="dR.toggle(); showDatepickerBorder('dateOfReceipt')">
              <mat-icon class="calendar-icon">calendar_today</mat-icon>
            </button>
          </div>
        </div>


  showDatepickerBorder(key: string) {
    this.datePickerInputFocusBorder[key] = true;
  }

  hideDatepickerBorder(obj) {
    for (let prop in obj) {
      if (obj.hasOwnProperty(prop)) {
        obj[prop] = false;
      }
    }
  }

  @HostListener('document:click', ['$event']) clickOutside(event) {
    if (!this.eRef.nativeElement.contains(event.target)) {
      this.hideDatepickerBorder(this.datePickerInputFocusBorder);
    }
  }

How can I make it easier ?

1 Answers1

0

you can simply ask about "dR.isOpen()" -dR is the template reference variable you use- and use [ngClass] If you has

input.form-control:focus, .focus-border{
    box-shadow: 0 0 0 .2rem rgba(255,140,0,.25);
    border:orange
}

You can use

<input class="form-control" ngbDatepicker #dR="ngbDatepicker"
         [ngClass]="dR.isOpen()?'focus-border':null" ...  >
Eliseo
  • 50,109
  • 4
  • 29
  • 67