Move padding into date formatter

This commit is contained in:
Michael Shamoon 2022-03-11 00:28:06 -08:00
parent a1fd9f7310
commit 4c7eb34290
3 changed files with 8 additions and 18 deletions

View file

@ -1,9 +1,6 @@
import { Component, forwardRef, Input, OnInit, ViewChild } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { NgbDateAdapter, NgbDateParserFormatter, NgbDatepickerContent } from '@ng-bootstrap/ng-bootstrap';
import { Component, forwardRef, OnInit } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { SettingsService } from 'src/app/services/settings.service';
import { LocalizedDateParserFormatter } from 'src/app/utils/ngb-date-parser-formatter';
import { v4 as uuidv4 } from 'uuid';
import { AbstractInputComponent } from '../abstract-input';
@ -30,17 +27,6 @@ export class DateComponent extends AbstractInputComponent<string> implements OnI
placeholder: string
// Allow dates to be specified without 'padding' e.g. 2/3
onFocusOut() {
if (!this.value || this.value.length > 8) return; // its already been formatted
if ([',','.','/','-'].some(sep => this.value.includes(sep))) {
let valArr = this.value.split(/[\.,\/-]+/)
valArr = valArr.map(segment => segment.padStart(2,'0'))
let dateFormatter = new LocalizedDateParserFormatter(this.settings)
this.value = dateFormatter.preformatDateInput(valArr.join(''))
}
}
// prevent chars other than numbers and separators
onKeyPress(event: KeyboardEvent) {
if (!/[0-9,\.\/-]+/.test(event.key)) {