CxJS

NumberField

import { NumberField } from 'cx/widgets'; Copied

The NumberField widget is used for numeric inputs. It supports formatting for currencies, percentages, and decimal places, with built-in validation for min/max values.

<LabelsTopLayout columns={2}>
  <NumberField label="Standard" value={bind(m.number, 1234.5)} autoFocus />
  <NumberField label="Disabled" value={m.number} disabled />
  <NumberField label="Read-only" value={m.number} readOnly />
  <NumberField
    label="Placeholder"
    value={m.placeholder}
    placeholder="Enter a number..."
  />
  <NumberField label="Required" value={m.number} required />
  <NumberField
    label="Min/Max (0-100)"
    value={m.number}
    minValue={0}
    maxValue={100}
  />
  <NumberField
    label="Wheel"
    value={m.number}
    step={10}
    reactOn="enter blur wheel"
  />
</LabelsTopLayout>

Formatting

NumberField supports various number formats including currencies and percentages. Use scale to convert between display and stored values (e.g., 0.01 for percentages).

<LabelsTopLayout columns={2}>
  <NumberField
    label="Currency (EUR)"
    value={bind(m.price, 1234.5)}
    format="currency;EUR"
  />
  <NumberField label="Currency (USD)" value={m.price} format="currency;USD" />
  <NumberField
    label="Percentage"
    value={bind(m.percent, 0.25)}
    format="p;0"
    scale={0.01}
  />
  <NumberField
    label="Two Decimals"
    value={bind(m.quantity, 42.5)}
    format="n;2"
  />
</LabelsTopLayout>

Configuration

Core Properties

PropertyTypeDefaultDescription
valuenumbernullNumeric value
formatstring"n"Display format. See Formatting
scalenumber1Multiplier for converting between stored and displayed value
offsetnumber0Offset added after scaling
placeholderstringHint text displayed when the field is empty
disabledbooleanfalseDisables the input
enabledbooleanOpposite of disabled
readOnlybooleanfalseMakes the input read-only
requiredbooleanfalseMarks the field as required
labelstringField label text
viewModebooleanfalseDisplay as plain text instead of interactive input
emptyTextstringText shown in view mode when the field is empty

Value Constraints

PropertyTypeDefaultDescription
minValuenumberMinimum allowed value
minExclusivebooleanfalseIf true, the minimum value itself is not allowed
maxValuenumberMaximum allowed value
maxExclusivebooleanfalseIf true, the maximum value itself is not allowed
constrainbooleanfalseIf true, automatically clamps value to min/max

Increment Behavior

PropertyTypeDefaultDescription
increment / stepnumberStep size for arrow keys and mouse wheel
incrementPercentagenumber0.1Auto-calculated increment as percentage of current value
snapToIncrementbooleantrueIf true, values snap to increment multiples on wheel change

Add wheel to reactOn to change values using the mouse wheel. When enabled, page scrolling is disabled while the field is focused.

Validation

PropertyTypeDefaultDescription
minValueErrorTextstring"Enter {0} or more."Error message when value is below minimum
minExclusiveErrorTextstring"Enter a number greater than {0}."Error message when value equals exclusive minimum
maxValueErrorTextstring"Enter {0} or less."Error message when value is above maximum
maxExclusiveErrorTextstring"Enter a number less than {0}."Error message when value equals exclusive maximum
inputErrorTextstring"Invalid number entered."Error message for invalid number input
errorstringCustom error message. Field is marked invalid if set
visitedbooleanIf true, shows validation errors immediately
validationModestring"tooltip"How to show errors: "tooltip", "help", "help-block"

Appearance

PropertyTypeDefaultDescription
iconstring/objectIcon on the left side
showClearbooleanfalseShows a clear button when the field has a value
hideClearbooleanfalseOpposite of showClear
alwaysShowClearbooleanfalseShows clear button even when field is required
tooltipstring/objectTooltip text or configuration
inputStylestring/objectCSS styles applied to the input element
inputClassstringCSS class applied to the input element

Behavior

PropertyTypeDefaultDescription
autoFocusbooleanfalseAutomatically focuses the field on mount
reactOnstring"enter blur"Events that trigger value updates. Options: "change", "enter", "blur", "wheel". Separate with space
trackFocusbooleanfalseAdds cxs-focus CSS class when input is focused
inputTypestring"text"HTML input type
onParseInputfunctionCustom parser for text input

Callbacks

PropertyDescription
onValidate(value, instance, validationParams) => string - Return error message to validate