0.1.3 • Published 5 months ago

s3d-react-datepicker v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

React DatePicker Component

A React implementation of the jQuery UI DateTimePicker plugin.

Overview

DatePicker is a React component for selecting dates, inspired by the jQuery UI DateTimePicker plugin. It provides a user-friendly interface for choosing dates with various configuration options.

Features

  • Interactive calendar for date selection.
  • Month and year dropdowns for easy navigation.
  • Customizable date format and language/locale.
  • Support for minimum and maximum selectable dates.
  • Right-to-left display option.
  • Callback function for handling date changes.

Installation

To install the DatePicker component in your React project, use the following command:

npm install s3d-react-datepicker

Usage

Import the DatePicker component into your React application:

import DatePicker from 's3d-react-datepicker';

Use the component in your JSX:

const YourComponent = () => {
    return (
        <div>
            <h1>My App</h1>
            <DatePicker />
        </div>
    );
};

Props

NameTypeRequiredDescription
idstringNoThe id property is a string representing the identifier associated with the Datepicker. It can be useful when associating the Datepicker with a label, particularly for the htmlFor attribute.
namestringNoThe name property represents the name associated with the input field. This is useful in a form to associate the field and its value during submission.
optionsobjectNoThe options property is an object allowing configuration of specific Datepicker options. All options are optional.
onChangefunctionNoThe onChange prop is a callback function triggered whenever a new date is selected in the DatePicker component. onChange: (selectedDate: Moment &vert; null) => void;
refReact.RefObjectNoThe ref property is a React reference object, providing direct access to the Datepicker instance. This can be used to access functions exposed by the component.

Examples

// Basic usage without optional props
<Datepicker />

// Using the optional 'name' prop for form association
<Datepicker name="eventDate" />

// Associating 'id' for accessibility with a label
<label htmlFor="birthdate">Birthdate :</label>
<Datepicker id="birthdate" />

// Providing 'options' for custom configuration
<Datepicker options={{ format: 'DD/MM/YYYY' }} />

// Using 'ref' to access Datepicker functions programmatically
const datepickerRef = useRef();
<Datepicker ref={datepickerRef}

// Using the 'onChange' prop to log the selected date to the console
<DatePicker onChange={(date) => {console.log('Selected date:', date?.format('DD/MM/YYYY'));}}/>

Exposed Functions

The Datepicker component exposes the following functions through the ref prop :

  • resetDatePicker() : Resets the date picker to its initial state.

Options

The options prop allows you to configure the behavior of the DateTimePicker component. Here are some key options:

OptionDescriptionDefault Value
datepickerEnable or disable the datepicker functionality.true
dayOfWeekStartSet the starting day of the week (0-6, Sunday to Saturday).0
defaultDateSet the default selected date.false
formatThe date and time format to display."YYYY-MM-DD"
langSet the language/locale for the component."en"
maxDateSet the maximum selectable date.false
minDateSet the minimum selectable date.false
rtlEnable or disable right-to-left layout.false
stylesCustomize the appearance of the DatePicker.{}
yearEndSet the end year for the year dropdown.2050
yearStartSet the start year for the year dropdown.1950

Style Customization

The DatePicker component provides flexibility in customizing its appearance through the styles option. You can customize different parts of the DatePicker component using either of the following approaches:

  1. Using CSS Class

    You can provide the name of a CSS class to override or add new styles.

  2. Using Object Style

    Alternatively, you can provide an object containing specific CSS properties to apply styles directly. This allows you to define individual styles for different elements of the DatePicker.

Available Style Options

VariableDescription
inputFieldCustom style for the date input field.
datepickerContainerCustom style for the overall DatePicker wrapper.
navigationSectionCustom style for the navigation section, including month change buttons.
dropdownContainerCustom style for the month and year dropdown container.
weekdaysHeaderCustom style for the section containing the names of the days of the week.
daysGridCustom style for the grid containing the numbers of the days in the month.

Note :

The class name approach can be used to easily redefine specific CSS variables within the datepickerContainer. The following variables can be overridden

VariableDescriptionDefault Value
--s3d-dp--color-backgroundBackground color of the DatePicker.white
--s3d-dp--color-calendar-gridColor of the calendar grid.#d9d9d9
--s3d-dp--color-cell-hoverColor when a cell is hovered.orange
--s3d-dp--color-current-dayColor of the current selected day.cornflowerblue
--s3d-dp--color-disabled-cellColor of disabled cells.#9f9f9f
--s3d-dp--color-disabled-cell-bgBackground color of disabled cells.gray
--s3d-dp--color-other-dayColor of days in other months.#9f9f9f
--s3d-dp--color-other-weekendColor of weekends in other months.#323232
--s3d-dp--color-scroll-barColor of the scroll bar.#646464
--s3d-dp--color-textDefault text color.black
--s3d-dp--font-sizeDefault font size.12px
--s3d-dp--color-nav-buttonDefault navigation button color.#9f9f9f
--s3d-dp--color-nav-button--hoverNavigation button color on hover.black

Example for Style Customization

To customize the style of the DatePicker, you can pass an object of styles as part of the options :

   /* custom.css file */
   .custom-datepickerContainer {
       /* override DatePicker variable */
       --s3d-dp--color-background: #f0f0f0;
       border-radius: 10px;       
   }
   
   .custom-menu-dropdown-style {
       padding: 10px;
   }
   
    /* ... other custom styles ... */
import "./custom.css";
import React, { useRef } from "react";
import DatePicker from "s3d-react-datepicker";

const App = () => {
    
    const customStyles = {
        inputField: { height: 40 },
        datepickerContainer: "custom-datepickerContainer",
        navigationSection: { background: "lightblue" },
        dropdownContainer: "custom-menu-dropdown-style",
        weekdaysHeader: { fontWeight: "bold" },
        daysGrid: { color: "green" },
    };

    return (
        <div>
            <h1>My App</h1>
            <DatePicker options={{ styles: customStyles }} />
        </div>
    );
};

export default App;

License

This DateTimePicker component is open-source and available under the MIT License.