Plugins

EzyTables now supports a plugin system that allows developers to extend the functionality of the library. This feature provides a flexible way to customize and enhance the behavior of EzyTables.

EzyTables now supports a plugin system that allows developers to extend the functionality of the library. This feature provides a flexible way to customize and enhance the behavior of EzyTables according to specific project requirements.

Plugin Interface

Each plugin must adhere to a specific interface, ensuring a consistent structure across all plugins. The interface includes the following properties:

  • name: A unique identifier for the plugin.

  • field: Specifies the field(s) the plugin should be applied to. It can be a string (for a single field) or an array of strings (for multiple fields).

  • transform: A function that takes in data and returns transformed data. This function is where the main logic of the plugin resides.

interface Plugin {
  name: string;
  field: string | string[];
  transform: (data: any) => any;
}

Registering Plugins

Plugins are registered when an EzyTables instance is created. The constructor of the EzyTables class accepts an array of plugins as an optional parameter. These plugins are then stored and applied during the table rendering process.

Applying Plugins

During the table rendering process, each plugin is applied to its specified field(s). If the field matches the one specified in the plugin, the transform function of the plugin is called with the field's value.

Example Usage

Here's an example of how a developer could use this system to register a date formatting plugin that only applies to the 'date' field:

const dateFormatPlugin: Plugin = {
  name: 'dateFormat',
  field: 'date',
  transform: (dateString: string) => {
    const date = new Date(dateString);
    return date.toLocaleDateString();
  },
};

const easyTable = new EasyTables({plugins: [dateFormatPlugin]});

This plugin will now only be applied to the date field when the table is rendered, transforming the date string into a more readable format.

The plugin feature in EzyTables provides a powerful way to customize the behavior of the library, making it even more flexible and adaptable to various use cases.

Last updated