chakra-dayzed-datepicker

repository·main·Indexed 42 days ago

https://github.com/aboveyunhai/chakra-dayzed-datepicker

A customizable datepicker component for Chakra UI, leveraging Dayzed for date logic and date-fns for date manipulation. Version 3.0.0 provides SingleDatepicker and RangeDatepicker components, featuring deep style customization via propsConfigs and flexible trigger variants including button, input, or custom children.

Tokens
6.7K
Snippets
25
Records
27
Agent score
51%

What's inside chakra-dayzed-datepicker

  1. Install chakra-dayzed-datepicker and dependencies

    main

    To use this library, you must install chakra-dayzed-datepicker along with its required peer dependencies: date-fns and dayzed.

    Using npm:

    npm i date-fns dayzed
    npm i chakra-dayzed-datepicker

    Using yarn:

    yarn add date-fns dayzed
    yarn add chakra-dayzed-datepicker
    npm i date-fns dayzed
    npm i chakra-dayzed-datepicker
  2. Configure component styles via propsConfigs

    main

    The propsConfigs prop allows you to pass Chakra UI props to various internal components to customize their appearance. This is useful for overriding default styles or applying global themes.

    Key configuration groups include:

    • dateNavBtnProps: Extends ButtonProps for navigation buttons.
    • dayOfMonthBtnProps: Customizes date buttons based on state. Style precedence: default < isInRange < selected < today.
    • popoverCompProps: Customizes the popover container and body.
    • calendarPanelProps: Customizes the layout wrapper, content, header, and dividers.
    • weekdayLabelProps and dateHeadingProps: Customizes text styles for labels and headings.
    • inputProps: Customizes the text input element.
    propsConfigs={{
      dateNavBtnProps: {
        colorScheme: "blue",
        variant: "outline"
      },
      dayOfMonthBtnProps: {
        defaultBtnProps: {
          borderColor: "red.300",
          _hover: { background: 'blue.400' }
        },
        isInRangeBtnProps: {
          color: "yellow",
        },
        selectedBtnProps: {
          background: "blue.200",
          color: "green",
        },
        todayBtnProps: {
          background: "teal.400",
        }
      },
      inputProps: {
        size: "sm"
      },
      popoverCompProps: {
        popoverContentProps: {
          background: "gray.700",
          color: "white",
        },
      },
      calendarPanelProps: {
        wrapperProps: {
          borderColor: 'green',
        },
        contentProps: {
          borderWidth: 0,
        },
        headerProps: {
          padding: '5px',
        },
        dividerProps: {
          display: "none",
        },
      },
      weekdayLabelProps: {
        fontWeight: 'normal'
      },
      dateHeadingProps: {
        fontWeight: 'semibold'
      }
    }}
  3. Configure date logic via configs

    main

    The configs prop is used for non-Chakra-related logic, such as date formatting and localization.

    Available keys:

    • dateFormat: String defining the date format (e.g., 'yyyy-MM-dd').
    • dayNames: Array of 7 strings for day names (e.g., ['Sun', 'Mon', ...]).
    • monthNames: Array of 12 strings for month names.
    • firstDayOfWeek: Integer representing the index of the first day of the week (default is 0, which is Sunday).
    configs={{
      dateFormat: 'yyyy-MM-dd',
      dayNames: 'abcdefg'.split(''),
      monthNames: 'ABCDEFGHIJKL'.split(''),
      firstDayOfWeek: 2,
    }}
  4. Use SingleDatepicker for single date selection

    main

    The SingleDatepicker component is used for selecting a single date. It requires a date prop for the current value and an onDateChange callback to update the state.

    import { SingleDatepicker } from "chakra-dayzed-datepicker";
    import { useState } from "react";
    
    const [date, setDate] = useState(new Date());
    
    <SingleDatepicker
      name="date-input"
      date={date}
      onDateChange={setDate}
    />
    import { SingleDatepicker } from "chakra-dayzed-datepicker";
    
    const [date, setDate] = useState(new Date());
    
    <SingleDatepicker
      name="date-input"
      date={date}
      onDateChange={setDate}
    />
  5. Use RangeDatepicker for date range selection

    main

    The RangeDatepicker component allows users to select a range of dates.

    Important: The selectedDates state will contain a single value during the intermediate selection process (when the user has clicked the start date but not yet the end date). Do not attempt to force the state to strictly follow a [startDate, endDate] pattern, as the length of the selectedDates array is used by the component to manage the selection lifecycle.

    import { RangeDatepicker } from "chakra-dayzed-datepicker";
    import { useState } from "react";
    
    const [selectedDates, setSelectedDates] = useState<Date[]>([new Date(), new Date()]);
    
    <RangeDatepicker
      selectedDates={selectedDates}
      onDateChange={setSelectedDates}
    />
    import { RangeDatepicker } from "chakra-dayzed-datepicker";
    
    const [selectedDates, setSelectedDates] = useState<Date[]>([new Date(), new Date()]);
    
    <RangeDatepicker
      selectedDates={selectedDates}
      onDateChange={setSelectedDates}
    />
  6. Customize DayOfMonth button styles via propsConfigs

    main

    You can customize the visual appearance of the individual day buttons within the calendar using the propsConfigs.dayOfMonthBtnProps property. This allows you to override the default Chakra UI styles for different states of a date button.

    Supported style groups:

    • defaultBtnProps: Styles for standard, unselected days.
    • isInRangeBtnProps: Styles for days that fall within a selected range.
    • selectedBtnProps: Styles for the currently selected day(s).
    • todayBtnProps: Styles for the current day.

    Note: The component applies a default _after pseudo-element to defaultBtnProps to expand the clickable area and improve UX by filling visual gaps in the grid.

    const propsConfigs = {
      dayOfMonthBtnProps: {
        defaultBtnProps: {
          variant: 'outline',
          _hover: { bg: 'blue.50' }
        },
        selectedBtnProps: {
          bg: 'blue.500',
          color: 'white'
        },
        isInRangeBtnProps: {
          bg: 'blue.100'
        },
        todayBtnProps: {
          fontWeight: 'bold'
        }
      }
    };
  7. Customize RangeDatepicker internal components with propsConfigs

    main

    You can pass specific Chakra UI props to the internal components of the RangeDatepicker using the propsConfigs object. This allows for deep customization of the UI elements without rebuilding the component.

    Available configuration paths within propsConfigs:

    • triggerBtnProps: Props for the Button used in the default variant.
    • inputProps: Props for the Input used in the input variant.
    • triggerIconBtnProps: Props for the icon button in the input variant.
    • popoverCompProps.popoverContentProps: Props for the Popover.Content.
    • popoverCompProps.popoverBodyProps: Props for the Popover.Body.
    <RangeDatepicker
      selectedDates={dates}
      onDateChange={setDates}
      propsConfigs={{
        triggerBtnProps: { colorScheme: 'blue' },
        popoverCompProps: {
          popoverContentProps: { shadow: 'xl' }
        }
      }}
    />
  8. Configure Datepicker behavior with configs

    main

    The configs prop allows you to customize the internal calendar logic and date formatting. These settings are merged with DefaultConfigs.

    Supported keys in DatepickerConfigs:

    • dateFormat: A date-fns compatible format string (e.g., 'yyyy-MM-dd'). This is used for both displaying the date in the trigger and parsing manual input in the input variant.
    • monthNames: Array of short month names.
    • dayNames: Array of short weekday names.
    • firstDayOfWeek: The index of the first day of the week (e.g., 0 for Sunday).
    • monthsToDisplay: Number of months to show in the calendar panel.
    <SingleDatepicker
      configs={{
        dateFormat: 'MM/dd/yyyy',
        firstDayOfWeek: 1, // Monday
        monthsToDisplay: 2
      }}
      date={selectedDate}
      onDateChange={onDateChange}
    />
  9. Customize internal component styles via PropsConfigs

    main

    The PropsConfigs interface allows you to pass Chakra UI component props (like ButtonProps, InputProps, StackProps, etc.) to the various sub-components of the datepicker. This is useful for matching the datepicker to your application's theme.

    Available configuration groups include:

    • dateNavBtnProps: Props for date navigation buttons.
    • dayOfMonthBtnProps: Styles for day buttons (supports defaultBtnProps, isInRangeBtnProps, selectedBtnProps, and todayBtnProps).
    • inputProps: Props for the date input field.
    • triggerBtnProps: Props for the button that triggers the popover.
    • popoverCompProps: Props for the popover components (popoverRootProps, popoverContentProps, popoverBodyProps).
    • calendarPanelProps: Props for the calendar layout (wrapperProps, contentProps, headerProps, bodyProps, dividerProps).
    • dateHeadingProps: Props for the date heading.
    • weekdayLabelProps: Props for the weekday labels.
  10. Configure SingleDatepicker via propsConfigs

    main

    The propsConfigs option allows you to pass Chakra UI component props directly to the internal components of the SingleDatepicker. This is useful for deep customization of the UI without replacing the entire component.

    Available configuration paths within propsConfigs:

    • triggerBtnProps: Props for the Button when triggerVariant is 'default'.
    • inputProps: Props for the Input field when triggerVariant is 'input'.
    • triggerIconBtnProps: Props for the icon button when triggerVariant is 'input'.
    • popoverCompProps.popoverRootProps: Props for the Popover.Root component.
    • popoverCompProps.popoverContentProps: Props for the Popover.Content component.
    • popoverCompProps.popoverBodyProps: Props for the Popover.Body component.
    <SingleDatepicker
      triggerVariant="input"
      propsConfigs={{
        inputProps: { placeholder: 'Select a date...' },
        triggerIconBtnProps: { colorScheme: 'blue' },
        popoverCompProps: {
          popoverBodyProps: { borderWidth: '2px' }
        }
      }}
      date={selectedDate}
      onDateChange={onDateChange}
    />
  11. Reference: Component Props

    main

    Common props available for both SingleDatepicker and RangeDatepicker components.

    | Name | single/range | Type | Default | Description |
    |------|--------------|------|---------|-------------|
    | `name` | both | string | undefined | name attribute for `<input />` element |
    | `usePortal` | both | boolean | undefined | to prevent parent styles from clipping or hiding content |
    | `defaultIsOpen` | both | boolean | false | open the date panel at the beginning |
    | `closeOnSelect` | both | boolean | true | close the date panel upon the complete selection |
    | `minDate` | both | Date | undefined | minimum date |
    | `maxDate` | both | Date | undefined | maximum date |
    | `disabledDates` | single | Set<number> | undefined | for single datepicker only, uses `startOfDay` as comparison (e.g. `new Set([startOfDay(new Date()).getTime()])`) |
  12. Custom trigger for RangeDatepicker using children

    main

    If you want to implement a completely custom trigger (e.g., a custom styled div or a complex component) instead of the default Button or Input, use the children prop as a render function. The function receives the current selectedDates as an argument.

    <RangeDatepicker
      selectedDates={dates}
      onDateChange={setDates}
    >
      {(selected) => (
        <button onClick={() => {/* logic to open popover if needed */}}>
          {selected.length === 2 
            ? `${selected[0].toLocaleDateString()} - ${selected[1].toLocaleDateString()}`
            : 'Select Range'}
        </button>
      )}
    </RangeDatepicker>