Skip to main content

Introducing Workflow Engine, try for FREE workflowengine.io.

DatePicker

Installation

Use the following command to install the package:

npm install @react-form-builder/components-material-ui

The DatePicker component uses additional dependencies to handle dates. Install them if your package manager does not install peer dependencies automatically.

npm install @mui/x-date-pickers dayjs

Introduction

The DatePicker component provides a calendar interface for users to select a date. It offers a user-friendly way to input dates with proper validation, localization support, and Material Design styling. The component includes features like date range restrictions, different views (day, month, year), and various display options.

The DatePicker component is based on Material UI X DatePicker. It is included within the following NPM package: @react-form-builder/components-material-ui.

The component uses Day.js for date manipulation and supports customizable date formats through the format property. Date values should be provided as strings in the specified format or as ISO 8601 strings that can be parsed by Day.js.

The DatePicker component can be configured to fit specific requirements using the properties described below.

Main properties

The following general properties are available within the Main tab:

Basic configuration

NameTypeDescriptionDefault
LabelstringText label displayed above the date picker field-
DisabledbooleanIf true, the date picker is disabled and cannot be interacted withfalse
ErrorbooleanApplies error styling to the field to indicate an invalid valuefalse
Helper textstringHelper text displayed below the field to provide additional context or validation messages-
RequiredbooleanIf true, the field is marked as required (adds an asterisk to the label)false
ValuedateThe currently selected date value (must be a valid date)-
FormatstringDate format string used for display and parsing (e.g., "yyyy-MM-dd", "MM/dd/yyyy")-
Read onlybooleanIf true, the date picker is read-only and cannot be editedfalse
Auto focusbooleanIf true, automatically focuses the date picker when the component mountsfalse
NamestringThe name attribute for the form field, used when submitting forms-

Date restrictions

NameTypeDescriptionDefault
Min datedateEarliest date that can be selected (inclusive)-
Max datedateLatest date that can be selected (inclusive)-
Disable pastbooleanIf true, prevents selection of dates before the current datefalse
Disable futurebooleanIf true, prevents selection of dates after the current datefalse
Reference datedateDate used to generate the initial calendar view when no value is selected-
Default valuedateInitial date value when the component is first rendered (if no value is set)-

Display and behavior

NameTypeDescriptionDefault
Close on selectbooleanCloses the date picker dialog automatically after a date is selectedtrue
Display week numberbooleanShows week numbers in the calendar viewfalse
Show days outside current monthbooleanDisplays days from the previous and next months in the current month viewfalse
ViewchoiceThe initial view to display (day, month, or year calendar)-
ViewschoiceThe available views (day, month, and year calendar)-
Open tochoiceThe view to open when the picker is activated-
Format densitychoiceControls spacing in date formatting: dense (compact) or spacious (more spacing)dense
Fixed week numbernumberFixed number of weeks to display (forces consistent calendar height)-
Months per rownumberNumber of month calendars to display per row in month/year views-
Years per rownumberNumber of year items to display per row in year view-
Years orderchoiceSorting order of years: ascending (oldest first) or descending (newest first)asc

State and interaction

NameTypeDescriptionDefault
LoadingbooleanIf true, displays a loading spinner indicating data is being loadedfalse
OpenbooleanIf true, the date picker dialog is open; can be used for controlled state managementfalse
Disable open pickerbooleanIf true, prevents opening the picker dialog (useful for read-only display)false
Disable highlight todaybooleanIf true, disables the visual highlighting of the current date in the calendarfalse
Desktop mode media querystringCSS media query string that determines when to switch to desktop mode layout-

Event handlers

NameTypeDescription
onChangeeventFired when the date value changes
onAccepteventFired when a date is accepted (e.g., on Enter key press or when clicking "OK")
onCloseeventFired when the picker dialog closes
onErroreventFired when date validation fails
onMonthChangeeventFired when the month view changes
onOpeneventFired when the picker dialog opens
onSelectedSectionsChangeeventFired when selected sections in the input field change
onViewChangeeventFired when the view (day/month/year) changes
onYearChangeeventFired when the year view changes

At the bottom of the Main properties menu, you can find additional options:

Style properties

These are properties that allow to customize the size, color and other style properties of your component:

  • For Device: You can provide component's style customization for all devices or specific devices by using this dropdown list. Refer to Adaptive Layout for more details.
  • Component > Class Name: Add a class name for your component for the subsequent styling if necessary.
  • Wrapper: The options below enable you to tailor the appearance of your component and customize it for a specific device type, as selected in the For Device property:

    NameTypeDescription
    WidthnumberSpecifies the width for your component
    HeightnumberSpecifies the height for your component
    Margin top/right/bottom/leftnumberMargin values for you component
    ColorstringAllows customizing the text color
    Background colorstringEnables customizing the background color

Localization Setup

The DatePicker component supports multiple languages through Day.js locales. To enable localization:

  1. Import the required Day.js locales in your application:
import 'dayjs/locale/en.js'
import 'dayjs/locale/de.js'
import 'dayjs/locale/ar.js'
import 'dayjs/locale/it.js'
import 'dayjs/locale/fa.js'
import 'dayjs/locale/fr.js'
import 'dayjs/locale/hi.js'
import 'dayjs/locale/es.js'
import 'dayjs/locale/ja.js'
import 'dayjs/locale/ko.js'
import 'dayjs/locale/sr.js'
import 'dayjs/locale/zh.js'
note

You only need to import the locales that your application actually uses. Importing unnecessary locales will increase your bundle size.

  1. Wrap your form builder or viewer with MuiLocalizationProvider:
import {BuilderView} from '@react-form-builder/core'
import {components, MuiViewerWrapper, MuiLocalizationProvider} from '@react-form-builder/components-material-ui'

const builderView = new BuilderView(components)
.withViewerWrapper(MuiViewerWrapper)
.withViewerWrapper(MuiLocalizationProvider)

The MuiLocalizationProvider automatically detects the current language from the form context and applies the appropriate locale to all date pickers. If no language is specified, it defaults to English (en).

Supported Locales

The DatePicker component supports the following locales out of the box:

  • English (en)
  • German (de)
  • Arabic (ar)
  • Italian (it)
  • Persian (fa)
  • French (fr)
  • Hindi (hi)
  • Spanish (es)
  • Japanese (ja)
  • Korean (ko)
  • Serbian (sr)
  • Chinese (zh)

To add support for additional locales, import the corresponding Day.js locale file and ensure it's available in your application bundle.