-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(date-selector): Refactor DateSelector component
- Loading branch information
1 parent
ec7f381
commit ef5a559
Showing
16 changed files
with
474 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/lib/components/dateSelector/components/Calendar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { useRef } from 'react'; | ||
import dayjs from 'dayjs'; | ||
import DayPicker from 'react-day-picker'; | ||
|
||
import { useOnClickOutside } from '../../../hooks/useOnClickOutside'; | ||
import { CalendarIcon } from '../../icon/icons'; | ||
|
||
import styles from './style.module.scss'; | ||
import './datepicker.scss'; | ||
import { Button } from '../../button'; | ||
|
||
export interface CalendarProps { | ||
dateFormat: string; | ||
value?: string; | ||
onChange: (date: string) => void; | ||
yearBoundaries: { min: number; max: number }; | ||
displayCalendar?: boolean; | ||
dayjsLocale?: ILocale; | ||
firstDayOfWeek?: number; | ||
isOpen?: boolean; | ||
setCalendarOpen: (isOpen: boolean) => void; | ||
} | ||
|
||
export const Calendar = ({ | ||
dateFormat, | ||
value, | ||
onChange, | ||
yearBoundaries, | ||
displayCalendar, | ||
dayjsLocale, | ||
firstDayOfWeek, | ||
setCalendarOpen, | ||
isOpen | ||
}: CalendarProps) => { | ||
const localeDate = dayjsLocale | ||
? dayjs().locale(dayjsLocale).localeData() | ||
: dayjs().locale('en').localeData(); | ||
|
||
const localizedWeekdays = localeDate.weekdays(); | ||
const localizedWeekdaysShort = localeDate.weekdaysShort(); | ||
const localizedMonths = localeDate.months(); | ||
|
||
const calendarContainerRef = useRef<HTMLDivElement | null>(null); | ||
|
||
const calendarDefaultDate = | ||
dayjs().year() >= yearBoundaries.min && dayjs().year() <= yearBoundaries.max | ||
? dayjs().toDate() | ||
: dayjs().set('year', yearBoundaries.max).toDate(); | ||
|
||
const selectedDateInDateType = value | ||
? dayjs(value).toDate() | ||
: calendarDefaultDate; | ||
const dateCalendarFromMonth = dayjs(String(yearBoundaries.min)) | ||
.startOf('year') | ||
.toDate(); | ||
const dateCalendarToMonth = dayjs(String(yearBoundaries.max)) | ||
.endOf('year') | ||
.toDate(); | ||
|
||
useOnClickOutside(calendarContainerRef, () => setCalendarOpen(false)); | ||
|
||
if (!displayCalendar) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div | ||
className={`${styles.container} ml8`} | ||
ref={calendarContainerRef} | ||
> | ||
<Button | ||
onClick={() => setCalendarOpen(!isOpen)} | ||
data-testid="calendar-button" | ||
hideLabel | ||
variant='textColor' | ||
leftIcon={<CalendarIcon />} | ||
> | ||
Select date | ||
</Button> | ||
|
||
{isOpen && ( | ||
<DayPicker | ||
month={selectedDateInDateType} | ||
showOutsideDays={true} | ||
fromMonth={dateCalendarFromMonth} | ||
toMonth={dateCalendarToMonth} | ||
selectedDays={selectedDateInDateType} | ||
onDayClick={(date: Date) => { | ||
if ( | ||
dayjs(date).isAfter(dateCalendarFromMonth) || | ||
dayjs(date).isBefore(dateCalendarToMonth) | ||
) { | ||
const selectedDate = dayjs(date).format(dateFormat); | ||
onChange(selectedDate); | ||
setCalendarOpen(false); | ||
} | ||
}} | ||
pagedNavigation={true} | ||
disabledDays={{ | ||
before: dateCalendarFromMonth, | ||
after: dateCalendarToMonth, | ||
}} | ||
firstDayOfWeek={firstDayOfWeek} | ||
locale={dayjsLocale?.name || 'en'} | ||
months={localizedMonths} | ||
weekdaysLong={localizedWeekdays} | ||
weekdaysShort={localizedWeekdaysShort} | ||
/> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.container { | ||
position: relative; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.