Skip to content

Commit

Permalink
fix(datepicker): incorrect month view in calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
binodnepali committed Jul 16, 2023
1 parent 0e25c6d commit 70ccbaa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function App() {
</p>
</div>

<NepaliDatePicker onDateSelect={setSelectedDate} lang='en' />
<NepaliDatePicker onDateSelect={setSelectedDate} />
</div>
</main>
</>
Expand Down
4 changes: 3 additions & 1 deletion src/components/NepaliCalendar/NepaliCalendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export const NepaliCalendar = ({
return;
}

const foundMonth = months.find((m) => m.value === selectedDate.month.value);
const foundMonth = months.find(
(m) => m.value === selectedDate.month.value - 1
);
if (!foundMonth) {
console.warn('Month not found');
return;
Expand Down
37 changes: 34 additions & 3 deletions src/utils/nepaliDate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { yearsWithDaysInMonth } from '@/constants/yearsWithDaysInMonth';
import { years } from './year';
import { months } from '@/constants/months';
import { weekDays } from '@/constants/weekDays';
import { Language } from '@/types/Language';
Expand All @@ -13,6 +12,7 @@ import { Day, Month, NepaliDate, Year } from '@/types/NepaliDate';

const GREGORIAN_START_YEAR = 1943; // Start year of the Gregorian calendar
const NEPALI_START_YEAR = 2000; // Start year of the Nepali calendar
const NEPALI_END_YEAR = 2089; // End year of the Nepali calendar
const NEPALI_YEAR_OFFSET = NEPALI_START_YEAR - GREGORIAN_START_YEAR; // Convert Gregorian year to Nepali year
const NEPALI_MONTH_OFFSET = 8; // Add an offset of 8 to convert Gregorian month to Nepali month
const NEPALI_DATE_OFFSET = 15; // Add an offset of 15 to convert Gregorian date to Nepali date
Expand Down Expand Up @@ -78,22 +78,53 @@ export const currentNepaliDate = () => {
};
};

const years = generateYears(NEPALI_START_YEAR, NEPALI_END_YEAR);
export const getYears = (lang: Language): Year[] => {
return years.map((year) => {
if (lang === 'ne') {
return {
value: year.value,
label: year.ne,
label: year.label.ne,
};
}

return {
value: year.value,
label: year.en,
label: year.label.en,
};
});
};

function generateYears(startYear: number, endYear: number) {
const years: {
label: {
ne: string;
en: string;
};
value: number;
daysInMonth: [number, number, number][];
}[] = [];

const days = yearsWithDaysInMonth as unknown as {
[year: number]: {
daysInMonth: [number, number, number][];
};
};

for (let year = startYear; year <= endYear; year++) {
years.push({
label: {
ne: convertToNepaliDigit(year),
en: year.toString(),
},
value: year,
daysInMonth: days[year].daysInMonth,
});
}

return years;
}

export const getMonths = (lang: Language): Month[] => {
return months.map((month) => {
if (lang === 'ne') {
Expand Down
37 changes: 0 additions & 37 deletions src/utils/year.ts

This file was deleted.

0 comments on commit 70ccbaa

Please sign in to comment.