feat(react-time): useDatePicker #9
Closed
bart-krakowski(GitHub profile, opens in a new tab)wants to merge 32 commits into
v0 fromrefs/pull/9/head·opened Summary#
This RFC proposes the creation of a custom date picker hook, useDatePicker, designed to manage date selection functionalities including date constraints, month navigation, and date selection callbacks.
Motivation#
The motivation behind this RFC is to provide a reusable and efficient date picker hook that can be integrated into various components, ensuring consistent and simplified management of date selection functionalities. This hook aims to support date constraints (min and max dates), month navigation, and provide utility functions for selecting and handling dates.
Detailed Design#
Hook Interface#
import { Temporal } from '@js-temporal/polyfill';
interface UseDatePickerProps {
initialDate?: Temporal.PlainDate | null;
minDate?: Temporal.PlainDate | null;
maxDate?: Temporal.PlainDate | null;
onSelectDate?: (date: Temporal.PlainDate) => void;
}
export const useDatePicker: (props: UseDatePickerProps) => {
selectedDate: Temporal.PlainDate | null;
minDate: Temporal.PlainDate | null;
maxDate: Temporal.PlainDate | null;
days: Array<Array<Temporal.PlainDate>>;
selectDate: (date: Temporal.PlainDate) => void;
getPrev: () => void;
getNext: () => void;
getCurrent: () => void;
get: (date: Temporal.PlainDate) => void;
dayNames: string[]
};
Example Usage#
const DatePickerComponent = () => {
const {
selectedDate,
minDate,
maxDate,
days,
selectDate,
getPrev,
getNext,
getCurrent,
daysNames,
} = useDatePicker({
initialDate: Temporal.PlainDate.from('2024-06-01'),
minDate: Temporal.PlainDate.from('2024-01-01'),
maxDate: Temporal.PlainDate.from('2024-12-31'),
onSelectDate: (date) => console.log('Date selected:', date),
});
return (
<div className="date-picker-container">
<div className="date-picker-header">
<button onClick={getPrev}>Previous</button>
<button onClick={getCurrent}>Today</button>
<button onClick={getNext}>Next</button>
</div>
<table className="date-picker-table">
<thead>
<tr>
{daysNames.map((day) => (
<th key={day}>{day}</th>
))}
</tr>
</thead>
<tbody>
{days.map((week, weekIndex) => (
<tr key={weekIndex}>
{week.map((day) => (
<td
key={day.toString()}
className={day.equals(selectedDate) ? 'selected' : ''}
onClick={() => selectDate(day)}
>
{day.day}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
};- 1
Sign in to join this conversation.