GithubHelp home page GithubHelp logo

custom-renderer's Introduction

Custom Form.io Renderer

An example custom renderer that extends the Form.io core renderer.

Getting Started

To get started, simply fork this repo, and then modify the index.js file to contain the overrides for your custom renderer.

Extending Components

To extend components, you will simply extend the class and then override the methods you wish to override.

import TextFieldComponent from 'formiojs/components/textfield/TextField';
export default class CustomTextFieldComponent extends TextFieldComponent {
  // Override the createLabel method.
  createLabel(container) {
    super.createLabel(container);
    this.addClass(this.labelElement, 'text-success');
  }
}

This component can now be included in the renderer by overriding the component in the component registry.

// Register all the components which will use the new BaseComponent provided above.
import AllComponents from 'formiojs/components';

// Use our extended textfield component.
import CustomTextFieldComponent from './components/TextField';
AllComponents.textfield = CustomTextFieldComponent;

import Components from 'formiojs/components/Components';
Components.setComponents(AllComponents);

Overriding the BaseComponent

To override the base component, you can create a new BaseComponent file, and then use the following pattern to override certain methods within the BaseComponent.

Base.js

import BaseComponent from 'formiojs/components/base/Base';

// Copy the BaseComponent show method.
const baseShow = BaseComponent.prototype.show;

/**
 * Override the BaseComponent show method to introduce a new option called "hideComponents" which
 * will always hide the components that are within this map, like this.
 *
 *   {
 *     hideComponents: {
 *       firstName: true,
 *       lastName: true,
 *       email: true
 *     }
 *   }
 *
 * @param show
 * @return {*}
 */
BaseComponent.prototype.show = function(show) {
  if (
    this.options.hideComponents &&
    this.options.hideComponents[this.component.key]
  ) {
    return baseShow.call(this, false);
  }
  return baseShow.call(this, show);
}

// Export the new BaseComponent.
export default BaseComponent;

This will then be included in the index.js at the very beginning like so.

import BaseComponent from './components/Base';

Please see the index.js included in the source for an example of this.

Building

To build this renderer simply execute the following commands.

npm install
npm run build

This will create a new dist/formio.custom.min.js file that you can then use as a custom form renderer instead of the typical formio.full.min.js. Please see example.html for an example of how this works.

custom-renderer's People

Contributors

travist avatar

Stargazers

Mehmet Ali Kıpçak avatar

Watchers

Travis Tidwell avatar Randall Knutson avatar James Cloos avatar Muffadal Jaorawala avatar  avatar Denise Kay avatar

custom-renderer's Issues

Question

Hi,

i'm building a Vue form app based on Vue-formio and structured similar to React Formio App.
I'm trying to customize the renderer and hitting a few issues.

Environment

Please provide as many details as you can:

  • Hosting type
    • [] Form.io
    • Local deployment
      • Version:
        {
        version: "6.7.2",
        schema: "3.3.3"
        }
  • Formio.js version: 3.27.3
  • Frontend framework: Vuejs
  • Browser: Chrome
  • Browser version: Version 76.0.3809.132 (Official Build) (64-bit)

Steps to Reproduce

newBase.js

import BaseComponent from 'formiojs/components/base/Base';

const baseCreateElement = BaseComponent.prototype.createElement;
BaseComponent.prototype.createElement = function createElement() {
    if (this.element) {
      //update class for case when Logic changed container class (customClass)
      this.element.className = this.className;
      return this.element;
    }
    this.element = this.ce('div', {
      class: this.className,
      style: this.customStyle
    });
    console.log(this.element);
    // Ensure you can get the component info from the element.
    this.element.component = this;
    this.hook('element', this.element);
    return this.element;
}

export default BaseComponent;

newTextField.js

import TextFieldComponent from 'formiojs/components/textfield/TextField';
export class NewTextFieldComponent extends TextFieldComponent {
  // Override the createLabel method.
  createLabel(container) {
    super.createLabel(container);
    this.addClass(this.labelElement, 'ott-text-field');
  }
}

Form.ts (based on vue-formio)

import BaseComponent from '../NewRenderer/newBase.js';

// Register all the components which will use the new BaseComponent provided above.
import AllComponents from 'formiojs/components';
import NewTextFieldComponent from '../NewRenderer/newTextField.js'
// Use our extended textfield component.
AllComponents.textfield = NewTextFieldComponent;

import Components from 'formiojs/components/Components';
Components.setComponents(AllComponents);

Expected behavior

1.If the changes in BaseComponent is applied there should be console.log prints
2.The rendered textfield should have class ott-text-field added

Observed behavior

2 works, the new textfield component was updated.
1 doesn't seem to affect the base component at all

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.