React Wrapper for Flatpickr.js
This package provides a React wrapper for the popular date and time picker library, Flatpickr.js. It allows you to easily integrate Flatpickr into your React applications with a variety of customizable options and hooks with typescript support and fully tested functionality.
To install the package, use npm or yarn:
npm install flatpickr flatpickr-react
or
yarn add flatpickr flatpickr-react
To use the DateTimePicker
component, import it into your React component and include it in your JSX:
import React, { useState } from "react";
import DateTimePicker from "flatpickr-react";
import "flatpickr/dist/flatpickr.css";
function App() {
const [date, setDate] = (useState < Date) | (null > null);
return (
<div>
<DateTimePicker
options={{
dateFormat: "Y-m-d",
}}
value={date}
onChange={(selectedDate: Date) => {
setDate(selectedDate);
}}
/>
<p>Selected Date: {date ? date.toString() : "None"}</p>
</div>
);
}
export default App;
The DateTimePicker
component accepts the following props:
-
defaultValue
: The default value of the input. -
options
: Flatpickr options object. -
locale
: Locale settings for Flatpickr. -
plugins
: Array of Flatpickr plugins. -
onChange
: Callback function for theonChange
event. -
onOpen
: Callback function for theonOpen
event. -
onClose
: Callback function for theonClose
event. -
onMonthChange
: Callback function for theonMonthChange
event. -
onYearChange
: Callback function for theonYearChange
event. -
onReady
: Callback function for theonReady
event. -
onValueUpdate
: Callback function for theonValueUpdate
event. -
onDayCreate
: Callback function for theonDayCreate
event. -
value
: The current value of the input. -
className
: Custom class name for the input. -
children
: Custom children elements. -
render
: Custom render function for the input. -
onCreate
: Callback function for when the Flatpickr instance is created. -
onDestroy
: Callback function for when the Flatpickr instance is destroyed.
<DateTimePicker
options={{
dateFormat: "Y-m-d",
}}
/>
<DateTimePicker
options={{
enableTime: true,
dateFormat: "Y-m-d H:i",
}}
/>
<DateTimePicker
options={{
mode: "range",
dateFormat: "Y-m-d",
}}
/>
<DateTimePicker
options={{
mode: "multiple",
dateFormat: "Y-m-d",
}}
/>
import { Spanish } from "flatpickr/dist/l10n/es";
<DateTimePicker
options={{
dateFormat: "Y-m-d",
}}
locale={Spanish}
/>;
<DateTimePicker
render={(props, ref) => (
<div className="custom-wrapper">
<label>Select Date:</label>
<button {...props} ref={ref}>
Pick a date
</button>
</div>
)}
/>
<DateTimePicker
onOpen={() => console.log("Opened")}
onClose={() => console.log("Closed")}
onChange={(selectedDates) => console.log("Date changed", selectedDates)}
onMonthChange={(selectedDates, dateStr, instance) => console.log("Month changed")}
onYearChange={(selectedDates, dateStr, instance) => console.log("Year changed")}
onReady={(selectedDates, dateStr, instance) => console.log("Ready")}
onValueUpdate={(selectedDates, dateStr, instance) => console.log("Value updated")}
onDayCreate={(selectedDates, dateStr, instance, dayElement) => console.log("Day created")}
/>
<DateTimePicker
options={{
position: "top",
}}
/>
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.
Feel free to customize the README file further to match your specific needs and preferences.