«time» Component Type

📖 Table of Contents

Introduction

The time component type extends the Input component type, providing specialized handling for time input fields with enhanced parsing and formatting capabilities.

  • Imports and Exports: ISO time string (HH:mm:ss) or null (when empty)
  • Singleton Pattern: Implements the Singleton Pattern
  • Data Conversion: Converts between various time formats and ISO time strings

Usage

Just add the data-smark attribute to the <input> element specifying the type:

<input data-smark='{"type":"time", "name":"meetingTime"}'>

Alternatively, you can also use the shorthand notation inferring the type from the <input> element type:

<input type="time" name="meetingTime" data-smark>

Example:

🔗
🗒️ HTML
⚙️ JS
👁️ Preview
<div id="myForm">
<input type="time" name="meetingTime" data-smark>
</div>
const myForm = new SmarkForm(document.getElementById("myForm"), {
    value: { "meetingTime": "14:30:00" }
});

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: This is where you can see the code in action.
  • Notes: Additional notes and insights for better understanding. Don't miss it‼️

✨ Additionally, in the Preview tab, you will find handy buttons:

  • ⬇️ Export to export the form data to the JSON data viewer/editor.
  • ⬆️ Import to import data into the form from the JSON data viewer/editor.
  • ♻️ Reset to reset the form to its default values.
  • ❌ Clear to reset the form to its initial state.

Data Formats

The time component accepts multiple input formats for maximum flexibility:

  • ISO Time String (HH:mm:ss): "14:30:45" (standard format with seconds)
  • Short Time String (HH:mm): "14:30" (automatically adds :00 for seconds)
  • Compact Time String (HHmmss): "143045" (6-digit format, converted to HH:mm:ss)
  • Short Compact Time String (HHmm): "1430" (4-digit format, converted to HH:mm:00)
  • Date Object: new Date("2023-12-25T14:30:45") (extracts time portion)
  • Epoch Timestamp: 1703512245000 (milliseconds since Unix epoch, extracts time)
  • Empty/Null Values: null, "", or undefined (clears the field)

All valid times are normalized to ISO time format (HH:mm:ss) for export, ensuring consistency across your application.

Requirements

The time component will throw an error if the target field is not an INPUT element or its type is explicitly defined and different to “time”. Invalid time strings that cannot be parsed will result in a null value rather than throwing an error.

🔗
🗒️ HTML
⚙️ JS
👁️ Preview
<div id="myForm">
<input type="text" name="meetingTime" data-smark='{"type":"time"}'>
</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: This is where you can see the code in action.
  • Notes: Additional notes and insights for better understanding. Don't miss it‼️

✨ Additionally, in the Preview tab, you will find handy buttons:

  • ⬇️ Export to export the form data to the JSON data viewer/editor.
  • ⬆️ Import to import data into the form from the JSON data viewer/editor.
  • ♻️ Reset to reset the form to its default values.
  • ❌ Clear to reset the form to its initial state.

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 options object as an argument, which contains the necessary information to perform the action.

👉 When called from triggers, the properties defined in their data-smark object, are passed as options to the action. Those expecting a path(★ ) to other components are resolved to the actual component.

The time component type supports the following actions:

(Async) export (Action)

Exports the value of the time input field as an ISO time string. If the field is empty or contains an invalid time, 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 time input field. Accepts various time 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 time string, compact time string, or epoch timestamp)
  • focus: (boolean, default true)

(Async) clear (Action)

Clears the value of the time 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 time 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.