UNPKG

9.77 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 * export class PageModule { }
105 * ```
106 * ### Usage
107 *
108 * If you want to use range selection, you have to use `NbRangepickerComponent` instead:
109 *
110 * ```html
111 * <input [nbDatepicker]="rangepicker">
112 * <nb-rangepicker #rangepicker></nb-rangepicker>
113 * ```
114 *
115 * Both range and date pickers support all parameters as calendar, so, check `NbCalendarComponent` for additional
116 * info.
117 *
118 * @stacked-example(Range showcase, datepicker/rangepicker-showcase.component)
119 *
120 * Datepicker is the form control so it can be bound with angular forms through ngModel and form controls.
121 *
122 * @stacked-example(Forms, datepicker/datepicker-forms.component)
123 *
124 * `NbDatepickerDirective` may be validated using `min` and `max` dates passed to the datepicker.
125 * And `filter` predicate that receives date object and has to return a boolean value.
126 *
127 * @stacked-example(Validation, datepicker/datepicker-validation.component)
128 *
129 * The `NbDatepickerComponent` supports date formatting:
130 *
131 * ```html
132 * <input [nbDatepicker]="datepicker">
133 * <nb-datepicker #datepicker format="MM\dd\yyyy"></nb-datepicker>
134 * ```
135 *
136 * ## Formatting Issue
137 *
138 * By default, datepicker uses angulars `LOCALE_ID` token for localization and `DatePipe` for dates formatting.
139 * And native `Date.parse(...)` for dates parsing. But native `Date.parse` function doesn't support formats.
140 * To provide custom formatting you have to use one of the following packages:
141 *
142 * - `@nebular/moment` - provides moment date adapter that uses moment for date objects. This means datepicker than
143 * will operate only moment date objects. If you want to use it you have to install it: `npm i @nebular/moment`, and
144 * import `NbMomentDateModule` from this package.
145 *
146 * - `@nebular/date-fns` - adapter for popular date-fns library. This way is preferred if you need only date formatting.
147 * Because date-fns is treeshakable, tiny and operates native date objects. If you want to use it you have to
148 * install it: `npm i @nebular/date-fns`, and import `NbDateFnsDateModule` from this package.
149 *
150 * ### NbDateFnsDateModule
151 *
152 * Format is required when using `NbDateFnsDateModule`. You can set it via `format` input on datepicker component:
153 * ```html
154 * <nb-datepicker format="dd.MM.yyyy"></nb-datepicker>
155 * ```
156 * Also format can be set globally with `NbDateFnsDateModule.forRoot({ format: 'dd.MM.yyyy' })` and
157 * `NbDateFnsDateModule.forChild({ format: 'dd.MM.yyyy' })` methods.
158 *
159 * Please note to use some of the formatting tokens you also need to pass
160 * `{ useAdditionalWeekYearTokens: true, useAdditionalDayOfYearTokens: true }` to date-fns parse and format functions.
161 * You can configure options passed this functions by setting `formatOptions` and
162 * `parseOptions` of options object passed to `NbDateFnsDateModule.forRoot` and `NbDateFnsDateModule.forChild` methods.
163 * ```ts
164 * NbDateFnsDateModule.forRoot({
165 * parseOptions: { useAdditionalWeekYearTokens: true, useAdditionalDayOfYearTokens: true },
166 * formatOptions: { useAdditionalWeekYearTokens: true, useAdditionalDayOfYearTokens: true },
167 * })
168 * ```
169 * Further info on `date-fns` formatting tokens could be found at
170 * [date-fns docs](https://date-fns.org/v2.0.0-alpha.27/docs/Unicode-Tokens).
171 *
172 * You can also use `parseOptions` and `formatOptions` to provide locale.
173 * ```ts
174 * import { eo } from 'date-fns/locale';
175 *
176 * @NgModule({
177 * imports: [
178 * NbDateFnsDateModule.forRoot({
179 * parseOptions: { locale: eo },
180 * formatOptions: { locale: eo },
181 * }),
182 * ],
183 * })
184 * ```
185 *
186 * @styles
187 *
188 * datepicker-background-color:
189 * datepicker-border-color:
190 * datepicker-border-style:
191 * datepicker-border-width:
192 * datepicker-border-radius:
193 * datepicker-shadow:
194 * */
195export declare class NbDatepickerDirective<D> implements OnDestroy, ControlValueAccessor, Validator {
196 protected document: any;
197 protected datepickerAdapters: NbDatepickerAdapter<D>[];
198 protected hostRef: ElementRef;
199 protected dateService: NbDateService<D>;
200 protected changeDetector: ChangeDetectorRef;
201 /**
202 * Provides datepicker component.
203 * */
204 set setPicker(picker: NbDatepicker<D>);
205 /**
206 * Datepicker adapter.
207 * */
208 protected datepickerAdapter: NbDatepickerAdapter<D>;
209 /**
210 * Datepicker instance.
211 * */
212 protected picker: NbDatepicker<D>;
213 protected destroy$: Subject<void>;
214 protected isDatepickerReady: boolean;
215 protected queue: D | undefined;
216 protected onChange: (D: any) => void;
217 protected onTouched: () => void;
218 /**
219 * Form control validators will be called in validators context, so, we need to bind them.
220 * */
221 protected validator: ValidatorFn;
222 constructor(document: any, datepickerAdapters: NbDatepickerAdapter<D>[], hostRef: ElementRef, dateService: NbDateService<D>, changeDetector: ChangeDetectorRef);
223 /**
224 * Returns html input element.
225 * */
226 get input(): HTMLInputElement;
227 /**
228 * Returns host input value.
229 * */
230 get inputValue(): string;
231 ngOnDestroy(): void;
232 /**
233 * Writes value in picker and html input element.
234 * */
235 writeValue(value: D): void;
236 registerOnChange(fn: any): void;
237 registerOnTouched(fn: any): void;
238 setDisabledState(isDisabled: boolean): void;
239 /**
240 * Form control validation based on picker validator config.
241 * */
242 validate(): ValidationErrors | null;
243 /**
244 * Hides picker, focuses the input
245 */
246 protected hidePicker(): void;
247 /**
248 * Validates that we can parse value correctly.
249 * */
250 protected parseValidator(): ValidationErrors | null;
251 /**
252 * Validates passed value is greater than min.
253 * */
254 protected minValidator(): ValidationErrors | null;
255 /**
256 * Validates passed value is smaller than max.
257 * */
258 protected maxValidator(): ValidationErrors | null;
259 /**
260 * Validates passed value satisfy the filter.
261 * */
262 protected filterValidator(): ValidationErrors | null;
263 /**
264 * Chooses datepicker adapter based on passed picker component.
265 * */
266 protected chooseDatepickerAdapter(): void;
267 /**
268 * Attaches picker to the host input element and subscribes on value changes.
269 * */
270 protected setupPicker(): void;
271 protected writePicker(value: D): void;
272 protected writeInput(value: D): void;
273 /**
274 * Validates if no datepicker adapter provided.
275 * */
276 protected noDatepickerAdapterProvided(): boolean;
277 protected subscribeOnInputChange(): void;
278 /**
279 * Parses input value and write if it isn't null.
280 * */
281 protected handleInputChange(value: string): void;
282 protected parseInputValue(value: any): D | null;
283}