File

packages/core/src/lib/form/form.ts

Description

The Form class is an Item with additional info about its properties (Fields).

Extends

Item

Index

Properties
Methods

Constructor

constructor(body: T, config?: FormConfig)

The constructor will populate the fields array. If config.fields is set only the configured fields will be created. If not, all properties of the given body will be used as fields.

Parameters :
Name Type Optional
body T No
config FormConfig<T> Yes

Properties

Public config
Type : FormConfig<T>

The configuration of the form. It is an extension of ItemConfig.

Public fields
Type : Field[]

Array of fields. It will be populated automatically when the form is constructed.

Public body
Type : T
Inherited from Item
Defined in Item:6

The value body of the item. This can be either a primitive value or an Object.

Public config
Type : ItemConfig<T>
Inherited from Item
Defined in Item:8

The config of the item.

Methods

createField
createField(property: string, config: FieldConfigProperty)

creates and adds a single field to the form

Parameters :
Name Type Optional
property string No
config FieldConfigProperty No
Returns : Field | undefined
getField
getField(property: string)

returns the field instance of the given property

Parameters :
Name Type Optional
property string No
Returns : any
getValue
getValue(property: string)

Returns the original value of the property, if any.

Parameters :
Name Type Optional
property string No
Returns : any
isCreating
isCreating()

Returns true if the form is currently in create mode (has not a body set)

Returns : boolean
isEditing
isEditing()

Returns true if the form is currently in edit mode (has a body set)

Returns : boolean
action
action(property, e)
Inherited from Item
Defined in Item:240

Action method that is meant to be called on a button click or similar. Calls the config#action method with the item and the property name

Parameters :
Name Optional
property No
e No
Returns : void
classes
classes(property?: string)
Inherited from Item
Defined in Item:140

If no property given: Returns the output of the config.classes method or ''. If property given: Returns the output of the config.fields[property].classes method or ''

Parameters :
Name Type Optional
property string Yes
Returns : string
clear
clear()
Inherited from Item
Defined in Item:47

deletes the item body

Returns : void
deleteImmutableProperties
deleteImmutableProperties(value: Object)
Inherited from Item
Defined in Item:191
Parameters :
Name Type Optional Default value
value Object No this.body
Returns : void
display
display(property?: string)
Inherited from Item
Defined in Item:152

Returns the output of the config.display transformation function with the given property value. If no display function is set, it will just return the property value.

Parameters :
Name Type Optional
property string Yes
Returns : any
Protected generateConfig
generateConfig()
Inherited from Item
Defined in Item:17

Generates a config from the body by setting view to the properties type.

Returns : ItemConfig<T>
getBody
getBody()
Inherited from Item
Defined in Item:37

Returns the item's body

Returns : T
getConfig
getConfig()
Inherited from Item
Defined in Item:57

Returns the item's config

Returns : any
getProperties
getProperties()
Inherited from Item
Defined in Item:62

Returns an Array of properties possessed by the body.

Returns : Array<string>
group
group(property: string)
Inherited from Item
Defined in Item:134

Returns the output of the config.group transformation function with the given property value. If no group function is set, it will just return the property value.

Parameters :
Name Type Optional
property string No
Returns : any
hasBody
hasBody()
Inherited from Item
Defined in Item:42

Returns true if the body is defined and not null

Returns : boolean
id
id()
Inherited from Item
Defined in Item:73

Returns the value of the the Item's identifier property.

Returns : any
isImmutableProperty
isImmutableProperty(property: string)
Inherited from Item
Defined in Item:179
Parameters :
Name Type Optional
property string No
Returns : boolean
pickWriteOnly
pickWriteOnly(value)
Inherited from Item
Defined in Item:165

Returns value with all readOnly properties removed

Parameters :
Name Optional Default value
value No this.body
Returns : any
resolve
resolve(property?: string)
Inherited from Item
Defined in Item:82

Returns either the whole body (if no property is given) or the value of the given property. This method will traverse the body via the config.resolve function (if given).

Parameters :
Name Type Optional
property string Yes
Returns : any
resolvePath
resolvePath(path: string)
Inherited from Item
Defined in Item:109

Resolves the given path on the item object. e.g. "value.config.usePassword" will resolve that object path, if existing.

Parameters :
Name Type Optional
path string No
Returns : any
save
save(value: T)
Inherited from Item
Defined in Item:222

Saves the given value. Run serializers before assigning the new value.

Parameters :
Name Type Optional Default value
value T No this.body
Returns : Promise<Item<T>>
serialize
serialize(value, put: boolean)
Inherited from Item
Defined in Item:200

Transforms the given field's value for serialization when saving.

Parameters :
Name Type Optional Default value
value No
put boolean No false
Returns : any
sort
sort(property: string)
Inherited from Item
Defined in Item:160

Transforms the given field's value for sorting

Parameters :
Name Type Optional
property string No
Returns : any
Public transform
transform(action: string, property: string, value: any, defaultValue: any)
Inherited from Item
Defined in Item:117

The main method for transformation functions like resolve, display and group. If you dont set the third parameter, the current item value will be used. The third parameter can be used to transform a value that is not yet possesed (e.g. to serialize)

Parameters :
Name Type Optional Default value
action string No
property string No
value any No this.resolve(property)
defaultValue any No this.resolve(property)
Returns : any
useConfig
useConfig(config: ItemConfig)
Inherited from Item
Defined in Item:52

Assigns the given config to the existing via Object.assign

Parameters :
Name Type Optional
config ItemConfig<T> No
Returns : void

Form

Form is an Item with extra functions:

  • instantiates fields to iterate over
  • getters for fields and property values, including prefills.
const tommy = new Form({ name: 'tommy' }); //init without config
tommy.getValue('name')); //'tommy'
const bobby = new Form(null, { fields: { name: { prefill:'bobby' }}}); //init with config only
tommy.getValue('name')); //'bobby'

import { FieldConfigProperty } from '../config/field-config-property.interface';
import { Field } from '../field/field';
import { Item } from '../item/item';
import { FormConfig } from './form-config.interface';

/** The Form class is an Item with additional info about its properties (Fields). */
export class Form<T> extends Item<T> {
  /** Array of fields. It will be populated automatically when the form is constructed. */
  public fields: Field[];
  /** The configuration of the form. It is an extension of ItemConfig. */
  public config: FormConfig<T>;

  /** The constructor will populate the fields array.
   * If config.fields is set only the configured fields will be created.
   * If not, all properties of the given body will be used as fields. */
  constructor(body: T, config?: FormConfig<T>) {
    super(body, config);
    this.fields = [];
    if (!this.config || !this.config.fields) {
      return;
    }
    Object.keys(this.config.fields).forEach((property) => {
      this.fields.push(new Field(property, this.config.fields[property]));
    });
  }

  /** creates and adds a single field to the form */
  createField(property: string, config: FieldConfigProperty): Field | undefined {
    if (!config) {
      return;
    }
    if (!property) {
      return;
    }
    if (this.config.fields[property]) {
      console.error('cannot create field "', property, '". Property name already taken.');
      return;
    }
    this.config.fields[property] = config;
    const field = new Field(property, this.config.fields[property]);
    this.fields = this.fields.concat([field]);
    return field;
  }

  /** returns the field instance of the given property */
  getField(property: string) {
    return this.fields.find((field) => field.property === property);
  }

  /** Returns the original value of the property, if any. */
  getValue(property: string) {
    if (!this.body && this.config.fields && this.config.fields[property]) {
      // If the prefill is not a primitive, return a clone to stay pristine
      if (Array.isArray(this.config.fields[property].prefill)) {
        return this.config.fields[property].prefill.slice(0);
      } else if (typeof this.config.fields[property].prefill === 'object') {
        return Object.assign({}, this.config.fields[property].prefill);
      }
      // if no body is present, the prefills are used
      return this.config.fields[property].prefill;
    } else {
      return this.resolve(property);
    }
  }

  /** Returns true if the form is currently in edit mode (has a body set) */
  isEditing() {
    return !!this.getBody();
  }

  /** Returns true if the form is currently in create mode (has not a body set) */
  isCreating() {
    return !this.isEditing();
  }
}

result-matching ""

    No results matching ""