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

PropTypeDefaultDescription
static FORM_TYPEstringfieldThe type of the Field.
static FIELD_TYPE_STRINGstringstringIndicates a string field in the form definition.
static FIELD_TYPE_NUMBERstringnumberIndicates a number field in the form definition.
static FIELD_TYPE_BOOLEANstringbooleanIndicates a boolean field in the form definition.
static FIELD_TYPE_DATEstringdateIndicates a date field in the form definition.
formType read onlystringField.FORM_TYPEThe form type of this field.
type read onlyFieldTypestringThe type of the field.
checked read onlybooleanfalseIdentifies 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 onlyFieldValuenullThe value when the field has been reset..
valueFieldValuenullThe typed value representation. The value is always null if the current element is invalid.
value will be set asynchronously after validating raw
formatted read onlystring | nullnullThe formatted value represented for the format option is provided in the schema.
formatted will be set asynchronously after validating raw
rawstring''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 onlybooleanfalseIdentifies 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;

Inherited Methods

From class Element

From class Evento

Edit this page on GitHub Updated at Fri, Feb 4, 2022