Field
Class Field
Represents a field in a form.
Constructor
Field(schema: FieldSchema, parent?: Element | null);
Parameters
- schema - an object that define the element.
type FieldType = 'string' | 'number' | 'boolean' | 'date';
type FieldValue = string | number | boolean | Date | null;
type Format = string | ((value: FieldValue) => string)
interface FieldSchema extends ElementSchema {
// 'string' | 'number' | 'boolean' | 'date'
type: FieldType;
// The format string will be used to generate the `formatted`
// property in current element. Currently, only `string` and `date`
// types are supported to use this feature.
format?: Format;
// The value when the field has been reset.
// For `date` type, it's any valid date represented as a string
// based on the current date format.
default?: any;
// The initial value of the field,
// if not provided, `default` will be used.
// For `date` type, it's any valid date represented as a string
// based on the current date format.
value?: any;
// The array of validation rule schemas
rules?: ValidationRuleSchema[];
// Lets you map a string value to a boolean field when it's selected.
// For example, if you are rendering a radio button,
// but want to use a string value instead of `true`.
// The `checkedValue` indicates what is `true` for this button.
checkedValue?: any;
}
- parent - the parent of this element.
Properties
Prop | Type | Default | Description |
---|---|---|---|
static FORM_TYPE | string | field | The type of the Field . |
static FIELD_TYPE_STRING | string | string | Indicates a string field in the form definition. |
static FIELD_TYPE_NUMBER | string | number | Indicates a number field in the form definition. |
static FIELD_TYPE_BOOLEAN | string | boolean | Indicates a boolean field in the form definition. |
static FIELD_TYPE_DATE | string | date | Indicates a date field in the form definition. |
formType read only | string | Field.FORM_TYPE | The form type of this field. |
type read only | FieldType | string | The type of the field. |
checked read only | boolean | false | Identifies if the current selected state of this field is checked . In case of a boolean field the property directly represent the boolean value. In case of a string or number field, the property is true if the current value matched with the value specified as checkedValue . In this way a selected status can be as determined for non-boolean fields. |
default read only | FieldValue | null | The value when the field has been reset.. |
value | FieldValue | null | The typed value representation. The value is always null if the current element is invalid. value will be set asynchronously after validating raw |
formatted read only | string | null | null | The formatted value represented for the format option is provided in the schema. formatted will be set asynchronously after validating raw |
raw | string | '' | The current external string representation of the value in this element. When using in Vue template, raw should be used as a HTML input's value |
pending read only | boolean | false | Identifies if the element is processing asynchronous methods |
Methods
static accept
Internal function to validate the input schema, called when generating the element.
Signatures
accept(schema: any): SchemaValidation;
type SchemaValidation = {
valid: boolean;
reason?: string;
infos?: Record<string, string>;
};
Parameters
- schema - schema object.
static create
This method helps to create new Field
dynamically.
Signatures
create(schema: FieldSchema, parent?: Element | null): Field;
Parameters
- schema -
FieldSchema
object. - parent - the parent of this element.
Returns
Field
instance
setRaw
Set new raw value to the element asynchronously. When calling, it will call the setValue
method automatically.
Signatures
async setRaw(value: any): void;
Parameters
- value - The new value.
Returns
- the typed value of this element.
setValue
Set new value to the element asynchronously. When calling, if the current value is different from new value, this method will first validate
the new value, if the value is valid
, new typed value will be assigned to this element, otherewise the element's value is null
. Finally, a changed
event will be triggered.
Signatures
async setValue(value: any): FieldValue;
Parameters
- value - The new value.
Returns
- the typed value of this element.
setCheckedValue
Set the checkedValue
to the element. In case of a boolean
field the property directly represent the boolean value. In case of a string
or number
field, the property is true
if the current value matched with the value specified as checkedValue
. In this way a selected status can be as determined for non-boolean fields.
Signatures
setCheckedValue(checkedValue: any): void;
Parameters
- checkedValue - The value want to be set to trigger the
checked = true
.
reset
Reset the element to the default state. When calling, this method will first set the raw
to the default
value, clean up the validation messages, and then reset the validation
of the element.
Signatures
reset(): void;
clear
This method simply cleans up all valiadtion messages, then force to set the element's value to an empty string.
Signatures
clear(): void;
validate
Validate the element with the current raw
value asynchronously. Firsly, it will trigger the validate
event, then try to cast the raw
value to the typed value, this value will be sent to the validation
, if all the validations are valid, new typed value will be assigned otherwise will be null
, the new formatted
value also be evaluated. Finally, an validated
event will be triggered.
Signatures
validate(): void;