Skip to content

Duration Field

Hours, minutes, and seconds number boxes on one line, each with Stolon's up and down arrows and its unit inside the box, like the Color Picker's percent. It is built on the Field Adornments number box and checks through the validation core.

Decision

When Use
A length of time a plugin stores in seconds, such as a timeout or an interval Duration Field
A size or a count with a unit to choose Number Unit Field
A time of day A time input, a Time Range Field, or a Schedule Picker

Usage

import { createDurationField } from './configurationpage?name=myplugin_stolon.js';

var timeout = createDurationField({ value: config.TimeoutSeconds, units: ['m', 's'], min: 5, max: 3600 });
view.querySelector('#timeoutHost').appendChild(timeout.element);

Classes: stolon-duration, stolon-duration-row. The handle is { element, inputs, getValue, setValue, isValid, setSize, destroy }, and getValue() returns the duration in seconds, or null while every box is empty.

States and Modifiers

  • units picks the boxes, largest first, such as ['h', 'm'] (default ['h', 'm', 's']). unitText replaces the unit inside a box and unitLabels its name, "Hours", "Minutes", and "Seconds" by default.
  • The largest box holds any count, and each smaller one 0 to 59. setValue rounds to the smallest unit shown.
  • min and max are in seconds, and their message names them as durations, such as "Enter a duration from 1m to 2h.". rangeText, minText, and maxText replace it.
  • required and validate(seconds) check through the core. One message under the boxes speaks for all of them.
  • durationText(seconds, units, text) writes a duration the way the messages do, such as 5400 as "1h 30m". The bag's formatDuration writes a clock style time such as "1:30" instead.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

durationText

function durationText(seconds: number, units?: DurationUnit[], text?: Partial<Record<DurationUnit, string>>): string

Writes seconds as a short duration in the given units, such as 5400 as '1h 30m'. The smallest unit takes what is left, rounded.

createDurationField

function createDurationField(options?: DurationFieldOptions): DurationFieldHandle

Hours, minutes, and seconds number boxes on one line, each with Stolon's up and down arrows and its unit inside the box, like the color picker's percent. The value is in seconds. Each box and the whole duration check through the validation core, so a minute of 75 or a duration past max shows the message under the boxes.

Option Type What it does
value number | null the duration in seconds [optional]
units DurationUnit[] the boxes to show, such as ['h', 'm'] (default ['h', 'm', 's'])
unitText Partial<Record<DurationUnit, string>> the unit shown inside each box, by unit (default 'h', 'm', and 's')
unitLabels Partial<Record<DurationUnit, string>> the aria-label of each box, by unit (default 'Hours', 'Minutes', and 'Seconds')
min number the shortest duration allowed, in seconds [optional]
max number the longest duration allowed, in seconds [optional]
required boolean true requires a duration (default false)
rangeText string message for a duration outside min and max, with {min} and {max} as durations (default 'Enter a duration from {min} to {max}.')
minText string message when only min is set (default 'Enter {min} or more.')
maxText string message when only max is set (default 'Enter {max} or less.')
size 'small' | 'medium' | 'large' 'small', 'medium', or 'large' for its fields (default 'medium')
validate (seconds: number | null): string | null | undefined function(seconds) returning an error sentence, or an empty value when valid, after the range passes [optional]
onChange (seconds: number | null): void function(seconds) after each change, with null while every box is empty [optional]
Handle member Type
inputs HTMLInputElement[]
getValue (): number | null
setValue (seconds: number | null | undefined): void
isValid (): boolean
setSize (size?: 'small' | 'medium' | 'large'): void
destroy (): void
Type What it is
DurationUnit One unit of a duration: hours, minutes, or seconds.
Class What it looks like
stolon-duration Hours, minutes, and seconds number boxes on one line, each with its unit inside before its arrows, and the message under them

Example

Example Code

<!-- title: Duration Field -->
<div class="stolon-field"><label>Scan Interval</label><div id="interval"></div></div>
<div class="stolon-field" style="margin-top: 16px;"><label>Request Timeout</label><div id="timeout"></div></div>
<script type="module">
const base = await window.__baseReady;
document.getElementById('interval').appendChild(base.createDurationField({ value: 5400, units: ['h', 'm'], min: 900 }).element);
const timeout = base.createDurationField({ value: 7500, units: ['m', 's'], min: 5, max: 3600 });
document.getElementById('timeout').appendChild(timeout.element);
timeout.isValid();
</script>