@mui/x-tree-view
Version:
The community edition of the MUI X Tree View components.
50 lines (48 loc) • 1.67 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { labelSelectors } from "./selectors.js";
import { useLabelEditingItemPlugin } from "./itemPlugin.js";
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(['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', _extends({}, this.store.state.itemMetaLookup, {
[itemId]: _extends({}, item, {
label
})
}));
if (this.store.parameters.onItemLabelChange) {
this.store.parameters.onItemLabelChange(itemId, label);
}
};
}