Browse Source

UI: Add dynamic min time validator for time unit input

pull/14765/head
ArtemDzhereleiko 9 months ago
parent
commit
c9bffc9875
  1. 2
      ui-ngx/src/app/shared/components/time-unit-input.component.html
  2. 17
      ui-ngx/src/app/shared/components/time-unit-input.component.ts

2
ui-ngx/src/app/shared/components/time-unit-input.component.html

@ -22,7 +22,7 @@
@if (labelText && !inlineField) { @if (labelText && !inlineField) {
<mat-label>{{ labelText }}</mat-label> <mat-label>{{ labelText }}</mat-label>
} }
<input type="number" min="0" step="1" matInput formControlName="time" placeholder="{{ 'widget-config.set' | translate }}"> <input type="number" [min]="minValueValidator" step="1" matInput formControlName="time" placeholder="{{ 'widget-config.set' | translate }}">
<div matSuffix> <div matSuffix>
<ng-content select="[matSuffix]"></ng-content> <ng-content select="[matSuffix]"></ng-content>
</div> </div>

17
ui-ngx/src/app/shared/components/time-unit-input.component.ts

@ -120,6 +120,8 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
[TimeUnit.SECONDS, SECOND/SECOND], [TimeUnit.SECONDS, SECOND/SECOND],
]); ]);
minValueValidator = 0;
private modelValue: number; private modelValue: number;
private propagateChange: (value: any) => void = () => {}; private propagateChange: (value: any) => void = () => {};
@ -144,8 +146,9 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
); );
} }
if (isDefinedAndNotNull(this.minTime)) { if (isDefinedAndNotNull(this.minTime)) {
this.minValueValidator = this.minValue;
validators.push((control: AbstractControl) => validators.push((control: AbstractControl) =>
Validators.min(Math.ceil(this.minTime / this.timeIntervalsInSec.get(this.timeInputForm.get('timeUnit').value)))(control) Validators.min(this.minValueValidator)(control)
); );
} }
@ -160,6 +163,9 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
this.timeInputForm.get('timeUnit').valueChanges.pipe( this.timeInputForm.get('timeUnit').valueChanges.pipe(
takeUntilDestroyed(this.destroyRef) takeUntilDestroyed(this.destroyRef)
).subscribe(() => { ).subscribe(() => {
if (isDefinedAndNotNull(this.minTime)) {
this.minValueValidator = this.minValue;
}
this.timeInputForm.get('time').updateValueAndValidity({onlySelf: true}); this.timeInputForm.get('time').updateValueAndValidity({onlySelf: true});
this.timeInputForm.get('time').markAsTouched({onlySelf: true}); this.timeInputForm.get('time').markAsTouched({onlySelf: true});
}); });
@ -171,6 +177,10 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
}); });
} }
get minValue(): number {
return Math.ceil(this.minTime / this.timeIntervalsInSec.get(this.timeInputForm.get('timeUnit').value));
}
get hasError(): string { get hasError(): string {
if (this.timeInputForm.get('time').hasError('required') && this.requiredText) { if (this.timeInputForm.get('time').hasError('required') && this.requiredText) {
return this.requiredText; return this.requiredText;
@ -194,6 +204,11 @@ export class TimeUnitInputComponent implements ControlValueAccessor, Validator,
this.timeInputForm.get('time').updateValueAndValidity({emitEvent: false}); this.timeInputForm.get('time').updateValueAndValidity({emitEvent: false});
} }
} }
if (propName === 'minTime') {
if (isDefinedAndNotNull(this.minTime)) {
this.minValueValidator = this.minValue;
}
}
} }
} }
} }

Loading…
Cancel
Save