UNPKG

@mui/x-tree-view

Version:

The community edition of the MUI X Tree View components.

58 lines (55 loc) 2 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.TreeViewLabelEditingPlugin = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var _selectors = require("./selectors"); var _itemPlugin = require("./itemPlugin"); class TreeViewLabelEditingPlugin { constructor(store) { this.store = store; store.itemPluginManager.register(_itemPlugin.useLabelEditingItemPlugin, null); } buildPublicAPI = () => { return { setEditedItem: this.setEditedItem, updateItemLabel: this.updateItemLabel }; }; /** * Set which item is currently being edited. * You can pass `null` to exit editing mode. * @param {TreeViewItemId | null} itemId The id of the item to edit, or `null` to exit editing mode. */ setEditedItem = itemId => { if (itemId !== null && !_selectors.labelSelectors.isItemEditable(this.store.state, itemId)) { return; } this.store.set('editedItemId', itemId); }; /** * Used to update the label of an item. * @param {TreeViewItemId} itemId The id of the item to update the label of. * @param {string} label The new label of the item. */ updateItemLabel = (itemId, label) => { if (!label) { throw new Error(['MUI X: The Tree View component requires all items to have a `label` property.', 'The label of an item cannot be empty.', itemId].join('\n')); } const item = this.store.state.itemMetaLookup[itemId]; if (item.label === label) { return; } this.store.set('itemMetaLookup', (0, _extends2.default)({}, this.store.state.itemMetaLookup, { [itemId]: (0, _extends2.default)({}, item, { label }) })); if (this.store.parameters.onItemLabelChange) { this.store.parameters.onItemLabelChange(itemId, label); } }; } exports.TreeViewLabelEditingPlugin = TreeViewLabelEditingPlugin;