handsontable
Version:
Handsontable is a JavaScript Data Grid available for React, Angular and Vue.
57 lines (54 loc) • 1.95 kB
JavaScript
;
exports.__esModule = true;
exports.intlTimeRenderer = intlTimeRenderer;
exports.valueFormatter = valueFormatter;
var _textRenderer = require("../textRenderer");
var _mixed = require("../../helpers/mixed");
var _object = require("../../helpers/object");
var _constants = require("../../helpers/constants");
var _dateTime = require("../../helpers/dateTime");
const RENDERER_TYPE = exports.RENDERER_TYPE = 'intl-time';
const DEFAULT_INTL_FORMAT = {
hour: 'numeric',
minute: '2-digit'
};
/**
* Formats the value using the date format.
*
* @param {*} value Value to be formatted.
* @param {CellMeta} cellProperties Cell meta object.
* @returns {*} Returns the rendered value.
*/
function valueFormatter(value, cellProperties) {
const {
timeFormat,
locale,
allowEmpty
} = cellProperties;
if ((0, _mixed.isEmpty)(value)) {
return allowEmpty ? value : _constants.BAD_VALUE_TEXT;
}
const time = (0, _dateTime.parseToLocalTime)(value);
if (time === null) {
return _constants.BAD_VALUE_TEXT;
}
const intlFormat = (0, _object.isObject)(timeFormat) ? timeFormat : DEFAULT_INTL_FORMAT;
return new Intl.DateTimeFormat(locale, intlFormat).format(time);
}
/**
* Default intlTime renderer.
*
* @private
* @param {Core} hotInstance The Handsontable instance.
* @param {HTMLTableCellElement} TD The rendered cell element.
* @param {number} row The visual row index.
* @param {number} col The visual column index.
* @param {number|string} prop The column property (passed when datasource is an array of objects).
* @param {*} value The rendered value.
* @param {object} cellProperties The cell meta object (see {@link Core#getCellMeta}).
*/
function intlTimeRenderer(hotInstance, TD, row, col, prop, value, cellProperties) {
_textRenderer.textRenderer.apply(this, [hotInstance, TD, row, col, prop, value, cellProperties]);
TD.dir = 'ltr';
}
intlTimeRenderer.RENDERER_TYPE = RENDERER_TYPE;