UNPKG

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