packages/core/src/lib/form/form.ts
The Form class is an Item with additional info about its properties (Fields).
Properties |
Methods |
constructor(body: T, config?: FormConfig
|
|||||||||
Defined in packages/core/src/lib/form/form.ts:14
|
|||||||||
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 :
|
Public config |
Type : FormConfig<T>
|
Defined in packages/core/src/lib/form/form.ts:14
|
The configuration of the form. It is an extension of ItemConfig. |
Public fields |
Type : Field[]
|
Defined in packages/core/src/lib/form/form.ts:12
|
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. |
createField | |||||||||
createField(property: string, config: FieldConfigProperty)
|
|||||||||
Defined in packages/core/src/lib/form/form.ts:31
|
|||||||||
creates and adds a single field to the form
Parameters :
Returns :
Field | undefined
|
getField | ||||||
getField(property: string)
|
||||||
Defined in packages/core/src/lib/form/form.ts:49
|
||||||
returns the field instance of the given property
Parameters :
Returns :
any
|
getValue | ||||||
getValue(property: string)
|
||||||
Defined in packages/core/src/lib/form/form.ts:54
|
||||||
Returns the original value of the property, if any.
Parameters :
Returns :
any
|
isCreating |
isCreating()
|
Defined in packages/core/src/lib/form/form.ts:75
|
Returns true if the form is currently in create mode (has not a body set)
Returns :
boolean
|
isEditing |
isEditing()
|
Defined in packages/core/src/lib/form/form.ts:70
|
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 :
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 :
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 :
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 :
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 :
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 :
Returns :
boolean
|
pickWriteOnly | ||||||
pickWriteOnly(value)
|
||||||
Inherited from
Item
|
||||||
Defined in
Item:165
|
||||||
Returns value with all readOnly properties removed
Parameters :
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 :
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 :
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 :
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 :
Returns :
any
|
sort | ||||||
sort(property: string)
|
||||||
Inherited from
Item
|
||||||
Defined in
Item:160
|
||||||
Transforms the given field's value for sorting
Parameters :
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 :
Returns :
any
|
useConfig | ||||||
useConfig(config: ItemConfig
|
||||||
Inherited from
Item
|
||||||
Defined in
Item:52
|
||||||
Assigns the given config to the existing via Object.assign
Parameters :
Returns :
void
|
Form is an Item with extra functions:
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();
}
}