DecimalRangeField
Source- RangeField<string,string>
- DecimalRangeField
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.
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.
Constructor
Arguments:
Argument | Type | Description | |
---|---|---|---|
props An object with the properties below | |||
props.blank | boolean | 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 | |
props.blankAsNull | boolean | 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 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.defaultValue | null|ValueT| | Default value for this field. This can either be a function that returns a value or the value directly. | |
props.formatterProps | Record | 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.
| |
props.helpText | string | Optional help text for this field that might be shown on a form This rendered by FormItem under the field widget. | |
props.label | string | 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.readOnly | boolean | 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
| |
props.widgetProps | Record | 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
| |
props.writeOnly | boolean | 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 |
Methods
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:
Argument | Type | Description | |
---|---|---|---|
* | viewModel | ViewModelConstructor | The |
Format the value for displaying in a form widget. eg. This could convert a Date
into
a localized date string
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.
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.
Should two values be considered equal?
This is used when determining if two records are equal (see viewModelFactory.isEqual)
Arguments:
Argument | Type | Description | |
---|---|---|---|
value1 | The value to compare | ||
value2 | The other value to compare against |
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.
An object with these properties: | |||
---|---|---|---|
Property | Type | Description | |
* | lower | T | |
* | upper | T |
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.
An object with these properties: | |||
---|---|---|---|
Property | Type | Description | |
* | lower | T | |
* | upper | T |
Convert value to plain JS representation useful for things like passing to a form or posting to a backend API
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).
Properties
asyncChoices
SourceAsync choices for this field.
This can be used with SelectAsyncChoicesWidget or useAsyncChoices to retrieve the choices.
blank
SourceIs 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
SourceIf 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
SourceWhen 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.
choices
SourceformatterProps
SourceAny props for components that use this field that were passed through to the Field
. Don't access this directly -
call getFormatterProps
instead.
helpText
SourceHelp text that can be displayed with the form widget
This rendered by FormItem under the field widget.
isBound
SourceReturns 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
SourceLabel 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
SourceThe ViewModel class this field is attached to.
This will throw an error if the field is not attached to a model.
name
SourceThe 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
SourceIndicates 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
SourceWhen 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
SourceAny props for components that use this field that were passed through to the Field
. Don't access this directly -
call getWidgetProps
instead.
writeOnly
SourceIndicates 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
SourceField 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.