slack-block-builder
Version:
Maintainable code for interactive Slack messages, modals, home tabs, and workflow steps. A must-have for the Slack Block Kit framework.
41 lines (40 loc) • 1.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccordionUIComponent = void 0;
const blocks_1 = require("../blocks");
const elements_1 = require("../elements");
const constants_1 = require("../internal/constants");
const lib_1 = require("../internal/lib");
class AccordionUIComponent {
constructor(params) {
this.items = params.items;
this.paginator = params.paginator;
this.expandButtonText = params.expandButtonText || constants_1.ComponentUIText.More;
this.collapseButtonText = params.collapseButtonText || constants_1.ComponentUIText.Close;
this.titleTextFunction = params.titleTextFunction;
this.actionIdFunction = params.actionIdFunction;
this.builderFunction = params.builderFunction;
this.isExpandableFunction = params.isExpandableFunction;
}
getBlocks() {
const unpruned = this.items.map((item, index) => {
const isExpanded = this.paginator.checkItemIsExpandedByIndex(index);
const section = blocks_1.Blocks.Section({ text: this.titleTextFunction({ item }) });
if (this.isExpandableFunction(item)) {
section.accessory(elements_1.Elements.Button({
text: isExpanded ? this.collapseButtonText : this.expandButtonText,
actionId: this.actionIdFunction({
expandedItems: this.paginator.getNextStateByItemIndex(index),
}),
}));
}
const blocks = [
section,
...isExpanded ? this.builderFunction({ item }).flat() : [],
];
return index === 0 ? blocks : [blocks_1.Blocks.Divider(), ...blocks];
}).flat();
return lib_1.Builder.pruneUndefinedFromArray(unpruned);
}
}
exports.AccordionUIComponent = AccordionUIComponent;