alfresco-ng2-components/docs/core/services/form-rendering.service.md
Amedeo Lepore d8f1eda132
[AAE-6919] add a display text form control (#7718)
* [AAE-9373] Create adf-rich-text-editor into core library, install editorjs and import it into the component, add a basic editorjs configuration

* [AAE-9374] create a rich text editor demo page

* [AAE-9376] Install header to provide headings blocks

* [AAE-9376] Install editorjs List plugin to add ordered and unordered list

* [AAE-9376] Install text color plugin to change text color and highlight text

* [AAE-9376] Install paragraph plugin to set text alignment to right, left, center and justify

* [AAE-9376] Install font size plugin to increase/decrease font size

* [AAE-9376] Install @editorjs/underline plugin to underline text

* [AAE-9376] Install @editorjs/inline-code to marking code-fragments

* [AAE-9376] Set shortcut to underline text

* [AAE-9376] Install @editorjs/code to add code examples

* [AAE-9376] Enable custom picker to color text changer, add colors codes

* [AAE-9376] Add input to fill rich text editor data

* [AAE-9376] Demo rich text editor, add sample data to display editor content

* [AAE-9376] Demo rich text editor, add sample data to display editor content

* [AAE-9376] Install @editorjs/marker plugin to highlight the text, because color plugin doesn't save marker style

* [AAE-9373] Send editor text output data onReady and onChange

* [AAE-9374] Update editor demo page to show the output on the right side of the page

* [AAE-9373] Allow to enable editorjs readOnly mode

* [AAE-9373] Return rich text editor save data as promise

* [AAE-9480] Add new display-rich-text widget to allow the user add Rich Text into a form

* [AAE-9371] Add readonly class if readOnly property is true to remove the editor 300px padding bottom in readonly mode, set a dynamic id to editorjs

* [AAE-9371] Destroy editorInstance on component destroy to FIX an error faced when there are multiple editorjs instance in the same page --> Uncaught TypeError: Cannot read property 'updateCurrentInput' of undefined

* [AAE-9480] Install editorjs-html utility to parse editorjs clean data to HTML

* [AAE-9480] parse editorjs data to HTML to avoid to create a new EditorJS instance for every single widget and improve performance

* [AAE-9480] Set pre styles to show show Code block styles correctly

* [AAE-9480] Remove space between rules

* [AAE-9480] Set editorjs and plugins fixed versions

* [AAE-9480] Removed unused editorjs dependency

* [AAE-9371] Set is ready property when editor instance is ready, check if is ready to destroy the instance on component destroy

* [AAE-9480] Add parse editorjs data to html Test

* [AAE-9371] Send rich-text-editor component data only if readOnly mode is false

* [AAE-9480] Test if display-rich-text widget is resolved

* [AAE-9480] Rename DisplayRichTextComponent into DisplayRichTextWidgetComponent to be compliant with other widget component names

* [AAE-9480] update Readme files with DisplayRichTextWidgetComponent

* [AAE-9371] Update header text

* [AAE-9371] Add Rich text editor component usage documentation

* [AAE-9480] Add padding to the widget container

* [AAE-9371] Remove plugin that align pragraph text since editorjs-html parser doesn't handle paragraph alignment

* [AAE-9371] Set editor autofocus to true

* [AAE-9480] Add a display-rich-text widget example to demo cloud form

* [AAE-9371] Fix lint issue

* [AAE-9371] Remove duplicated import to fix import module issue in a lib build job
2022-07-26 12:36:08 +02:00

7.9 KiB

Title, Added, Status, Last reviewed
Title Added Status Last reviewed
Form Rendering service v2.0.0 Active 2018-11-20

Form Rendering service

Maps a form field type string onto the corresponding form widget component type.

Class members

Methods

  • getComponentTypeResolver(type: string, defaultValue: Type<any> = this.defaultValue): DynamicComponentResolveFunction
    Gets the currently active DynamicComponentResolveFunction for a field type.
    • type: string - The type whose resolver you want
    • defaultValue: Type<any> - Default type returned for types that are not yet mapped
    • Returns DynamicComponentResolveFunction - Resolver function
  • register(components: Function, override: boolean = false)
    Register multiple components
    • components: Function -
    • override: boolean -
  • resolveComponentType(model: DynamicComponentModel, defaultValue: Type<any> = this.defaultValue): Type<any>
    Finds the component type that is needed to render a form field.
    • model: DynamicComponentModel - Form field model for the field to render
    • defaultValue: Type<any> - Default type returned for field types that are not yet mapped.
    • Returns Type<any> - Component type
  • setComponentTypeResolver(type: string, resolver: DynamicComponentResolveFunction, override: boolean = true)
    Sets or optionally replaces a DynamicComponentResolveFunction for a field type.
    • type: string - The type whose resolver you want to set
    • resolver: DynamicComponentResolveFunction - The new resolver function
    • override: boolean - The new resolver will only replace an existing one if this parameter is true

Details

The Form Field component uses this service to choose which widget to use to render an instance of a form field. The Form Field model stores the field type name as a string (see the table below). The Form Rendering service maintains a mapping between each type name and a corresponding DynamicComponentResolveFunction. The function takes a FormFieldModel object as its argument and uses the data from the object to determine which widget should be used to render the field.

In some cases, the field type string alone is enough to determine the widget type and so the function just returns the type directly:

let customResolver: DynamicComponentResolveFunction = () => CustomWidgetComponent;
formRenderingService.setComponentTypeResolver('text', customResolver, true);

In other cases, the function may need to choose the widget dynamically based on specific values in the form data:

let customResolver: DynamicComponentResolveFunction = (field: FormFieldModel): Type<{}> => {
    if (field) {
        let params = field.params;
    }
    return UnknownWidgetComponent;
};
formRenderingService.setComponentTypeResolver('text', customResolver, true);

Default type mapping

The Form Rendering service is initialized with the mapping shown in the table below:

Stencil name Field type string Component type
Amount "amount" AmountWidgetComponent
Attach "upload" AttachWidgetComponent or UploadWidgetComponent (based on metadata)
Checkbox "boolean" CheckboxWidgetComponent
Date "date" DateWidgetComponent
Display text "readonly-text" DisplayTextWidgetComponentComponent
Display Rich text "display-rich-text" DisplayRichTextWidgetComponent
Display value "readonly" DisplayValueWidgetComponent
Dropdown "dropdown" DropdownWidgetComponent
Dynamic table "dynamic-table" DynamicTableWidgetComponent
Group of people "functional-group" FunctionalGroupWidgetComponent
Header "group" ContainerWidgetComponent
Hyperlink "hyperlink" HyperlinkWidgetComponent
Multi-line text "multi-line-text" MultilineTextWidgetComponentComponent
Number "integer" NumberWidgetComponent
People "people" PeopleWidgetComponent
Radio buttons "radio-buttons" RadioButtonsWidgetComponent
Text "text" TextWidgetComponent
Typeahead "typeahead" TypeaheadWidgetComponent

You can add new items to the mapping or replace existing items in order to customize the way fields are rendered.

Adding new or replacement items to the mapping

You can use the setComponentTypeResolver method to add a new ComponentTypeResolver function for a particular type string. You can also replace the resolver for a type that already exists in the mapping if you set the override parameter to 'true':

formRenderingService.setComponentTypeResolver('text', newResolver, true);

You would typically use this to replace an existing widget with your own custom version that implements a modified layout or responds differently when the data is entered. See the Form Extensibility and Customisation guide for further details and examples of this technique.

See also