Skip to content

Quantity Field

Edit an exact physical quantity and convert compatible display units.

Usage

Length

Enter a length and switch between compatible display units.

Enter a length and switch between compatible display units.

Temperature

Convert a temperature between compatible units without changing its physical quantity.

Convert a temperature between compatible units without changing its physical quantity.

Controlled

Let the parent own the current value and apply accepted changes back to the component.

Let the parent own the current value and apply accepted changes back to the component.

Examples

Calculator

Enter 50-20% and commit the calculated result as 40.

Enter 50-20% and commit the calculated result as 40.

Compound

Parse a compound unit while preserving one canonical quantity.

Parse a compound unit while preserving one canonical quantity.

API

Vue package: @sectile/vue/quantity-field

Components
  • QuantityFieldRoot
  • QuantityFieldInput
  • QuantityFieldUnitSelect
  • QuantityFieldValue

Functions

createStandardQuantityPolicies

ts
function createStandardQuantityPolicies(canonicalUnit: string, unitSystem?: StandardQuantityUnitSystem): QuantityFieldPolicies

Props

QuantityFieldRootProps

as

Element or component rendered for this part.

asChild

Whether to merge this part into its single child instead of rendering a wrapper.

defaultDisplayUnit

Display unit used before the component receives a controlled unit.

defaultValue

Initial value used when the component owns its state.

disabled

Whether interaction is unavailable.

displayUnit

Controlled unit used to present the stored quantity.

label

Accessible name announced for the control.

modelValue

Current value when state is controlled by the parent.

policies

Behavior policies that customize validation, movement, or selection.

readonly

Whether the value can be inspected but not changed.

QuantityFieldInputProps

as

Element or component rendered for this part.

asChild

Whether to merge this part into its single child instead of rendering a wrapper.

form

ID of the native form associated with the control.

name

Name used for native form submission.

required

Whether the control must contain a valid value before submission.

QuantityFieldPartProps

as

Element or component rendered for this part.

asChild

Whether to merge this part into its single child instead of rendering a wrapper.

Slots

QuantityFieldRootSlotProps

disabled

Whether interaction is unavailable.

displayUnit

Unit currently used for presentation.

invalid

Whether the current draft fails validation.

readonly

Whether the value can be inspected but not changed.

text

Formatted text for the current value.

value

Current value exposed by this contract.

Events

QuantityFieldRoot

commit

Emitted when the current draft is committed.

update:displayUnit

Emitted when the requested display unit changes.

update:modelValue

Emitted when the component requests a new controlled value.

Other types

StandardQuantityUnitSystem

ts
type StandardQuantityUnitSystem = 'metric' | 'imperial' | 'all'

QuantityFieldValueChangeHandler

ts
type QuantityFieldValueChangeHandler = (value: QuantityValue | null) => void

QuantityFieldDisplayUnitChangeHandler

ts
type QuantityFieldDisplayUnitChangeHandler = (value: string) => void

QuantityFieldCommitHandler

ts
type QuantityFieldCommitHandler = (details: { value: QuantityValue | null; expression: string; displayUnit: string }) => void

Parts

Shared scope: [data-scope="quantity-field"]. Combine it with a part selector to keep styles local to this component.

PartSelectorRoleExtra attributes
root[data-part="root"]Defines the component boundary and owns its composed parts.
input[data-part="input"]Accepts the editable value or draft.
unit-select[data-part="unit-select"]Chooses the unit applied to the numeric value.
value[data-part="value"]Displays the current committed value.

Keyboard interaction

KeyBehavior
Standard editing keysEdit and select text with the host input conventions.
TabCommit focus movement without replacing native text behavior.

Accessibility

The labeled input exposes the accepted quantity while unit selection and formatted output remain separately identifiable.

Released under the MIT License.