Date Range Calendar [<span class="plan-pro"></span>](/x/introduction/licensing/#pro-plan 'Pro plan')
Date Range Calendar
Date Range Calendar는 입력이나 popper/modal 없이도 사용자가 날짜 범위를 선택할 수 있게 해줍니다.
본문
기본 사용법
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { LocalizationProvider } from '@mui/x-date-pickers-pro/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';
export default function BasicDateRangeCalendar() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar']}>
<DateRangeCalendar />
</DemoContainer>
</LocalizationProvider>
);
}
비제어(uncontrolled) vs. 제어(controlled) 값
컴포넌트의 값은 비제어 또는 제어로 설정할 수 있습니다.
import * as React from 'react';
import dayjs, { Dayjs } from 'dayjs';
import { DemoContainer, DemoItem } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';
import { DateRange } from '@mui/x-date-pickers-pro/models';
export default function DateRangeCalendarValue() {
const [value, setValue] = React.useState<DateRange<Dayjs>>([
dayjs('2022-04-17'),
dayjs('2022-04-21'),
]);
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar', 'DateRangeCalendar']}>
<DemoItem label="Uncontrolled calendar">
<DateRangeCalendar
defaultValue={[dayjs('2022-04-17'), dayjs('2022-04-21')]}
/>
</DemoItem>
<DemoItem label="Controlled calendar">
<DateRangeCalendar
value={value}
onChange={(newValue) => setValue(newValue)}
/>
</DemoItem>
</DemoContainer>
</LocalizationProvider>
);
}
:::info
- 부모가
valueprop을 제공해 값을 관리하면 값은 **제어(controlled)**됩니다. - 컴포넌트 자체의 내부 상태로 관리되면 값은 **비제어(uncontrolled)**됩니다. 이 상태는
defaultValueprop으로 초기화할 수 있습니다.
Controlled and uncontrolled 패턴에 대해 더 알아보려면 React 문서를 참고하세요. :::
폼 props
컴포넌트를 비활성화하거나 읽기 전용으로 만들 수 있습니다.
import dayjs from 'dayjs';
import { DemoContainer, DemoItem } from '@mui/x-date-pickers/internals/demo';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';
export default function DateRangeCalendarFormProps() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar', 'DateRangeCalendar']}>
<DemoItem label="disabled">
<DateRangeCalendar
defaultValue={[dayjs('2022-04-17'), dayjs('2022-04-21')]}
disabled
/>
</DemoItem>
<DemoItem label="readOnly">
<DateRangeCalendar
defaultValue={[dayjs('2022-04-17'), dayjs('2022-04-21')]}
readOnly
/>
</DemoItem>
</DemoContainer>
</LocalizationProvider>
);
}
커스터마이즈
렌더링할 월 선택하기
calendars prop을 사용해 동시에 최대 3개월까지 렌더링할 수 있습니다:
import { DemoContainer, DemoItem } from '@mui/x-date-pickers/internals/demo';
import { LocalizationProvider } from '@mui/x-date-pickers-pro/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';
export default function DateRangeCalendarCalendarsProp() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar', 'DateRangeCalendar']}>
<DemoItem label="1 calendar">
<DateRangeCalendar calendars={1} />
</DemoItem>
<DemoItem label="2 calendars">
<DateRangeCalendar calendars={2} />
</DemoItem>
</DemoContainer>
</LocalizationProvider>
);
}
currentMonthCalendarPosition prop을 사용해 현재 월이 렌더링되는 위치를 선택할 수 있습니다.
disableFuture를 사용해 현재 월과 다음 달 대신 현재 월과 이전 달을 렌더링할 때 유용합니다.
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import { LocalizationProvider } from '@mui/x-date-pickers-pro/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';
export default function DateRangeCalendarCurrentMonthCalendarPositionProp() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar']}>
<DateRangeCalendar currentMonthCalendarPosition={2} disableFuture />
</DemoContainer>
</LocalizationProvider>
);
}
커스텀 요일 렌더링
표시되는 요일은 Day 컴포넌트 슬롯으로 커스터마이즈할 수 있습니다.
DateRangePickerDay 컴포넌트를 활용할 수 있습니다.
import * as React from 'react';
import { DemoContainer } from '@mui/x-date-pickers/internals/demo';
import dayjs from 'dayjs';
import { styled } from '@mui/material/styles';
import { LocalizationProvider } from '@mui/x-date-pickers-pro/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers-pro/AdapterDayjs';
import { DateRangeCalendar } from '@mui/x-date-pickers-pro/DateRangeCalendar';
import {
DateRangePickerDay as MuiDateRangePickerDay,
DateRangePickerDayProps,
} from '@mui/x-date-pickers-pro/DateRangePickerDay';
const DateRangePickerDay = styled(MuiDateRangePickerDay)(({ theme }) => ({
'--PickerDay-horizontalMargin': 0,
// Ensures the day increases width, accounting for the padding, matching the width of the week number cell
boxSizing: 'content-box',
paddingLeft: '2px',
paddingRight: '2px',
variants: [
{
props: ({ isHighlighting, outsideCurrentMonth }) =>
!outsideCurrentMonth && isHighlighting,
style: {
borderRadius: 0,
backgroundColor: theme.palette.primary.main,
color: theme.palette.common.white,
'&:hover, &:focus': {
backgroundColor: theme.palette.primary.dark,
},
},
},
{
props: ({ isStartOfHighlighting }) => isStartOfHighlighting,
style: {
borderTopLeftRadius: '50%',
borderBottomLeftRadius: '50%',
},
},
{
props: ({ isEndOfHighlighting }) => isEndOfHighlighting,
style: {
borderTopRightRadius: '50%',
borderBottomRightRadius: '50%',
},
},
],
})) as React.ComponentType<DateRangePickerDayProps>;
export default function CustomDateRangePickerDay() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DemoContainer components={['DateRangeCalendar']}>
<DateRangeCalendar
defaultValue={[dayjs('2022-04-17'), dayjs('2022-04-21')]}
slots={{ day: DateRangePickerDay }}
/>
</DemoContainer>
</LocalizationProvider>
);
}
로컬라이제이션
자세한 내용은 Date format and localization 및 Translated components 문서 페이지를 참고하세요.
검증(Validation)
자세한 내용은 Validation 문서 페이지를 참고하세요.