@gooddata/react-components
Version:
GoodData.UI - A powerful JavaScript library for building analytical applications
122 lines • 6.86 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
// (C) 2020 GoodData Corporation
var React = require("react");
var react_intl_1 = require("react-intl");
var typings_1 = require("./typings");
var PresetsDropdown_1 = require("./presetsDropdown/PresetsDropdown");
var IntlWrapper_1 = require("../core/base/IntlWrapper");
var CustomFormatDialog_1 = require("./customFormatDialog/CustomFormatDialog");
exports.CUSTOM_FORMAT_PRESET_LOCAL_IDENTIFIER = "customFormat";
var WrappedMeasureNumberFormat = /** @class */ (function (_super) {
__extends(WrappedMeasureNumberFormat, _super);
function WrappedMeasureNumberFormat(props) {
var _this = _super.call(this, props) || this;
_this.findSelectedPreset = function () {
return _this.props.presets.find(function (preset) { return preset.format === _this.props.selectedFormat; }) ||
_this.getCustomFormatPreset();
};
_this.toggleDropdownOpened = function (e) {
_this.toggleButtonEl = e.currentTarget;
_this.setState(function (state) { return (__assign({}, state, { showDropdown: !state.showDropdown })); });
_this.toggleCustomFormatDialog();
};
_this.closeDropdown = function () {
_this.setState({ showDropdown: false });
};
_this.toggleCustomFormatDialog = function (open) {
if (open === void 0) { open = false; }
_this.setState({
showCustomFormatDialog: open,
});
};
_this.onCustomFormatDialogApply = function (format) {
_this.toggleCustomFormatDialog(false);
_this.setState({
selectedPreset: _this.getCustomFormatPreset(),
});
_this.props.setFormat(format);
};
_this.onCustomFormatDialogCancel = function () {
_this.toggleCustomFormatDialog(false);
};
_this.onSelect = function (selectedPreset) {
var setFormat = _this.props.setFormat;
_this.closeDropdown();
if (_this.isCustomPreset(selectedPreset)) {
_this.toggleCustomFormatDialog(true);
}
else {
_this.setState(function (state) { return (__assign({}, state, { selectedPreset: selectedPreset })); });
setFormat(selectedPreset.format);
}
};
_this.isCustomPreset = function (_a) {
var localIdentifier = _a.localIdentifier, type = _a.type;
return localIdentifier === exports.CUSTOM_FORMAT_PRESET_LOCAL_IDENTIFIER && type === typings_1.PresetType.CUSTOM_FORMAT;
};
_this.getCustomFormatPreset = function () { return ({
name: _this.props.intl.formatMessage({ id: "measureNumberFormat.custom.optionLabel" }),
localIdentifier: exports.CUSTOM_FORMAT_PRESET_LOCAL_IDENTIFIER,
format: null,
previewNumber: null,
type: typings_1.PresetType.CUSTOM_FORMAT,
}); };
_this.state = {
selectedPreset: _this.findSelectedPreset(),
showDropdown: false,
showCustomFormatDialog: false,
};
return _this;
}
WrappedMeasureNumberFormat.prototype.render = function () {
var _a = this.props, ToggleButton = _a.toggleButton, anchorElementSelector = _a.anchorElementSelector, presets = _a.presets, separators = _a.separators, selectedFormat = _a.selectedFormat, defaultCustomFormat = _a.defaultCustomFormat, presetsDropdownPositioning = _a.presetsDropdownPositioning, customFormatDialogPositioning = _a.customFormatDialogPositioning, documentationLink = _a.documentationLink, templates = _a.templates, intl = _a.intl;
var _b = this.state, showDropdown = _b.showDropdown, showCustomFormatDialog = _b.showCustomFormatDialog, selectedPreset = _b.selectedPreset;
var buttonText = intl.formatMessage({ id: "measureNumberFormat.buttonLabel" }, { selectedFormatPresetName: selectedPreset.name });
var anchorEl = anchorElementSelector || this.toggleButtonEl;
var customPreset = this.getCustomFormatPreset();
return (React.createElement(React.Fragment, null,
React.createElement(ToggleButton, { text: buttonText, isOpened: showDropdown || showCustomFormatDialog, toggleDropdown: this.toggleDropdownOpened }),
showDropdown && (React.createElement(PresetsDropdown_1.PresetsDropdown, { presets: presets, customPreset: customPreset, separators: separators, selectedPreset: selectedPreset, onSelect: this.onSelect, onClose: this.closeDropdown, anchorEl: anchorEl, positioning: presetsDropdownPositioning, intl: intl })),
showCustomFormatDialog && (React.createElement(CustomFormatDialog_1.CustomFormatDialog, { onApply: this.onCustomFormatDialogApply, onCancel: this.onCustomFormatDialogCancel, formatString: selectedFormat || defaultCustomFormat, separators: separators, anchorEl: anchorEl, positioning: customFormatDialogPositioning, documentationLink: documentationLink, templates: templates, intl: intl }))));
};
return WrappedMeasureNumberFormat;
}(React.PureComponent));
var MeasureNumberFormatWithIntl = react_intl_1.injectIntl(WrappedMeasureNumberFormat);
var MeasureNumberFormat = /** @class */ (function (_super) {
__extends(MeasureNumberFormat, _super);
function MeasureNumberFormat() {
return _super !== null && _super.apply(this, arguments) || this;
}
MeasureNumberFormat.prototype.render = function () {
return (React.createElement(IntlWrapper_1.IntlWrapper, { locale: this.props.locale },
React.createElement(MeasureNumberFormatWithIntl, __assign({}, this.props))));
};
return MeasureNumberFormat;
}(React.PureComponent));
exports.MeasureNumberFormat = MeasureNumberFormat;
//# sourceMappingURL=MeasureNumberFormat.js.map