Skip to content

Commit

Permalink
Merge pull request #949 from harshith-venkatesh-freshworks/fix/date-p…
Browse files Browse the repository at this point in the history
…icker

fix: Datepicker emitEvent error handling on parsing of icelandic language
  • Loading branch information
rihansiddhi authored Nov 14, 2024
2 parents be3b0db + 81e488b commit 2f40451
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 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 All @@ -21,7 +21,7 @@ import {
startOfDay,
getDaysInMonth,
format,
isMatch,
isMatch as parseIsMatch,
formatISO,
addDays,
startOfWeek,
Expand Down Expand Up @@ -81,6 +81,52 @@ const getWeekDays = (lang): any => {
format(addDays(startOfWeek(new Date()), i), 'EEEEE', { locale: lang })
);
};

const parseIcelandicDate = (value, langModule) => {
// For Icelandic language, the date format is different. There is a discrepency which is handled in this PR https://github.com/date-fns/date-fns/pull/3934
if (!value) return value;
const icelandicLanguageDisplayFormat = 'dd MMMM yyyy';
const icelandicMonthMapper = {
'jan.': 'jan.',
'feb.': 'feb.',
'mars': 'm',
'apríl': 'apríl',
'maí': 'maí',
'júní': 'júní',
'júlí': 'júlí',
'ágúst': 'á',
'sept.': 's',
'okt.': 'ó',
'nóv.': 'n',
'des.': 'd',
};
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 parseDate(
correctedDate,
icelandicLanguageDisplayFormat,
new Date(),
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 parseDate(value, displayFormat, date, langModule);
};

const isMatch = (value, displayFormat, langModule) => {
if (langModule?.locale?.code !== 'is') {
return parseIsMatch(value, displayFormat, langModule);
}
return true;
};

@Component({ tag: 'fw-datepicker', styleUrl: 'datepicker.scss', shadow: true })
export class Datepicker {
@State() showDatePicker: boolean;
Expand Down Expand Up @@ -336,33 +382,6 @@ export class Datepicker {

private formatDate(value) {
if (!value) return value;
// For Icelandic language, the date format is different. There is a discrepency which is handled in this PR https://github.com/date-fns/date-fns/pull/3934
if (this.langModule?.code === 'is' && this.dateFormat === 'dd MMM yyyy') {
const icelandicLanguageDisplayFormat = 'dd MMMM yyyy';
const icelandicMonthMapper = {
'jan.': 'jan.',
'feb.': 'feb.',
'mars': 'm',
'apríl': 'apríl',
'maí': 'maí',
'júní': 'júní',
'júlí': 'júlí',
'ágúst': 'á',
'sept.': 's',
'okt.': 'ó',
'nóv.': 'n',
'des.': 'd',
};
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 formatISO(
parse(correctedDate, icelandicLanguageDisplayFormat, new Date(), {
locale: this.langModule,
})
);
}

return this.displayFormat
? formatISO(
Expand Down

0 comments on commit 2f40451

Please sign in to comment.