Skip to content

Commit 6ddf745

Browse files
committed
fix: Add property check before updating shorthand for english locale
Signed-off-by: Akshat Patel <akshat@live.ca>
1 parent 229f87d commit 6ddf745

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/datepicker/datepicker.component.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,33 @@ import {
1313
AfterViewChecked,
1414
AfterViewInit,
1515
ViewChild,
16-
AfterContentInit,
1716
OnInit,
1817
SimpleChange
1918
} from "@angular/core";
2019
import rangePlugin from "flatpickr/dist/plugins/rangePlugin";
2120
import flatpickr from "flatpickr";
2221
import { NG_VALUE_ACCESSOR } from "@angular/forms";
2322
import { carbonFlatpickrMonthSelectPlugin } from "./carbon-flatpickr-month-select";
24-
import languages from "flatpickr/dist/l10n/index";
23+
import * as languages from "flatpickr/dist/l10n/index";
2524
import { DatePickerInput } from "carbon-components-angular/datepicker-input";
2625
import { I18n } from "carbon-components-angular/i18n";
2726

28-
// Due to type error, we have to use square brackets property accessor
29-
(languages.default["en"].weekdays.shorthand as string[])
30-
= languages.default["en"].weekdays.longhand.map(day => {
31-
if (day === "Thursday") {
32-
return "Th";
33-
}
34-
return day.charAt(0);
35-
});
27+
/**
28+
* Due to type error, we have to use square brackets property accessor
29+
* There is a webpack issue when attempting to access exported languages from flatpickr l10n Angular 14+ apps
30+
* languages.default[locale] fails in app consuming CCA library but passes in test
31+
* languages.default.default[locale] fails in test but works in app consuming CCA library.
32+
*
33+
* To please both scenarios, we are adding a condition to prevent tests from failing
34+
*/
35+
if (languages.default?.default["en"]?.weekdays) {
36+
(languages.default.default["en"].weekdays.shorthand as string[]) = languages.default.default["en"].weekdays.longhand.map(day => {
37+
if (day === "Thursday") {
38+
return "Th";
39+
}
40+
return day.charAt(0);
41+
});
42+
}
3643

3744
/**
3845
* [See demo](../../?path=/story/components-date-picker--single)
@@ -221,7 +228,7 @@ export class DatePicker implements
221228
mode: this.range ? "range" : "single",
222229
plugins,
223230
dateFormat: this.dateFormat,
224-
locale: languages.default[this.language]
231+
locale: languages.default?.default[this.language] || languages.default[this.language]
225232
});
226233
}
227234

0 commit comments

Comments
 (0)