Skip to content

Commit

Permalink
fix: generalising the datepicker for icelandic language
Browse files Browse the repository at this point in the history
  • Loading branch information
harshith-venkatesh-freshworks committed Nov 13, 2024
1 parent f422876 commit d00f87b
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/crayons-core/src/components/datepicker/datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@stencil/core';
import {
isValid,
parse,
parse as parseDate,
parseISO,
getYear,
getMonth,
Expand Down Expand Up @@ -83,6 +83,7 @@ const getWeekDays = (lang): any => {
};

const parseIcelandicDate = (value, langModule) => {
if (!value) return value;
const icelandicLanguageDisplayFormat = 'dd MMMM yyyy';
const icelandicMonthMapper = {
'jan.': 'jan.',
Expand All @@ -98,23 +99,24 @@ const parseIcelandicDate = (value, langModule) => {
'nóv.': 'n',
'des.': 'd',
};
const correctedDate = value.replace(
const correctedDate = value?.replace(
/jan\.|feb\.|mars|apríl|maí|júní|júlí|ágúst|sept\.|okt\.|nóv\.|des\./g,
(match) => icelandicMonthMapper[match]
);
return parse(
return parseDate(
correctedDate,
icelandicLanguageDisplayFormat,
new Date(),
langModule
);
};

const parseDate = (value, displayFormat, langModule) => {
const parse = (value, displayFormat, date, langModule) => {
if (!value) return value;
if (langModule?.locale?.code === 'is' && displayFormat === 'dd MMM yyyy') {
return parseIcelandicDate(value, langModule);
}
return parse(value, displayFormat, new Date(), langModule);
return parseDate(value, displayFormat, date, langModule);
};

@Component({ tag: 'fw-datepicker', styleUrl: 'datepicker.scss', shadow: true })
Expand Down Expand Up @@ -375,7 +377,7 @@ export class Datepicker {

return this.displayFormat
? formatISO(
parseDate(value, this.displayFormat, {
parse(value, this.displayFormat, new Date(), {
locale: this.langModule,
})
)
Expand Down Expand Up @@ -903,11 +905,11 @@ export class Datepicker {
fromDate = fromDate?.trim();
toDate = toDate?.trim();

const parsedFromDate = parseDate(fromDate, this.displayFormat, {
const parsedFromDate = parse(fromDate, this.displayFormat, new Date(), {
locale: this.langModule,
});

const parsedToDate = parseDate(toDate, this.displayFormat, {
const parsedToDate = parse(toDate, this.displayFormat, new Date(), {
locale: this.langModule,
});

Expand Down Expand Up @@ -1010,12 +1012,12 @@ export class Datepicker {

processValueChange(val, emitChange = false) {
// show error if not ISO format and not display format
const parsedDate = parseDate(val, this.displayFormat, {
const parsedDate = parse(val, this.displayFormat, new Date(), {
locale: this.langModule,
});

const year = getYear(
parseDate(val, this.displayFormat, {
parse(val, this.displayFormat, new Date(), {
locale: this.langModule,
})
);
Expand Down

0 comments on commit d00f87b

Please sign in to comment.