Skip to content

Commit

Permalink
Merge pull request #2681 from Akshat55/webpack-flatpickr-l10n
Browse files Browse the repository at this point in the history
fix: access correct object property to resolve webpack import error
  • Loading branch information
Akshat55 authored Aug 24, 2023
2 parents 268fbe1 + 6ddf745 commit 565b83b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/datepicker/datepicker.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
AfterViewChecked,
AfterViewInit,
ViewChild,
AfterContentInit,
OnInit,
SimpleChange
} from "@angular/core";
Expand All @@ -23,9 +22,25 @@ import { NG_VALUE_ACCESSOR } from "@angular/forms";
import { carbonFlatpickrMonthSelectPlugin } from "./carbon-flatpickr-month-select";
import * as languages from "flatpickr/dist/l10n/index";
import { DatePickerInput } from "carbon-components-angular/datepicker-input";
import { ElementService } from "carbon-components-angular/utils";
import { I18n } from "carbon-components-angular/i18n";

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

/**
* [See demo](../../?path=/story/components-date-picker--single)
*/
Expand Down Expand Up @@ -101,8 +116,7 @@ export class DatePicker implements
OnDestroy,
OnChanges,
AfterViewChecked,
AfterViewInit,
AfterContentInit {
AfterViewInit {
private static datePickerCount = 0;

/**
Expand Down Expand Up @@ -214,7 +228,7 @@ export class DatePicker implements
mode: this.range ? "range" : "single",
plugins,
dateFormat: this.dateFormat,
locale: languages.default[this.language]
locale: languages.default?.default[this.language] || languages.default[this.language]
});
}

Expand Down Expand Up @@ -330,16 +344,6 @@ export class DatePicker implements
}
}

ngAfterContentInit() {
(languages.default.en.weekdays.shorthand as string[])
= languages.default.en.weekdays.longhand.map(day => {
if (day === "Thursday") {
return "Th";
}
return day.charAt(0);
});
}

@HostListener("focusin")
onFocus() {
// Updates the month manually when calendar mode is range because month
Expand Down
3 changes: 2 additions & 1 deletion src/datepicker/datepicker.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default {
invalid: false,
warnText: "This is a warning",
warn: false,
disabled: false
disabled: false,
language: "en"
},
argTypes: {
theme: {
Expand Down

0 comments on commit 565b83b

Please sign in to comment.