DecimalRangeField

Source
import { DecimalRangeField } from "@prestojs/viewmodel";
Hierarchy

A field that represents a date range. See RangeField for details about ranges generally.

Range values are always assumed to be inclusive of both ends of the range. If your backend returns a range exclusive of either end you will need to convert it first.

Usage

To use, instantiate the class with any of the Field props you want to customize:

new DecimalRangeField({ label: "Price Range" })

Pass boundsFieldProps to pass through extra props to the DecimalField used for each value in the range:

new DecimalRangeField({
  boundsFieldsProps: {
    formatterProps: {
      locales: ["en-AU"],
      localeOptions: { style: "currency", currency: "AUD" },
    },
  },
})

See the examples below for usage with widgets & formatters.

Examples

Default widget in a Form

This example shows the default widget that will be used in a Form when using @prestojs/ui-antd. See getWidgetForField.

The default widget is DecimalRangeWidget.

You can pass options for the widget via the DecimalField under the boundsFieldProps.widgetProps option.

Default formatter for DecimalRangeField

This example shows the default formatter that will be used with FieldFormatter.

See getFormatterForField for how a formatter is selected for a field.

The default formatter for DecimalRangeField is RangeFormatter.

You can pass options for the formatter via the DecimalField under the boundsFieldProps.formatterProps option.

API

Constructor

new DecimalRangeField(?props)
Source

Arguments:

ArgumentTypeDescription
props
An object with the properties below
props.blankboolean

Is this field allowed to be assigned a blank (null, undefined, "") value?

This isn't currently used by anything in PrestoJS but is useful if you are creating generic validators for form values.

Defaults to false

props.blankAsNullboolean

Frontend values are often stored as strings even if they are not stored like that in a backend (eg. database). Depending on your backend implementation it may expect empty values to be represented as null rather than an empty string. Setting blankAsNull to true indicates that empty strings should be converted to null when being sent to the backend.

This isn't currently used by anything inPrestoJS but is useful if you are writing generic data transformations.

props.boundsFieldProps

Any extra props to pass through to the DecimalField used for each value in the range.

props.defaultValuenull|ValueT|

Default value for this field. This can either be a function that returns a value or the value directly.

props.formatterPropsRecord

Any arbitrary props that should be passed to formatter components

These props are included in Field.getFormatterProps and are passed to components by getFormatterForField.

It's up to the component implementations to make use of them.

new BooleanField({
  formatterProps: { trueLabel: "✅", falseLabel: "❌" },
})

props.helpTextstring

Optional help text for this field that might be shown on a form

This rendered by FormItem under the field widget.

props.labelstring

Label for this field. If not specified will be generated from the name.

This is rendered by FormItem as the label for a form input.

props.readOnlyboolean

True if field should be considered read only (eg. excluded from forms)

This isn't used by anything in PrestoJS but is useful if generating forms from a ViewModel (eg. you could exclude readOnly fields from a form).

props.widgetPropsRecord

Any arbitrary props that should be passed to widget components

These props are included in Field.getWidgetProps and are passed to components by getWidgetForField.

It's up to the component implementations to make use of them. By default the @prestojs/ui-antd components should support any of the underlying antd component props via this option.

new Field({
  widgetProps: { placeholder: "Enter you name" },
})

props.writeOnlyboolean

True if field should be considered write only (eg. excluded from detail views)

This isn't used by anything in PrestoJS but is useful if rendering values from ViewModel generically (eg. you could exclude writeOnly fields from display).

Methods

clone()
Source

Returns a clone of the field that should be functionally equivalent

Returns:Field
contributeToClass(viewModel)
Source

Called once after fields are attached to a ViewModel. This occurs the first time .fields is accessed on the ViewModel.

By default this does nothing but can be used by fields to attach extra properties or validate against the final view model (for example checking that another field does / does not exist). For example RelatedViewModelField uses this to validate the sourceFieldName field exists.

This is called by viewModelFactory when the ViewModel class is created in the order that fields are defined.

NOTE: This is called for every distinct ViewModel class; so if class A is extended by class B then it will be called on both A and B.

You would never call this function directly but might implement it if defining a custom Field

Arguments:

ArgumentTypeDescription
*viewModelViewModelConstructor

The ViewModel class the field is attached to

Returns:void
format(value)
Source

Format the value for displaying in a form widget. eg. This could convert a Date into a localized date string

Arguments:

ArgumentTypeDescription
*value

The value from the ViewModel to format for use in a widget.

Returns:any
getFormatterProps()
Source

Return any props that may be used form formatters created for this field.

The default implementation just returns the optional formatterProps Field option but specific field implementations may return additional props.

getFormatterForField will call this method in order to generate the props that should be passed to the formatter.

Returns:{[fieldName: string]: any }
getWidgetProps()
Source

Return any props that may be used form widgets created for this field.

The default implementation just returns the optional widgetProps Field option but specific field implementations may return additional props.

getWidgetForField will call this method in order to generate the props that should be passed to the widget.

Returns:{[fieldName: string]: any }
isEqual(?value1,?value2)
Source

Should two values be considered equal?

This is used when determining if two records are equal (see viewModelFactory.isEqual)

Arguments:

ArgumentTypeDescription
value1

The value to compare

value2

The other value to compare against value

Returns:boolean
normalize(value)
Source

Normalize a value passed into a ViewModel constructor. This could do things like parse a date string to a Date.

This implementation will often match parse which performs a similar function but for values received from a form input. In general normalize should throw on invalid input whereas parse should not.

Arguments:

ArgumentTypeDescription
*value

The value to normalize

Returns:null|
An object with these properties:
PropertyTypeDescription
*lowerT
*upperT
parse(value)
Source

Parse a value received from a form widget onChange call. eg. This could convert a localized date string into a Date.

This implementation will often match normalize which performs a similar function but processes the values received by the ViewModel. In general parse should not throw on invalid input (eg. user could be part way through entering a value) whereas normalize should.

Arguments:

ArgumentTypeDescription
*valuenull|

The value received from a form widget

Returns:null|
An object with these properties:
PropertyTypeDescription
*lowerT
*upperT
toJS(value)
Source

Convert value to plain JS representation useful for things like passing to a form or posting to a backend API

Arguments:

ArgumentTypeDescription
*value

The value to convert

Returns:null|string|number|Record
toString()
Source

Return a string representation of this field

By default this will return Field({ name: "<field name>" }) (where Field matches the constructor name of the field class).

Returns:string

Properties

asyncChoices

Source
AsyncChoicesInterface

Async choices for this field.

This can be used with SelectAsyncChoicesWidget or useAsyncChoices to retrieve the choices.

blank

Source
boolean

Is this field required when saving a record?

This isn't currently used by anything in PrestoJS but is useful if you are creating generic validators for form values.

blankAsNull

Source
boolean

If true an empty string value should be converted to a null value

This isn't currently used by anything inPrestoJS but is useful if you are writing generic data transformations (eg. transforming a form submission to send to the server) and need to know whether a value should be forced to null.

boundRecord

Source
undefined|ViewModelInterface

When accessed on a bound field will return the current instance of the ViewModel the field is bound to.

If called on an unbound field then this will always be undefined and a warning will be raised.

boundsField

Source
Field

choices

Source

The choices defined for this field (if any).

When constructing the field you can pass in choices as either a Map of value to label or an array of 2-tuples being the value and label for that value. When retrieved from the field these are always returned as a Map.

defaultValue

Source
undefined|null|ValueT|Promise

Get the default value for this field.

formatterProps

Source
Record

Any props for components that use this field that were passed through to the Field. Don't access this directly - call getFormatterProps instead.

helpText

Source
string

Help text that can be displayed with the form widget

This rendered by FormItem under the field widget.

isBound

Source
boolean

Returns true if field is bound to a ViewModel instance. When a field is bound to a instance the value for that field is accessible on the 'value' property.

label

Source
string

Label that can be displayed as the form label for a widget

If not specified will be generated from name.

This is rendered by FormItem as the label for a form input.

model

Source
ViewModelConstructor

The ViewModel class this field is attached to.

This will throw an error if the field is not attached to a model.

name

Source
string

The name of this field.

This is set automatically when the ViewModel is created based on the object key the field was set on:

// The field here will get a `name` of `id` as that matches the key in the object.
viewModelFactory({ id: new Field() }, { pkFieldName: "id" })

This will throw an error if the field is not attached to a model.

readOnly

Source
boolean

Indicates this field should only be read, not written. Not enforced but can be used by components to adjust their output accordingly (eg. exclude it from a form or show it on a form with a read only input)

value

Source
undefined|ValueT

When isBound is true this will return the current value of this field on the bound ViewModel otherwise it will always be undefined. You can get the bound field using the _f property on the ViewModel.

Most of the time you would get the value for a field directly from the record:

const User = viewModelFactory(
  { id: new Field(), name: new Field() },
  { pkFieldName: "id" }
)
const user = new User({ id: 1, name: "Jo" })
user.name === "Jo"
// true

But if you want to pass the field itself along with the value this is useful (eg. see FieldFormatter):

// FieldFormatter now has access to the `Field` instance and the value
;<FieldFormatter field={user._f.name} />

widgetProps

Source
Record

Any props for components that use this field that were passed through to the Field. Don't access this directly - call getWidgetProps instead.

writeOnly

Source
boolean

Indicates this field should only be written only and is not intended to be read directly. This is not enforced but can be used by components to adjust their output accordingly (eg. exclude it from a detail view on a record)

Static Properties

fieldClassName

Source
string

Field class name

This exists so getWidgetForField and getFormatterForField can select the widget or formatter for a field without needing to import all fields up front. For example the following example avoids importing any code upfront that isn't needed which wouldn't be possible if using instanceof.

if (field.fieldClassName === "ImageField") {
  return React.lazy(
    () => import("./components/ImageWidget")
  )
}

For custom fields this isn't required unless your implementation of getWidgetForField wants to avoid importing the field up front.