UNPKG

@lonli-lokli/react-mosaic-component

Version:
68 lines (67 loc) 1.98 kB
// libs/react-mosaic-component/src/lib/buttons/TabSplitButton.tsx import classNames from "classnames"; import { noop } from "lodash-es"; import React from "react"; import { MosaicContext } from "../contextTypes.mjs"; import { OptionalBlueprint } from "../util/OptionalBlueprint.mjs"; import { DefaultToolbarButton } from "./MosaicButton.mjs"; var TabSplitButton = class extends React.PureComponent { static contextType = MosaicContext; render() { return /* @__PURE__ */ React.createElement( DefaultToolbarButton, { title: "Split Tab Group", className: classNames( "split-button", OptionalBlueprint.getIconClass( this.context.blueprintNamespace, "SPLIT_COLUMNS" ) ), onClick: this.split } ); } split = () => { const { mosaicActions } = this.context; const { path } = this.props; if (mosaicActions.createNode == null) { console.error( "Operation invalid unless `createNode` is defined on Mosaic" ); return; } Promise.resolve(mosaicActions.createNode()).then((newNode) => { if (!newNode) return; const currentNode = this.getCurrentTabsNode(); if (!currentNode) return; const newSplitNode = { type: "split", direction: "row", splitPercentages: [50, 50], children: [currentNode, newNode] }; mosaicActions.replaceWith(path, newSplitNode); if (this.props.onClick) { this.props.onClick(); } }).catch(noop); }; getCurrentTabsNode = () => { const root = this.context.mosaicActions.getRoot(); if (!root) return null; let currentNode = root; for (const index of this.props.path) { if (typeof currentNode === "object" && "children" in currentNode) { currentNode = currentNode.children[index]; } else { return null; } } return currentNode; }; }; export { TabSplitButton };