UNPKG

handsontable

Version:

Handsontable is a JavaScript Data Grid available for React, Angular and Vue.

95 lines (92 loc) 2.81 kB
"use strict"; exports.__esModule = true; exports.getRenderer = _getItem; exports.registerRenderer = _register; var _staticRegister = require("../utils/staticRegister"); var _errors = require("../helpers/errors"); const { register, getItem, hasItem, getNames, getValues } = (0, _staticRegister.staticRegister)('renderers'); /** * Retrieve renderer function. * * @param {string} name Renderer identification. * @returns {Function} Returns renderer function. */ exports.getRegisteredRenderers = getValues; exports.getRegisteredRendererNames = getNames; exports.hasRenderer = hasItem; function _getItem(name) { if (typeof name === 'function') { return name; } if (!hasItem(name)) { (0, _errors.throwWithCause)(`No registered renderer found under "${name}" name`); } return getItem(name); } /** * Register renderer under its alias. * * @param {string|Function} name Renderer's alias or renderer function with its descriptor. * @param {Function} [renderer] Renderer function. */ function _register(name, renderer) { if (typeof name !== 'string') { renderer = name; name = renderer.RENDERER_TYPE; } register(name, renderer); } /** * Factory function for creating custom Handsontable cell renderers. * * This factory simplifies the creation of custom cell renderers by wrapping the standard * Handsontable renderer function signature with a more convenient object-based callback. * * @param {Function} callback - Function that renders the cell content. * @param {object} callback.instance - The Handsontable instance. * @param {HTMLElement} callback.td - The table cell element to render into. * @param {number} callback.row - The row index of the cell. * @param {number} callback.column - The column index of the cell. * @param {string|number} callback.prop - The property name or column index. * @param {*} callback.value - The current value of the cell. * @param {object} callback.cellProperties - The cell's configuration properties. * * @returns {Function} A renderer function compatible with Handsontable's renderer API. * * @example * ```typescript * const myRenderer = rendererFactory(({ td, value, cellProperties }) => { * td.innerHTML = ''; * const div = document.createElement('div'); * div.textContent = value || ''; * div.style.color = cellProperties.customColor || 'black'; * td.appendChild(div); * }); * * // Use in column definition * const columns = [{ * data: 'myColumn', * renderer: myRenderer * }]; * ``` */ const rendererFactory = callback => { return (instance, td, row, column, prop, value, cellProperties) => { return callback({ instance, td, row, column, prop, value, cellProperties }); }; }; exports.rendererFactory = rendererFactory;