Number Unit Field¶
A number input beside a unit select, such as 512 MB or 2 GB. The number is checked against min and max, and a unit may carry a factor so the field can also report and take a base value.
Decision¶
| When | Use |
|---|---|
| Sizes, durations, and limits that the user thinks of in different units | Number Unit Field |
| A value with a small range | Slider Field |
Usage¶
import { createNumberUnitField } from './configurationpage?name=myplugin_stolon.js';
var limit = createNumberUnitField({ units: [{ value: 'MB', factor: 1 }, { value: 'GB', factor: 1024 }], min: 1, max: 4096 });
limit.setBaseValue(config.LimitMb);
config.LimitMb = limit.getValue().base;
Classes: stolon-number-unit, stolon-number-unit-row. The handle is { element, input, select, getValue, setValue, setBaseValue, isValid, setSize, destroy }.
States and Modifiers¶
unitsis required, an array of strings or of{ value, label, factor }. The factor is how many base units one of this unit holds.getValue()returns{ value, unit, base }.valueis null while the input is empty, andbaseis the value times the unit's factor, or null with no factor.setBaseValuepicks the largest unit that divides the base evenly.- A number outside
minandmaxshows an inline error through the validation core when the field is left, and setsaria-invalid. The messages are "Enter a number from {min} to {max}.", "Enter {min} or more.", and "Enter {max} or less.", andrangeText,minText, andmaxTextreplace them. - A
stepthe caller sets is a rule too, sostep: 0.5refuses 0.25. With nostepthe arrows step by 1 and any typed number is allowed.validate({ value, unit, base })runs after the range. - A
minabovemaxswaps the two.unitsets the first unit, the first inunitsby default. - The labels default to "Value" and "Unit".
sizeissmall,medium(default), orlarge, and sizes its fields on the size scale.setSize(size)changes it later.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createNumberUnitField¶
function createNumberUnitField(options: NumberUnitFieldOptions): NumberUnitFieldHandle
A number input beside a unit select, such as 512 MB or 2 GB. The value is checked against min and max through the validation core, and a unit may carry a factor so the field can also report and take a base value.
| Option | Type | What it does |
|---|---|---|
value |
number | string | null |
first number [optional] |
unit |
string |
first unit value (default the first unit) |
units (required) |
Array<string | NumberUnit> |
array of strings, or of { value, label, factor } where factor is how many base units one of this unit holds [required] |
min |
number | string |
lowest number allowed, and a min above max swaps the two [optional] |
max |
number | string |
highest number allowed [optional] |
step |
number |
step of the number input, and a given step is also a rule, so 0.5 refuses 0.25 (default 1, which the arrows use and typing may leave) |
valueLabel |
string |
aria-label of the number input (default 'Value') |
unitLabel |
string |
aria-label of the unit select (default 'Unit') |
rangeText |
string |
message for a number out of range, where {min} and {max} are filled in (default 'Enter a number 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 |
(value: NumberUnitValue): string | null | undefined |
function({ value, unit, base }) returning an error sentence, or an empty value when valid, after the range passes [optional] |
onChange |
(value: NumberUnitValue): void |
function({ value, unit, base }) [optional] |
| Handle member | Type |
|---|---|
input |
HTMLInputElement |
select |
HTMLSelectElement |
getValue |
(): NumberUnitValue |
setValue |
(next?: { value?: number | string | null; unit?: string; }): void |
setBaseValue |
(base: number): void |
isValid |
(): boolean |
setSize |
(size?: 'small' | 'medium' | 'large'): void |
destroy |
(): void |
| Type | What it is |
|---|---|
NumberUnit |
One unit choice. factor is how many base units one of this unit holds. |
NumberUnitValue |
What getValue returns. value is null while the input is empty, and base is null with no factor. |
| Class | What it looks like |
|---|---|
stolon-number-unit-row |
A number input and a unit select side by side, with a message under them |
Example¶
Example Code¶
<!-- title: Number Unit Field -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
mount.appendChild(base.createNumberUnitField({ units: [{ value: 'MB', factor: 1 }, { value: 'GB', factor: 1024 }], value: 512, unit: 'MB', min: 1, max: 4096 }).element);
</script>