Skip to content

Commit

Permalink
chore: refactor the code related to datepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
harshith-venkatesh-freshworks committed Nov 11, 2024
1 parent 7dc7dc7 commit e6a6f19
Showing 1 changed file with 27 additions and 52 deletions.
79 changes: 27 additions & 52 deletions packages/crayons-core/src/components/datepicker/datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ const getWeekDays = (lang): any => {
format(addDays(startOfWeek(new Date()), i), 'EEEEE', { locale: lang })
);
};

const parseIcelandicDate = (value, langModule) => {
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 parse(correctedDate, icelandicLanguageDisplayFormat, new Date(), {
locale: langModule,
});
};
@Component({ tag: 'fw-datepicker', styleUrl: 'datepicker.scss', shadow: true })
export class Datepicker {
@State() showDatePicker: boolean;
Expand Down Expand Up @@ -338,30 +363,7 @@ export class Datepicker {
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 formatISO(parseIcelandicDate(value, this.langModule));
}

return this.displayFormat
Expand Down Expand Up @@ -1001,34 +1003,7 @@ export class Datepicker {
// show error if not ISO format and not display format
let parsedDate;
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 = val.replace(
/jan\.|feb\.|mars|apríl|maí|júní|júlí|ágúst|sept\.|okt\.|nóv\.|des\./g,
(match) => icelandicMonthMapper[match]
);

parsedDate = parse(
correctedDate,
icelandicLanguageDisplayFormat,
new Date(),
{
locale: this.langModule,
}
);
parsedDate = parseIcelandicDate(val, this.langModule);
} else {
parsedDate = parse(val, this.displayFormat, new Date(), {
locale: this.langModule,
Expand Down

0 comments on commit e6a6f19

Please sign in to comment.