«date» Component Type
📖 Table of Contents
Introduction
The date component type extends the Input component type, providing specialized handling for date input fields with date parsing and ISO formatting.
- Imports and Exports: ISO date string (
YYYY-MM-DD) ornull(when empty) - Singleton Pattern: Implements the Singleton Pattern
- Data Conversion: Converts between various date formats and ISO date strings
Usage
Use <input type="date" data-smark>. SmarkForm also infers date from the native type, or sets the native type when you explicitly declare {"type":"date"} on an <input> without type.
<input data-smark='{"type":"date", "name":"birthdate"}'>
Alternatively, you can also use the shorthand notation inferring the type from the <input> element type:
<input type="date" name="birthdate" data-smark>
Example:
<div id="myForm">
<input type="date" name="birthdate" data-smark>
</div>
const myForm = new SmarkForm(document.getElementById("myForm"), {
"value": {
"birthdate": "1990-07-15"
}
});
Data Formats
The date component accepts multiple input formats for maximum flexibility:
- ISO Date String:
"2023-12-25"(standard HTML date input format) - Compact Date String:
"20231225"(YYYYMMDD format) - Date Object: a valid JavaScript
Dateinstance - Epoch Timestamp: milliseconds since Unix epoch; these inputs are treated as instants and exported using their UTC calendar date
- Empty/Null Values:
nullor""clears the field.undefinedis a reset signal; see the reset caveat below.
All valid dates are normalized to ISO date format (YYYY-MM-DD) for export, ensuring consistency across your application.
Date-only strings use the calendar date as written. A Date object or epoch is an instant; the exported date comes from toISOString() (UTC), which can differ from the local calendar date near a timezone boundary.
Field defaults and reset
Current implementation caveat: date.import(undefined) normalizes the reset signal to null before delegating to the generic input type. A direct field reset() therefore clears to null and can replace the field’s stored default with null; the base field’s initial reset can also discard a field-local value default. A parent Form reset that imports an explicit date in its stored object still restores that value. No focused regression test covers date field-level defaults; see the Step 14 transfer map.
Requirements
The date component requires an <input type="date">; a missing native type is filled in, while a different type or tag raises a render error. Unparseable strings export as null. Only valid Date instances are supported; an invalid Date can throw during ISO conversion.
<div id="myForm">
<p>
<label>Correct:</label>
<input type="date" name="birthdate1" data-smark='{"type":"date"}'>
</p>
<p>
<label>Incorrect:</label>
<input type="text" name="birthdate2" data-smark='{"type":"date"}'>
</p>
</div>
const myForm = new SmarkForm(document.getElementById("myForm"));
When a component fails to render, SmarkForm replaces it with an error placeholder that shows the code and provides a button to replay the error in the console.
👉 Take a look to the Preview tab of the previous example to see it in action.
Notice that you’ll need to open your browser console if you want to see the error details when pressing the “Replay Error” button.
API Reference
Actions
Actions are special component methods that can be triggered both programmatically (by directly calling the function) or by user interaction with a trigger component. Each action is associated with a specific behavior that can be performed on the component.
👉 Actions receive an
optionsobject as an argument, which contains the necessary information to perform the action.👉 When called from triggers, the properties defined in their
data-smarkobject, are passed asoptionsto the action. Those expecting a path(★ ) to other components are resolved to the actual component.
The date component type supports the following actions:
(Async) export (Action)
Exports the value of the date input field as an ISO date string. If the field is empty or contains an invalid date, it returns null.
Options (export)
- action: (= “export”)
- origin: Origin is automatically set to the trigger component that called the action (and cannot be overridden). In case of a manual call, it is set to Null.
- context: Path★ (absolute or relative to its natural context) to the component that is the context of the action. If not provided, the context is set to its natural context.
- target: Path★ (absolute or relative to its context). Pipes the exported data to the import action of the target component.
- data:
(Async) import (Action)
Imports a value into the date input field. Accepts various date formats and converts them to the appropriate input format.
Options (import)
- action: (= “import”)
- origin: Origin is automatically set to the trigger component that called the action (and cannot be overridden). In case of a manual call, it is set to Null.
- context: Path★ (absolute or relative to its natural context) to the component that is the context of the action. If not provided, the context is set to its natural context.
- target: Path★ (absolute or relative to its context). Pipes the imported data from the export action of the target component, overridding the “data” option if specified.
- data: (Date object, ISO date string, compact date string, or epoch timestamp)
- focus: (boolean, default true)
(Async) clear (Action)
Clears the value of the date input field (sets it to null).
Options (clear)
- action: (= “clear”)
- origin: Origin is automatically set to the trigger component that called the action (and cannot be overridden). In case of a manual call, it is set to Null.
- context: Path★ (absolute or relative to its natural context) to the component that is the context of the action. If not provided, the context is set to its natural context.
(Async) reset (Action)
The reset behavior for this type differs from the generic field contract; see Field defaults and reset.
Options (reset)
- action: (= “reset”)
- origin: Origin is automatically set to the trigger component that called the action (and cannot be overridden). In case of a manual call, it is set to Null.
- context: Path★ (absolute or relative to its natural context) to the component that is the context of the action. If not provided, the context is set to its natural context.