DateField

Component for entering date in forms.

The <DateField> allows users to enter and edit date values using a keyboard. Each part of a date value is displayed in an individually editable segment. These segments are individually focusable and editable by the user, by typing or using the arrow keys to increment and decrement the value. This approach allows values to be formatted and parsed correctly regardless of the locale or date format, and offers an easy and error-free way to edit dates using the keyboard.

Anatomy

A <DateField> consists of a label and a group of segments representing each unit of a date and time, such as years, months, days, hours, and minutes.

Anatomy of datefield

Appearance

The appearance of a component can be customized using the variant and size props. These props adjust the visual style and dimensions of the component, available values are based on the active theme.

Variant
Size
The selected theme does not has any options for "variant" and "size".
Event Date
10152023
Select the date for your ticket event.
PropertyTypeDescription
variant-The available variants of this component.
size-The available sizes of this component.

Usage

The <DateField> component is a versatile input element used for selecting dates in forms. It enhances user experience by providing a structured way to input date information, reducing errors associated with manual entry. With features like labels and descriptions, it clearly communicates its purpose to users. The component can also support default values. Overall, the <DateField> is essential for applications requiring date selection, ensuring both accessibility and usability.

Props

Did you know? You can explore, test, and customize props live in Marigold's storybook. Watch the effects they have in real-time!
action?
ReactElement<any, string | JSXElementConstructor<any>>;
Optional element (e.g., button or icon) rendered inside the DateField.
aria-describedby?
string;
Identifies the element (or elements) that describes the object.
aria-details?
string;
Identifies the element (or elements) that provide a detailed, extended description for the object.
aria-label?
string;
Defines a string value that labels the current element.
aria-labelledby?
string;
Identifies the element (or elements) that labels the current element.
autoFocus?
boolean;
Whether the element should receive focus on render.
defaultValue?
DateValue | null;
The default value (uncontrolled).
description?
ReactNode;
A helpful text.
disabled?
boolean;
If true, the date field is disabled.
Defaults to:
"false"
error?
boolean;
If true, the field is considered invalid and if set the errorMessage is shown instead of the description.
Defaults to:
"false"
errorMessage?
ReactNode | ((v: ValidationResult) => ReactNode);
An error message.
granularity?
Granularity;
Determines the smallest unit that is displayed in the date picker. By default, this is "day" for dates, and "minute" for times.
hideTimeZone?
boolean;
Whether to hide the time zone abbreviation.
Defaults to:
"false"
hourCycle?
12 | 24;
Whether to display the time in 12 or 24 hour format. By default, this is determined by the user's locale.
id?
string;
The element's unique identifier. See MDN.
isDateUnavailable?
(date: DateValue) => boolean;
Callback that is called for each date of the calendar. If it returns true, then the date is unavailable.
label?
ReactNode;
Specifies the label of the field.
maxValue?
DateValue | null;
The maximum allowed date that a user may select.
minValue?
DateValue | null;
The minimum allowed date that a user may select.
name?
string;
The name of the input element, used when submitting an HTML form. See MDN.
onBlur?
(e: FocusEvent<Element, Element>) => void;
Handler that is called when the element loses focus.
onChange?
(value: DateValue | null) => void;
Handler that is called when the value changes.
onFocus?
(e: FocusEvent<Element, Element>) => void;
Handler that is called when the element receives focus.
onFocusChange?
(isFocused: boolean) => void;
Handler that is called when the element's focus status changes.
onKeyDown?
(e: KeyboardEvent) => void;
Handler that is called when a key is pressed.
onKeyUp?
(e: KeyboardEvent) => void;
Handler that is called when a key is released.
placeholderValue?
DateValue | null;
A placeholder date that influences the format of the placeholder shown when no value is selected. Defaults to today's date at midnight.
readOnly?
boolean;
If true, the date field is readOnly.
Defaults to:
"false"
ref?
Ref<HTMLInputElement>;
Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref). @see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs}
required?
boolean;
If true, the date field is required.
Defaults to:
"false"
shouldForceLeadingZeros?
boolean;
Whether to always show leading zeros in the month, day, and hour fields. By default, this is determined by the user's locale.
slot?
string | null;
A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.
validate?
(value: DateValue) => true | ValidationError | null;
A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead.
validationBehavior?
"native" | "aria";
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.
Defaults to:
'native'
value?
DateValue | null;
The current value (controlled).
width?
WidthProp;
Sets the width of the field. You can see allowed tokens here: https://tailwindcss.com/docs/width
Defaults to:
'full'

Alternative components

  • DatePicker: a user interface element that allows users to select a date from a calendar.

Last update: 6 months ago