import * as React from 'react'; import { TimePicker, DatePicker, Label, Text, IStackTokens, Stack, IStackStyles, IComboBox } from '@fluentui/react'; const stackStyles: Partial = { root: { width: 500 } }; const stackTokens: IStackTokens = { childrenGap: 20 }; const snapTimeToUpdatedDateAnchor = (datePickerDate: Date, currentTime: Date) => { let snappedTime = new Date(currentTime); if (currentTime && !isNaN(currentTime.valueOf())) { const startAnchor = new Date(datePickerDate); const endAnchor = new Date(startAnchor); endAnchor.setDate(startAnchor.getDate() + 1); if (currentTime < startAnchor || currentTime > endAnchor) { snappedTime = new Date(startAnchor); snappedTime.setHours(currentTime.getHours()); snappedTime.setMinutes(currentTime.getMinutes()); snappedTime.setSeconds(currentTime.getSeconds()); snappedTime.setMilliseconds(currentTime.getMilliseconds()); } } return snappedTime; }; export const TimePickerDateTimePickerExample: React.FC = () => { const currentDate = new Date('2023-02-01 05:00:00'); const [datePickerDate, setDatePickerDate] = React.useState(currentDate); const [currentTime, setCurrentTime] = React.useState(); const onSelectDate = React.useCallback( (selectedDate: Date) => { setDatePickerDate(selectedDate); if (currentTime) { const snappedTime = snapTimeToUpdatedDateAnchor(selectedDate, currentTime); setCurrentTime(snappedTime); } }, [currentTime], ); const onTimePickerChange = React.useCallback((_ev: React.FormEvent, date: Date) => { setCurrentTime(date); }, []); return (
{`⚓ Date anchor: ${datePickerDate.toString()}`} {`⌚ Selected time: ${currentTime ? currentTime.toString() : ''}`}
); };