이벤트 타임라인 - 내비게이션
이벤트 타임라인 - 내비게이션 (Event Timeline - Navigation)
명령형 API로 표시 날짜를 제어하고 프로그래밍 방식으로 내비게이션할 수 있어요.
본문
기본 표시 날짜 (Default visible date)
defaultVisibleDate prop을 사용해 표시 날짜를 초기화할 수 있어요:
const defaultVisibleDate = DateTime.fromISO('2025-11-01');
<EventTimelinePremium defaultVisibleDate={defaultVisibleDate} />;
import * as React from 'react';
import { SchedulerEvent } from '@mui/x-scheduler/models';
import { EventTimelinePremium } from '@mui/x-scheduler-premium/event-timeline-premium';
import { initialEvents, resources } from '../../datasets/company-roadmap';
const defaultVisibleDate = new Date('2025-11-01');
export default function DefaultVisibleDate() {
const [events, setEvents] = React.useState<SchedulerEvent[]>(initialEvents);
return (
<div style={{ height: '500px', width: '100%', overflow: 'auto' }}>
<EventTimelinePremium
events={events}
resources={resources}
defaultVisibleDate={defaultVisibleDate}
onEventsChange={setEvents}
defaultPreset="monthAndYear"
/>
</div>
);
}
:::success
visibleDate와 onVisibleDateChange prop을 사용해서도 표시 날짜를 제어할 수 있어요:
const [visibleDate, setVisibleDate] = React.useState(() => DateTime.now());
return (
<EventTimelinePremium
visibleDate={visibleDate}
onVisibleDateChange={setVisibleDate}
/>
);
:::
명령형 API (Imperative API)
apiRef 객체를 사용하려면 useEventTimelinePremiumApiRef() 훅으로 초기화해야 해요:
const apiRef = useEventTimelinePremiumApiRef();
return <EventTimelinePremium apiRef={apiRef} events={EVENTS} />;
컴포넌트가 처음 렌더링될 때 apiRef.current는 undefined예요. 초기 렌더링 이후에는 apiRef가 Event Timeline과 명령형으로 상호작용할 수 있는 메서드를 보유하게 돼요.
표시 날짜 설정하기 (Set the visible date)
주어진 날짜로 이동하려면 setVisibleDate() API 메서드를 사용해요:
apiRef.current.setVisibleDate({
// The DOM event that triggered the change (will be passed to onVisibleDateChange if provided).
event,
// The date to navigate to.
visibleDate,
});
import * as React from 'react';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import { SchedulerEvent } from '@mui/x-scheduler/models';
import { EventTimelinePremium } from '@mui/x-scheduler-premium/event-timeline-premium';
import { useEventTimelinePremiumApiRef } from '@mui/x-scheduler-premium/use-event-timeline-premium-api-ref';
import {
initialEvents,
resources,
defaultVisibleDate,
} from '../../datasets/company-roadmap';
export default function ApiMethodSetVisibleDate() {
const [events, setEvents] = React.useState<SchedulerEvent[]>(initialEvents);
const apiRef = useEventTimelinePremiumApiRef();
return (
<Stack spacing={2} sx={{ width: '100%', alignItems: 'flex-start' }}>
<Button
variant="contained"
onClick={(event) =>
apiRef.current?.setVisibleDate({ visibleDate: new Date(), event })
}
>
Navigate to today
</Button>
<div style={{ height: '500px', width: '100%', overflow: 'auto' }}>
<EventTimelinePremium
apiRef={apiRef}
events={events}
resources={resources}
defaultVisibleDate={defaultVisibleDate}
onEventsChange={setEvents}
defaultPreset="monthAndYear"
/>
</div>
</Stack>
);
}
다음/이전 범위로 이동하기 (Navigate to the next and previous range)
시간을 앞뒤로 이동하려면 goToNextVisibleDate()와 goToPreviousVisibleDate() API 메서드를 사용해요. 이동 간격(step)은 현재 뷰에 따라 자동으로 결정돼요:
| View | Step |
|---|---|
time |
4 days |
days |
56 days (8 weeks) |
weeks |
16 weeks |
months |
36 months (3 years) |
years |
30 years |
// Navigate to the next date range
apiRef.current.goToNextVisibleDate(event);
// Navigate to the previous date range
apiRef.current.goToPreviousVisibleDate(event);
import * as React from 'react';
import Stack from '@mui/material/Stack';
import IconButton from '@mui/material/IconButton';
import Button from '@mui/material/Button';
import Select, { SelectChangeEvent } from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
import ChevronRightIcon from '@mui/icons-material/ChevronRight';
import { EventTimelinePremium } from '@mui/x-scheduler-premium/event-timeline-premium';
import { useEventTimelinePremiumApiRef } from '@mui/x-scheduler-premium/use-event-timeline-premium-api-ref';
import { EventTimelinePremiumPreset } from '@mui/x-scheduler-internals-premium/models';
import {
initialEvents,
resources as allResources,
defaultVisibleDate,
} from '../../datasets/company-roadmap';
const resources = allResources.slice(0, 5);
const presetOptions: { value: EventTimelinePremiumPreset; label: string }[] = [
{ value: 'dayAndHour', label: 'Time' },
{ value: 'dayAndMonth', label: 'Days' },
{ value: 'dayAndWeek', label: 'Weeks' },
{ value: 'monthAndYear', label: 'Months' },
{ value: 'year', label: 'Years' },
];
export default function ExternalNavigation() {
const [events, setEvents] = React.useState(initialEvents);
const [preset, setPreset] =
React.useState<EventTimelinePremiumPreset>('monthAndYear');
const [visibleDate, setVisibleDate] = React.useState<Date>(defaultVisibleDate);
const apiRef = useEventTimelinePremiumApiRef();
const handlePresetChange = (event: SelectChangeEvent) => {
setPreset(event.target.value as EventTimelinePremiumPreset);
};
return (
<Stack spacing={2} sx={{ width: '100%' }}>
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
<IconButton
onClick={(event) => apiRef.current?.goToPreviousVisibleDate(event)}
>
<ChevronLeftIcon />
</IconButton>
<Button
variant="outlined"
onClick={(event) =>
apiRef.current?.setVisibleDate({ visibleDate: new Date(), event })
}
>
Today
</Button>
<IconButton onClick={(event) => apiRef.current?.goToNextVisibleDate(event)}>
<ChevronRightIcon />
</IconButton>
<Select value={preset} onChange={handlePresetChange} size="small">
{presetOptions.map(({ value, label }) => (
<MenuItem key={value} value={value}>
{label}
</MenuItem>
))}
</Select>
</Stack>
<div style={{ height: '500px', width: '100%', overflow: 'auto' }}>
<EventTimelinePremium
apiRef={apiRef}
events={events}
resources={resources}
preset={preset}
onPresetChange={setPreset}
visibleDate={visibleDate}
onVisibleDateChange={setVisibleDate}
onEventsChange={setEvents}
/>
</div>
</Stack>
);
}