«date» Component Type
📖 Table of Contents
Introduction
The date component type extends the Input component type, providing specialized handling for date input fields with enhanced parsing and formatting capabilities.
- 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
Just add the data-smark attribute to the <input"> element specifying the 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"
}
});
Every example in this section comes with many of the following tabs:
- HTML: HTML source code of the example.
- CSS: CSS applied (if any).
- JS: JavaScript source code of the example.
- Preview: Live, sandboxed rendering of the example — fully isolated from the page styles.
- Notes: Additional notes and insights for better understanding. Don't miss it‼️
✨ In the Preview tab, a JSON playground editor is available with handy buttons:
⬇️ Exportto export the form data to the JSON playground editor.⬆️ Importto import data from the JSON playground editor into the form.♻️ Resetto reset the form to its default values.❌ Clearto clear the whole form.
💡 The JSON playground editor is part of the SmarkForm form itself — it is just omitted from the code snippets to keep the examples focused on what matters.
🛠️ Between the tab labels and the content there is always an edit toolbar:
✏️ Edit— activates edit mode: each source tab turns into a syntax-highlighted code editor (powered by Ace) pre-filled with the full, merged source. Changes are sandboxed — the original example is not affected.📋 Include editor— (only visible in edit mode) controls whether the JSON playground editor is included in the preview. When toggled, the HTML and JS editors update instantly so you can see exactly what code is needed to add or remove it.▶️ Run— (only visible in edit mode) re-renders the Preview from the current editor contents and switches to the Preview tab.
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:
new Date("2023-12-25")(JavaScript Date instance) - Epoch Timestamp:
1703462400000(milliseconds since Unix epoch) - Empty/Null Values:
null,"", orundefined(clears the field)
All valid dates are normalized to ISO date format (YYYY-MM-DD) for export, ensuring consistency across your application.
Requirements
The date component will throw an error if the target field is not an INPUT element or its type is explicitly defined and different to “date”. Invalid date strings that cannot be parsed will result in a null value rather than throwing an error.
<div id="myForm">
<input type="text" name="birthdate" data-smark='{"type":"date"}'>
</div>
const myForm = new SmarkForm(document.getElementById("myForm"));
Every example in this section comes with many of the following tabs:
- HTML: HTML source code of the example.
- CSS: CSS applied (if any).
- JS: JavaScript source code of the example.
- Preview: Live, sandboxed rendering of the example — fully isolated from the page styles.
- Notes: Additional notes and insights for better understanding. Don't miss it‼️
✨ In the Preview tab, a JSON playground editor is available with handy buttons:
⬇️ Exportto export the form data to the JSON playground editor.⬆️ Importto import data from the JSON playground editor into the form.♻️ Resetto reset the form to its default values.❌ Clearto clear the whole form.
💡 The JSON playground editor is part of the SmarkForm form itself — it is just omitted from the code snippets to keep the examples focused on what matters.
🛠️ Between the tab labels and the content there is always an edit toolbar:
✏️ Edit— activates edit mode: each source tab turns into a syntax-highlighted code editor (powered by Ace) pre-filled with the full, merged source. Changes are sandboxed — the original example is not affected.📋 Include editor— (only visible in edit mode) controls whether the JSON playground editor is included in the preview. When toggled, the HTML and JS editors update instantly so you can see exactly what code is needed to add or remove it.▶️ Run— (only visible in edit mode) re-renders the Preview from the current editor contents and switches to the Preview tab.
When a SmarkForm component fails to render due to a RenderError, it is replaced by a flamboyant placeholder that shows the error code and provides a handy button to “replay” it to the console making it easier to debug the issue.
👉 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)
Reverts the date field to its configured default value. If no default was configured, the field reverts to null (same as clear).
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.