Git Product home page Git Product logo

custom-renderer's Introduction

This repository is now considered legacy and no longer supported. Please take a look at our recent repositories and help documentation at the following links.

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.

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.