Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(timepicker): add min max validation #3126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/framework/theme/components/timepicker/timepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ export class NbTimePickerComponent<D> implements OnChanges {

_date: D;

/**
* Minimum available date for selection.
* */
@Input() min: D;

/**
* Maximum available date for selection.
* */
@Input() max: D;

/**
* In timepicker value should be always true
* In calendar-with-time.component should set to false
Expand Down Expand Up @@ -320,6 +330,10 @@ export class NbTimePickerComponent<D> implements OnChanges {
return false;
}

getValidatorConfig() {
return { min: this.min, max: this.max };
}

protected buildColumnOptions(): void {
this.fullTimeOptions = this.singleColumn ? this.calendarTimeModelService.getHoursRange(this.step) : [];

Expand Down
53 changes: 51 additions & 2 deletions src/framework/theme/components/timepicker/timepicker.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ import {
} from '@angular/core';
import { distinctUntilChanged, filter, map, pairwise, startWith, takeUntil } from 'rxjs/operators';
import { fromEvent, merge, Subject, Subscription } from 'rxjs';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import {
ControlValueAccessor,
NG_VALIDATORS,
NG_VALUE_ACCESSOR,
ValidationErrors,
Validator,
ValidatorFn,
Validators,
} from '@angular/forms';
import { NbTimePickerComponent } from './timepicker.component';
import { NbOverlayRef, NbScrollStrategy } from '../cdk/overlay/mapping';
import {
Expand Down Expand Up @@ -176,9 +184,14 @@ import { NB_DOCUMENT } from '../../theme.options';
useExisting: forwardRef(() => NbTimePickerDirective),
multi: true,
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => NbTimePickerDirective),
multi: true,
},
],
})
export class NbTimePickerDirective<D> implements AfterViewInit, ControlValueAccessor {
export class NbTimePickerDirective<D> implements AfterViewInit, ControlValueAccessor, Validator {
/**
* Provides timepicker component.
* */
Expand Down Expand Up @@ -260,6 +273,13 @@ export class NbTimePickerDirective<D> implements AfterViewInit, ControlValueAcce
return !this.isOpen;
}

/**
* Form control validators will be called in validators context, so, we need to bind them.
* */
protected validator: ValidatorFn = Validators.compose(
[this.minValidator, this.maxValidator].map((fn) => fn.bind(this)),
);

constructor(
@Inject(NB_DOCUMENT) protected document,
protected positionBuilder: NbPositionBuilderService,
Expand Down Expand Up @@ -465,6 +485,13 @@ export class NbTimePickerDirective<D> implements AfterViewInit, ControlValueAcce
this.onTouched = fn;
}

/**
* Form control validation based on picker validator config.
* */
validate(): ValidationErrors | null {
return this.validator(null);
}

protected parseNativeDateString(value: string): string {
const date = this.dateService.today();
const year = this.dateService.getYear(date);
Expand All @@ -473,4 +500,26 @@ export class NbTimePickerDirective<D> implements AfterViewInit, ControlValueAcce

return `${year}-${month}-${day} ${value}`;
}

/**
* Validates passed value is greater than min.
* */
protected minValidator(): ValidationErrors | null {
const config = this.timepicker.getValidatorConfig();
const date = this.dateService.parse(this.inputValue, this.timepicker.computedTimeFormat);
return !config.min || !date || this.dateService.compareDates(config.min, date) <= 0
? null
: { nbDatepickerMin: { min: config.min, actual: date } };
}

/**
* Validates passed value is smaller than max.
* */
protected maxValidator(): ValidationErrors | null {
const config = this.timepicker.getValidatorConfig();
const date = this.dateService.parse(this.inputValue, this.timepicker.computedTimeFormat);
return !config.max || !date || this.dateService.compareDates(config.max, date) >= 0
? null
: { nbDatepickerMax: { max: config.max, actual: date } };
}
}