Sorry, but you either have no stories or none are selected somehow.
If the problem persists, check the browser console, or the terminal you've run Storybook from.
The component failed to render properly, likely due to a configuration issue in Storybook. Here are some common causes and how you can address them:
This is a pre-release version and is not production ready. For new and ongoing projects, please refer to the latest Design System version.
Display dates on a calendar view, allowing selection of a date or date range.
Make sure the @ng-bootstrap/ng-bootstrap
package is already present in your project
or follow ng-bootstrap installation guidelines.
To import all ng-bootstrap components:
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; @NgModule({ imports: [NgbModule], }) export class YourAppModule { }
To import only this component:
import {NgbDatepickerModule} from '@ng-bootstrap/ng-bootstrap'; @NgModule({ imports: [NgbDatepickerModule], }) export class YourAppModule { }
Make sure the @swisspost/design-system-styles
package is already present in your project or follow the installation guidelines.
To import all Design System styles:
@use '@swisspost/design-system-styles/post-compact.scss';
To import only the styles required for this component:
@use '@swisspost/design-system-styles/basics.scss'; @use '@swisspost/design-system-styles/components/datepicker.scss';
Some ng-bootstrap components contain static English text that does not appear on screen but is used in aria attributes needed for accessibility.
The internationalization of this content is done using the @angular/localize
package.
To do this, make sure to add import '@angular/localize/init';
to your polyfills.ts
file,
then use the implementation below to enable translations:
import { loadTranslations } from '@angular/localize'; const I18N_VALUES = { de: { 'ngb.alert.close': 'Schließen', 'ngb.datepicker.select-year': 'Jahr auswählen', 'ngb.datepicker.select-month': 'Monat auswählen', 'ngb.datepicker.previous-month': 'Vorheriger Monat', 'ngb.datepicker.next-month': 'Nächster Monat', 'ngb.pagination.first-aria': 'Erster', 'ngb.pagination.previous-aria': 'Vorheriger', 'ngb.pagination.next-aria': 'Nächster', 'ngb.pagination.last-aria': 'Letzter', 'ngb.timepicker.hours': 'Stunden', 'ngb.timepicker.increment-hours': 'Stunden erhöhen', 'ngb.timepicker.decrement-hours': 'Stunden verringern', 'ngb.timepicker.minutes': 'Minuten', 'ngb.timepicker.increment-minutes': 'Minuten erhöhen', 'ngb.timepicker.decrement-minutes': 'Minuten verringern', 'ngb.timepicker.seconds': 'Sekunden', 'ngb.timepicker.increment-seconds': 'Sekunden erhöhen', 'ngb.timepicker.decrement-seconds': 'Sekunden verringern', }, fr: { 'ngb.alert.close': 'Fermer', 'ngb.datepicker.select-year': "Sélectionner l'année", 'ngb.datepicker.select-month': 'Sélectionner le mois', 'ngb.datepicker.previous-month': 'Mois précédent', 'ngb.datepicker.next-month': 'Mois suivant', 'ngb.pagination.first-aria': 'Premier', 'ngb.pagination.previous-aria': 'Précédent', 'ngb.pagination.next-aria': 'Suivant', 'ngb.pagination.last-aria': 'Dernier', 'ngb.timepicker.hours': 'Heures', 'ngb.timepicker.increment-hours': 'Augmenter les heures', 'ngb.timepicker.decrement-hours': 'Diminuer les heures', 'ngb.timepicker.minutes': 'Minutes', 'ngb.timepicker.increment-minutes': 'Augmenter les minutes', 'ngb.timepicker.decrement-minutes': 'Diminuer les minutes', 'ngb.timepicker.seconds': 'Secondes', 'ngb.timepicker.increment-seconds': 'Augmenter les secondes', 'ngb.timepicker.decrement-seconds': 'Diminuer les secondes', }, it: { 'ngb.alert.close': 'Chiudi', 'ngb.datepicker.select-year': "Seleziona l'anno", 'ngb.datepicker.select-month': 'Seleziona il mese', 'ngb.datepicker.previous-month': 'Mese precedente', 'ngb.datepicker.next-month': 'Mese successivo', 'ngb.pagination.first-aria': 'Primo', 'ngb.pagination.previous-aria': 'Precedente', 'ngb.pagination.next-aria': 'Successivo', 'ngb.pagination.last-aria': 'Ultimo', 'ngb.timepicker.hours': 'Ore', 'ngb.timepicker.increment-hours': 'Aumenta le ore', 'ngb.timepicker.decrement-hours': 'Diminuisci le ore', 'ngb.timepicker.minutes': 'Minuti', 'ngb.timepicker.increment-minutes': 'Aumenta i minuti', 'ngb.timepicker.decrement-minutes': 'Diminuisci i minuti', 'ngb.timepicker.seconds': 'Secondi', 'ngb.timepicker.increment-seconds': 'Aumenta i secondi', 'ngb.timepicker.decrement-seconds': 'Diminuisci i secondi', }, en: { 'ngb.alert.close': 'Close', 'ngb.datepicker.select-year': 'Select Year', 'ngb.datepicker.select-month': 'Select Month', 'ngb.datepicker.previous-month': 'Previous Month', 'ngb.datepicker.next-month': 'Next Month', 'ngb.pagination.first-aria': 'First', 'ngb.pagination.previous-aria': 'Previous', 'ngb.pagination.next-aria': 'Next', 'ngb.pagination.last-aria': 'Last', 'ngb.timepicker.hours': 'Hours', 'ngb.timepicker.increment-hours': 'Increment Hours', 'ngb.timepicker.decrement-hours': 'Decrement Hours', 'ngb.timepicker.minutes': 'Minutes', 'ngb.timepicker.increment-minutes': 'Increment Minutes', 'ngb.timepicker.decrement-minutes': 'Decrement Minutes', 'ngb.timepicker.seconds': 'Seconds', 'ngb.timepicker.increment-seconds': 'Increment Seconds', 'ngb.timepicker.decrement-seconds': 'Decrement Seconds', }, }; // use this function where your application's locale is initialized loadTranslations(I18N_VALUES[localeId]);
The datepicker requires additional translations for months and days of the week. These can be configured using the service below:
import { Inject, Injectable, LOCALE_ID } from '@angular/core'; import { formatDate } from '@angular/common'; import { NgbDatepickerI18n, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; const I18N_VALUES = { en: { weekdays: ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su'], months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], monthsFull: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ], }, fr: { weekdays: ['Lu', 'Ma', 'Me', 'Je', 'Ve', 'Sa', 'Di'], months: ['Jan', 'Fév', 'Mar', 'Avr', 'Mai', 'Juin', 'Juil', 'Aou', 'Sep', 'Oct', 'Nov', 'Déc'], monthsFull: [ 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre', ], }, it: { weekdays: ['Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab', 'Dom'], months: ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic'], monthsFull: [ 'Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre', ], }, de: { weekdays: ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'], months: ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'], monthsFull: [ 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember', ], }, }; @Injectable() export class SwissPostDatepickerI18n extends NgbDatepickerI18n { constructor(@Inject(LOCALE_ID) private locale: string) { super(); } getWeekdayLabel(weekday: number): string { return I18N_VALUES[this.locale].weekdays[weekday - 1]; } getMonthShortName(month: number): string { return I18N_VALUES[this.locale].months[month - 1]; } getMonthFullName(month: number): string { return I18N_VALUES[this.locale].monthsFull[month - 1]; } getDayAriaLabel(date: NgbDateStruct): string { const jsDate = new Date(date.year, date.month - 1, date.day); return formatDate(jsDate, 'fullDate', this.locale); } }
import { NgbDatepickerI18n } from '@ng-bootstrap/ng-bootstrap'; @NgModule({ providers: [{ provide: NgbDatepickerI18n, useClass: SwissPostDatepickerI18n }], }) export class YourAppModule {}
The datepicker uses the NgbDateStruct interface as a model and not the native Date object.
To format dates in another type, you can define a formatter.
The formatter below transforms the NgbDateStruct into the usual Swiss format string DD.MM.YYYY
.
import { Injectable } from '@angular/core'; import { NgbDateParserFormatter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap'; @Injectable() export class SwissPostDateParserFormatter extends NgbDateParserFormatter { private static formatValue(value: string): NgbDateStruct { const dateParts = value.trim().split('.'); if (dateParts.length === 1 && isNumber(dateParts[0])) { return { year: null, month: null, day: toInteger(dateParts[0]) }; } else if (dateParts.length === 2 && isNumber(dateParts[0]) && isNumber(dateParts[1])) { return { year: null, month: toInteger(dateParts[0]), day: toInteger(dateParts[1]) }; } else if ( dateParts.length === 3 && isNumber(dateParts[0]) && isNumber(dateParts[1]) && isNumber(dateParts[2]) ) { return { year: toInteger(dateParts[2]), month: toInteger(dateParts[1]), day: toInteger(dateParts[0]), }; } return null; } parse(value: string): NgbDateStruct { if (value) { return SwissPostDateParserFormatter.formatValue(value); } return null; } format(date: NgbDateStruct): string { if (date) { const day = isNumber(date.day) ? padNumber(date.day) : ''; const month = isNumber(date.month) ? padNumber(date.month) : ''; return `${day}.${month}.${date.year}`; } return ''; } } function toInteger(value: unknown): number { return parseInt(`${value}`, 10); }
import { NgbDateParserFormatter } from '@ng-bootstrap/ng-bootstrap'; @NgModule({ providers: [{ provide: NgbDateParserFormatter, useClass: SwissPostDateParserFormatter }], }) export class YourAppModule {}
<div class="form-control-wrapper form-floating"> <input #d="ngbDatepicker" class="form-control" id="datepicker" maxlength="10" ngbDatepicker placeholder=" " type="text" /> <label for="datepicker" class="form-label">Datepicker</label> <button class="ngb-dp-open" (click)="d.toggle()" aria-label="Open calendar" type="button"> <i class="pi pi-3203" aria-hidden="true"></i> </button> </div>
<div class="form-control-wrapper position-relative"> <input #d="ngbDatepicker" navigation="arrows" aria-label="Datepicker" class="form-control form-control-sm" maxlength="10" ngbDatepicker placeholder="yyyy-mm-dd" type="text" /> <button class="ngb-dp-open" (click)="d.toggle()" aria-label="Open calendar" type="button"> <i class="pi pi-3203" aria-hidden="true"></i> </button> </div>
<div class="form-control-wrapper position-relative"> <input #d="ngbDatepicker" aria-label="Datepicker" class="form-control is-invalid" maxlength="10" ngbDatepicker placeholder="yyyy-mm-dd" type="text" /> <button class="ngb-dp-open" (click)="d.toggle()" aria-label="Open calendar" type="button"> <i class="pi pi-3203" aria-hidden="true"></i> </button> <p class="invalid-feedback">Selected date is invalid.</p> </div>