UNPKG

10.8 kBTypeScriptView Raw
1import { ChangeDetectorRef, ElementRef, InjectionToken, OnDestroy, Type } from '@angular/core';
2import { ControlValueAccessor, ValidationErrors, Validator, ValidatorFn } from '@angular/forms';
3import { Observable, Subject } from 'rxjs';
4import { NbDateService } from '../calendar-kit/services/date.service';
5import * as i0 from "@angular/core";
6/**
7 * The `NbDatepickerAdapter` instances provide way how to parse, format and validate
8 * different date types.
9 * */
10export declare abstract class NbDatepickerAdapter<D> {
11 /**
12 * Picker component class.
13 * */
14 abstract picker: Type<any>;
15 /**
16 * Parse date string according to the format.
17 * */
18 abstract parse(value: string, format: string): D;
19 /**
20 * Format date according to the format.
21 * */
22 abstract format(value: D, format: string): string;
23 /**
24 * Validates date string according to the passed format.
25 * */
26 abstract isValid(value: string, format: string): boolean;
27}
28/**
29 * Validators config that will be used by form control to perform proper validation.
30 * */
31export interface NbPickerValidatorConfig<D> {
32 /**
33 * Minimum date available in picker.
34 * */
35 min: D;
36 /**
37 * Maximum date available in picker.
38 * */
39 max: D;
40 /**
41 * Predicate that determines is value available for picking.
42 * */
43 filter: (D: any) => boolean;
44}
45/**
46 * Datepicker is an control that can pick any values anyway.
47 * It has to be bound to the datepicker directive through nbDatepicker input.
48 * */
49export declare abstract class NbDatepicker<T, D = T> {
50 /**
51 * HTML input element date format.
52 * */
53 abstract format: string;
54 abstract get value(): T;
55 abstract set value(value: T);
56 abstract get valueChange(): Observable<T>;
57 abstract get init(): Observable<void>;
58 /**
59 * Attaches datepicker to the native input element.
60 * */
61 abstract attach(hostRef: ElementRef): any;
62 /**
63 * Returns validator configuration based on the input properties.
64 * */
65 abstract getValidatorConfig(): NbPickerValidatorConfig<D>;
66 abstract show(): any;
67 abstract hide(): any;
68 abstract shouldHide(): boolean;
69 abstract get isShown(): boolean;
70 abstract get blur(): Observable<void>;
71}
72export declare const NB_DATE_ADAPTER: InjectionToken<NbDatepickerAdapter<any>>;
73export declare const NB_DATE_SERVICE_OPTIONS: InjectionToken<unknown>;
74/**
75 * The `NbDatepickerDirective` is form control that gives you ability to select dates and ranges. The datepicker
76 * is shown when input receives a `focus` event.
77 *
78 * ```html
79 * <input [nbDatepicker]="datepicker">
80 * <nb-datepicker #datepicker></nb-datepicker>
81 * ```
82 *
83 * @stacked-example(Showcase, datepicker/datepicker-showcase.component)
84 *
85 * ### Installation
86 *
87 * Import `NbDatepickerModule.forRoot()` to your root module.
88 * ```ts
89 * @NgModule({
90 * imports: [
91 * // ...
92 * NbDatepickerModule.forRoot(),
93 * ],
94 * })
95 * export class AppModule { }
96 * ```
97 * And `NbDatepickerModule` to your feature module.
98 * ```ts
99 * @NgModule({
100 * imports: [
101 * // ...
102 * NbDatepickerModule,
103 * ],
104 * })
105 *
106 * export class PageModule { }
107 * ```
108 * ### Usage
109 *
110 * If you want to use range selection, you have to use `NbRangepickerComponent` instead:
111 *
112 * ```html
113 * <input [nbDatepicker]="rangepicker">
114 * <nb-rangepicker #rangepicker></nb-rangepicker>
115 * ```
116 *
117 * Both range and date pickers support all parameters as calendar, so, check `NbCalendarComponent` for additional
118 * info.
119 *
120 * @stacked-example(Range showcase, datepicker/rangepicker-showcase.component)
121 *
122 * Datepicker is the form control so it can be bound with angular forms through ngModel and form controls.
123 *
124 * @stacked-example(Forms, datepicker/datepicker-forms.component)
125 *
126 * `NbDatepickerDirective` may be validated using `min` and `max` dates passed to the datepicker.
127 *
128 * @stacked-example(Validation, datepicker/datepicker-validation.component)
129 *
130 * Also `NbDatepickerDirective` may be filtered using `filter` predicate
131 * that receives date object and has to return a boolean value.
132 *
133 * @stacked-example(Filter, datepicker/datepicker-filter.component)
134 *
135 * If you need to pick a time along with the date, you can use nb-date-timepicker
136 *
137 * ```html
138 * <input nbInput placeholder="Pick Date" [nbDatepicker]="dateTimePicker">
139 * <nb-date-timepicker withSeconds #dateTimePicker></nb-date-timepicker>
140 * ```
141 * @stacked-example(Date timepicker, datepicker/date-timepicker-showcase.component)
142 *
143 * A single column picker with options value as time and minute, so users won’t be able to pick
144 * hours and minutes individually.
145 *
146 * @stacked-example(Date timepicker single column, datepicker/date-timepicker-single-column.component)
147
148 * The `NbDatepickerComponent` supports date formatting:
149 *
150 * ```html
151 * <input [nbDatepicker]="datepicker">
152 * <nb-datepicker #datepicker format="MM\dd\yyyy"></nb-datepicker>
153 * ```
154 * <span id="formatting-issue"></span>
155 * ## Formatting Issue
156 *
157 * By default, datepicker uses angulars `LOCALE_ID` token for localization and `DatePipe` for dates formatting.
158 * And native `Date.parse(...)` for dates parsing. But native `Date.parse` function doesn't support formats.
159 * To provide custom formatting you have to use one of the following packages:
160 *
161 * - `@nebular/moment` - provides moment date adapter that uses moment for date objects. This means datepicker than
162 * will operate only moment date objects. If you want to use it you have to install it: `npm i @nebular/moment`, and
163 * import `NbMomentDateModule` from this package.
164 *
165 * - `@nebular/date-fns` - adapter for popular date-fns library. This way is preferred if you need only date formatting.
166 * Because date-fns is treeshakable, tiny and operates native date objects. If you want to use it you have to
167 * install it: `npm i @nebular/date-fns`, and import `NbDateFnsDateModule` from this package.
168 *
169 * ### NbDateFnsDateModule
170 *
171 * Format is required when using `NbDateFnsDateModule`. You can set it via `format` input on datepicker component:
172 * ```html
173 * <nb-datepicker format="dd.MM.yyyy"></nb-datepicker>
174 * ```
175 * Also format can be set globally with `NbDateFnsDateModule.forRoot({ format: 'dd.MM.yyyy' })` and
176 * `NbDateFnsDateModule.forChild({ format: 'dd.MM.yyyy' })` methods.
177 *
178 * Please note to use some of the formatting tokens you also need to pass
179 * `{ useAdditionalWeekYearTokens: true, useAdditionalDayOfYearTokens: true }` to date-fns parse and format functions.
180 * You can configure options passed this functions by setting `formatOptions` and
181 * `parseOptions` of options object passed to `NbDateFnsDateModule.forRoot` and `NbDateFnsDateModule.forChild` methods.
182 * ```ts
183 * NbDateFnsDateModule.forRoot({
184 * parseOptions: { useAdditionalWeekYearTokens: true, useAdditionalDayOfYearTokens: true },
185 * formatOptions: { useAdditionalWeekYearTokens: true, useAdditionalDayOfYearTokens: true },
186 * })
187 * ```
188 * Further info on `date-fns` formatting tokens could be found at
189 * [date-fns docs](https://date-fns.org/v2.0.0-alpha.27/docs/Unicode-Tokens).
190 *
191 * You can also use `parseOptions` and `formatOptions` to provide locale.
192 * ```ts
193 * import { eo } from 'date-fns/locale';
194 *
195 * @NgModule({
196 * imports: [
197 * NbDateFnsDateModule.forRoot({
198 * parseOptions: { locale: eo },
199 * formatOptions: { locale: eo },
200 * }),
201 * ],
202 * })
203 * ```
204 *
205 * @styles
206 *
207 * datepicker-background-color:
208 * datepicker-border-color:
209 * datepicker-border-style:
210 * datepicker-border-width:
211 * datepicker-border-radius:
212 * datepicker-shadow:
213 * */
214export declare class NbDatepickerDirective<D> implements OnDestroy, ControlValueAccessor, Validator {
215 protected document: any;
216 protected datepickerAdapters: NbDatepickerAdapter<D>[];
217 protected hostRef: ElementRef;
218 protected dateService: NbDateService<D>;
219 protected changeDetector: ChangeDetectorRef;
220 /**
221 * Provides datepicker component.
222 * */
223 set setPicker(picker: NbDatepicker<D>);
224 /**
225 * Datepicker adapter.
226 * */
227 protected datepickerAdapter: NbDatepickerAdapter<D>;
228 /**
229 * Datepicker instance.
230 * */
231 protected picker: NbDatepicker<D>;
232 protected destroy$: Subject<void>;
233 protected isDatepickerReady: boolean;
234 protected queue: D | undefined;
235 protected onChange: (D: any) => void;
236 protected onTouched: () => void;
237 /**
238 * Form control validators will be called in validators context, so, we need to bind them.
239 * */
240 protected validator: ValidatorFn;
241 constructor(document: any, datepickerAdapters: NbDatepickerAdapter<D>[], hostRef: ElementRef, dateService: NbDateService<D>, changeDetector: ChangeDetectorRef);
242 /**
243 * Returns html input element.
244 * */
245 get input(): HTMLInputElement;
246 /**
247 * Returns host input value.
248 * */
249 get inputValue(): string;
250 ngOnDestroy(): void;
251 /**
252 * Writes value in picker and html input element.
253 * */
254 writeValue(value: D): void;
255 registerOnChange(fn: any): void;
256 registerOnTouched(fn: any): void;
257 setDisabledState(isDisabled: boolean): void;
258 /**
259 * Form control validation based on picker validator config.
260 * */
261 validate(): ValidationErrors | null;
262 /**
263 * Hides picker, focuses the input
264 */
265 protected hidePicker(): void;
266 /**
267 * Validates that we can parse value correctly.
268 * */
269 protected parseValidator(): ValidationErrors | null;
270 /**
271 * Validates passed value is greater than min.
272 * */
273 protected minValidator(): ValidationErrors | null;
274 /**
275 * Validates passed value is smaller than max.
276 * */
277 protected maxValidator(): ValidationErrors | null;
278 /**
279 * Validates passed value satisfy the filter.
280 * */
281 protected filterValidator(): ValidationErrors | null;
282 /**
283 * Chooses datepicker adapter based on passed picker component.
284 * */
285 protected chooseDatepickerAdapter(): void;
286 /**
287 * Attaches picker to the host input element and subscribes on value changes.
288 * */
289 protected setupPicker(): void;
290 protected writePicker(value: D): void;
291 protected writeInput(value: D): void;
292 /**
293 * Validates if no datepicker adapter provided.
294 * */
295 protected noDatepickerAdapterProvided(): boolean;
296 protected subscribeOnInputChange(): void;
297 /**
298 * Parses input value and write if it isn't null.
299 * */
300 protected handleInputChange(value: string): void;
301 protected parseInputValue(value: any): D | null;
302 static ɵfac: i0.ɵɵFactoryDeclaration<NbDatepickerDirective<any>, never>;
303 static ɵdir: i0.ɵɵDirectiveDeclaration<NbDatepickerDirective<any>, "input[nbDatepicker]", never, { "setPicker": "nbDatepicker"; }, {}, never>;
304}