Skip to content

fix(input-time-picker): only load valid dayjs locale files and fallback to base lang when region code is unsupported #7049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/components/input-time-picker/input-time-picker.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ describe("calcite-input-time-picker", () => {
renders(`<calcite-input-time-picker lang="en-us"></calcite-input-time-picker>`, { display: "inline-block" });
});

describe("renders with base lang when region code is unsupported", () => {
renders(`<calcite-input-time-picker lang="nl-nl"></calcite-input-time-picker>`, { display: "inline-block" });
});

describe("renders with pt-PT locale", () => {
renders(`<calcite-input-time-picker lang="pt-PT"></calcite-input-time-picker>`, { display: "inline-block" });
});

describe("renders with no locale", () => {
renders(`<calcite-input-time-picker lang="no"></calcite-input-time-picker>`, { display: "inline-block" });
});

describe("honors hidden attribute", () => {
hidden("calcite-input-time-picker");
});
Expand Down
41 changes: 15 additions & 26 deletions src/components/input-time-picker/input-time-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import localeData from "dayjs/esm/plugin/localeData";
import localizedFormat from "dayjs/esm/plugin/localizedFormat";
import preParsePostFormat from "dayjs/esm/plugin/preParsePostFormat";
import updateLocale from "dayjs/esm/plugin/updateLocale";
import { getSupportedLocale } from "../../utils/locale";

// some bundlers (e.g., Webpack) need dynamic import paths to be static
const supportedDayJsLocaleToLocaleConfigImport = new Map([
Expand Down Expand Up @@ -555,18 +556,24 @@ export class InputTimePicker
};

private async loadDateTimeLocaleData(): Promise<void> {
const normalizedLocale = this.getNormalizedLocale();
let supportedLocale = getSupportedLocale(this.effectiveLocale).toLowerCase();

if (normalizedLocale === "en" || normalizedLocale === "en-us") {
return;
if (supportedLocale === "no") {
supportedLocale = "nb";
}

if (supportedLocale === "pt-pt") {
supportedLocale = "pt";
}

const { default: localeConfig } = await supportedDayJsLocaleToLocaleConfigImport.get(
normalizedLocale
)();
if (supportedDayJsLocaleToLocaleConfigImport.has(supportedLocale)) {
const { default: localeConfig } = await supportedDayJsLocaleToLocaleConfigImport.get(
supportedLocale
)();

dayjs.locale(localeConfig, null, true);
dayjs.updateLocale(normalizedLocale, this.getExtendedLocaleConfig(normalizedLocale));
dayjs.locale(localeConfig, null, true);
dayjs.updateLocale(supportedLocale, this.getExtendedLocaleConfig(supportedLocale));
}
}

private getExtendedLocaleConfig(
Expand Down Expand Up @@ -644,22 +651,6 @@ export class InputTimePicker
}
}

private getNormalizedLocale(): string {
const { effectiveLocale } = this;
let normalizedLocale = effectiveLocale ? effectiveLocale.toLowerCase() : "en";

if (normalizedLocale === "en-us") {
normalizedLocale = "en";
}
if (normalizedLocale === "pt-pt") {
normalizedLocale = "pt";
}
if (normalizedLocale === "no") {
normalizedLocale = "nb";
}
return normalizedLocale;
}

onLabelClick(): void {
this.setFocus();
}
Expand Down Expand Up @@ -765,8 +756,6 @@ export class InputTimePicker
connectedCallback() {
connectLocalized(this);

this.effectiveLocale = this.getNormalizedLocale();

if (isValidTime(this.value)) {
this.setValueDirectly(this.value);
} else {
Expand Down