@mui/x-tree-view
Version:
The community edition of the MUI X Tree View components.
53 lines (51 loc) • 1.82 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage";
import { labelSelectors } from "./selectors.mjs";
import { useLabelEditingItemPlugin } from "./itemPlugin.mjs";
export class TreeViewLabelEditingPlugin {
constructor(store) {
this.store = store;
store.itemPluginManager.register(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 && !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(process.env.NODE_ENV !== "production" ? `MUI X: The Tree View component requires all items to have a \`label\` property.
The label of an item cannot be empty.
Item ID: ${itemId}` : _formatErrorMessage(197, itemId));
}
const item = this.store.state.itemMetaLookup[itemId];
if (item.label === label) {
return;
}
this.store.set('itemMetaLookup', _extends({}, this.store.state.itemMetaLookup, {
[itemId]: _extends({}, item, {
label
})
}));
if (this.store.parameters.onItemLabelChange) {
this.store.parameters.onItemLabelChange(itemId, label);
}
};
}