Unit Selector state variables represent an input that allows the user to select the units to be displayed by the unitInput and unitOuput state variables of the same unitClass.
Unit Selector state variables inherit from the SelectorInputSv and support a specific type of options. The option value is the internal unit (e.g. 'kg'), and the text is what the user should see (e.g. "kg." or "Kilograms"). The unit values must be from those defined in Units.
By default, the selector renders as a popup list of units for the user to select. The currently selected unit is highlighted. The selector can also render as a set of radio buttons by specifying the radio attribute. The radio buttons can be horizontal or vertical, and the text labels can be on either side for both orientations by specifying one of the following attributes:
labelToplabelRightlabelBottom (default)labelLeftThe options are not saved when a Unit Selector Input is saved to storage. Only the selector value is saved. It is expected that the app code will reestablish the current unit options and upgrade the saved values as required.
Selector Input state variables maintain a public child state variable that can be referred to using the subname options (e.g., myPage_myUnitSelector_options). The child state variable's value is an array of options objects containing the following properties:
textvalueThe child state variable can be used to determine when the state variable options change (as opposed to the state variable value).
'unitSelector', class: UnitSelSvctl.createPage({
...,
stateVarInfo:[
{
type: 'unitSelector',
options: [{value:<i>valueString</i>, text:<i>textString</i>}, ...]
},
],
});
infoinfo object can contain the following properties, but subclasses of StateVar may have additional properties:info properties.info properties.info properties.options (optional)text (optional)text is omitted, then value coerced to a string is used.value (required) The value of the state variable if this option is selected.
If an element is a number, the displayed text is the number coerced to a string, and the value is the element. If an element is a string, it is used for both the displayed text and value.
Note: The component() method returns a UnitSelSvComponent, a subclass of SelectorInputSvCmp. If the component has both the valueOnly and showUnits attributes, it renders only a <span> containing the unit abbreviation (fmtUnit()) of the current unit; otherwise it renders as a selector.
fmtUnit()fmtUnit()
Returns the unit abbreviation (units.fmtAbbrev) for the currently selected unit (the state variable value). See Units. The method takes no parameters.
A string containing the unit abbreviation.
isUnitCtlAlways true for unitSelector.
A Boolean.
unitClassThe unitClass string for this selector.
A String.
UnitSelSv.currentUnit()UnitSelSv.currentUnit(unitClass);
Return the current unit value from the unitSelector associated with unitClass. Returns a unit option value. This is typically string (e.g., 'kg'). Throws a TypeError if no unitSelector exists for unitClass.
unitClassunitClass string for the unitSelector state variable that controls which unit sibling is current for state variables in this unitClass.An option value.
UnitSelSv.getUnitSelector()UnitSelSv.getUnitSelector(unitClass);
Return the unitSelector state variable that controls unitClass, or undefined if there is none.
unitClassunitClass string for the unitSelector state variable that controls which unit sibling is current for state variables in this unitClass.A UnitSelSv instance or undefined.
UnitSelSv.convertCurrentUnits()UnitSelSv.convertCurrentUnits(value, fromUnits, unitClass);
Return the value converted from fromUnits units to the units selected by the current value of the unitSelecotor state variable for the unitClass.
unitClass string for a unitSelector.A Number.