formio.js icon indicating copy to clipboard operation
formio.js copied to clipboard

Custom component in formio.js

Open amirmf opened this issue 8 years ago • 16 comments

hi i would be wondering if u could tell me is there any way to add custom component to formio.js? i found a solution in angular version but couldn't found any in pure js version thanks

amirmf avatar Apr 03 '18 14:04 amirmf

@amirmf check the code in this closed defects. Although it's being answered under Angular project, however, same steps can be applied in vanila JavaScript but your project should use TypeScript since the core of form.io (render engine) is being written by it.

asalhani avatar Apr 18 '18 19:04 asalhani

@asalhani thanks for the response you mean we cant register any custom component if we use pure js renderer like below ???

Formio.createForm(document.getElementById('taskForm'), formioJSON,formOptions).then(function (form) { ....

amirmf avatar Apr 22 '18 10:04 amirmf

Here is a Gist of how to create custom components. The registration of this component in the gist happens in Angular 5, but the concept is the same for just the vanilla.js renderer here.

https://gist.github.com/travist/59fa77086ec899e24992fc1508e61075

travist avatar Jul 17 '18 13:07 travist

Awesome. Thank you @travist

Aiso786 avatar Jul 17 '18 14:07 Aiso786

@travist @Aiso786 does this line work for you? import { FormioForm } from 'formiojs/full'; if so what version are you using? I am using: "formiojs": "^3.5.3" and so far cannot find this formiojs/full

I just want to use a custom component and cannot as this code does not work! What am I missing here? Thanks.

Sigkill9 avatar Sep 19 '18 13:09 Sigkill9

Hello @Sigkill9 I'm doing like this with the latest version:

import { Formio, Form } from 'formiojs'; ... Formio.registerComponent('tabs', CustomTabsComponent);

Aiso786 avatar Sep 19 '18 13:09 Aiso786

@Aiso786 Thanks for the reply. I am using angular-formio and have installed formiojs using: npm i formiojs --save and the @types/formio is not a thing.

I have this problem? Could not find a declaration file for module 'formiojs'. 'c:/angularapp/node_modules/formiojs/index.js' implicitly has an 'any' type. Try npm install @types/formiojs if it exists or add a new declaration (.d.ts) file containing declare module 'formiojs';

Sigkill9 avatar Sep 19 '18 14:09 Sigkill9

You just need to

add a new declaration (.d.ts) file containing declare module 'formiojs';"

Aiso786 avatar Sep 19 '18 14:09 Aiso786

You just need to

add a new declaration (.d.ts) file containing declare module 'formiojs';"

Where? :(

mavwolverine avatar Sep 20 '18 18:09 mavwolverine

You just need to

add a new declaration (.d.ts) file containing declare module 'formiojs';"

Can you please let me know where to add this file?

rohitsiddha avatar Sep 20 '18 18:09 rohitsiddha

@Aiso786 @travist

From what I could find on this topic I added a new file. src/app/typings/index.d.ts

//index.d.ts
declare module 'formiojs'

Defined path to above file in tsconfig.json

"typeRoots": [
      "node_modules/@types",
      "./src/app/typings/index"
    ],

I have added the file and the error has gone away.

While debugging I see my component added to _components but when rendering the form I get Unknown component: mycustomcomponent

This is really the only thing holding up my implementation. So there is no straight forward way through the angular-formio lib to add a custom component? I tried using angular-formio/node_modules/formiojs But that fails too.. So ... stuck again.

Please help!

Sigkill9 avatar Sep 20 '18 21:09 Sigkill9

Stepping out of this thread as my issue is really about registering custom components in angular-formio and not vanilla JavaScript.

Based on the example below. Angular 5 + Form.io Custom Component

Sigkill9 avatar Sep 21 '18 14:09 Sigkill9

Hi fomiojs,

I am facing the same issue (being on 4.0.0-rc.22).

HOW TO REPRODUCE

  1. Take the sample component Javascript as it is from: https://github.com/formio/formio.js/blob/v4.0.0-rc.22/app/examples/customcomponent.md

  2. Add it to the builder: mygroup: { title: "My Group", key: "mygroup", default: true, weight: 0, components: { checkmatrix: { title: "Check Matrix", group: "mygroup", key: "checkmatrix" } }, subgroups: [] }

CURRENT BEHAVIOUR

The component is displayed for selection and it is possible to add it to the form. However, there is no preview available:

image

image

ghost avatar Aug 01 '19 08:08 ghost

Additional Note:

When taking the component code from https://gist.github.com/travist/59fa77086ec899e24992fc1508e61075 without altering it, not even the "unknown component" message is displayed. The console says "component.render is not a function".

image

Please provide a working example for a custom component. Thank you!

ghost avatar Aug 01 '19 09:08 ghost

That component is designed for the 3.x branch. Here is the same one converted to the 4.x branch which has very different internals. https://github.com/formio/react-app-starterkit/blob/master/src/components/CheckMatrix.js

randallknutson avatar Aug 01 '19 14:08 randallknutson

@randallknutson Thank you, this worked as expected! Also, it looks cleaner than the previous version.

I just had to add Formio.Components.addComponent('checkmatrix', CheckMatrix); at the end.

ghost avatar Aug 01 '19 15:08 ghost