Hello all!
So I have created a DropDownButton with four options like this:
handleTitleMenuClick = (e: any): void => {
switch (e.itemIndex) {
case0:
this.openPartTypeModal();
break;
case1:
this.openPartNameModal();
break;
case2:
this.openAddSubPartModal();
break;
case3:
this.deletePart();
break;
default:
return;
}
}
render = () => {
const titleMenuItems = [
{text: "Change part type"},
{text: "Rename part"},
{text: "Add subpart", disabled: this.state.subParts.length >= this.state.maxSubParts},
{text: "Delete part", disabled: this.state.subParts.length > 0}
];
return (
<div className="part-container">
<div className="part-title">
<div className="part-title-text">...</div>
<div className="part-title-menu">
<DropDownButton buttonClass="part-title-menu-button"
popupSettings={{popupClass: 'common-step-title-menu-container'}}
icon={'more-vertical'}
items={titleMenuItems}
onItemClick={this.handleTitleMenuClick}
/>
</div>
</div>
<div className="part-content">...</div>
</div>
);
}
This works exactly like I intend it; I have four options in my dropdown and when I click on an option it behaves a expected.
However, this solution feels very clunky and I have been looking for a neater solution. What I would have deemed most intuitive was to have a "onClick" for each item, something like this:
const titleMenuItems = [
{text: "Change part type", onClick: this.openPartTypeModal()},
{text: "Rename part", onClick: this.openPartNameModal()},
{text: "Add subpart", disabled: this.state.subParts.length >= this.state.maxSubParts, onClick: this.openAddSubPartModal()},
{text: "Delete part", disabled: this.state.subParts.length > 0, onClick: this.deletePart()}
];
I have tried adding the above but that resulted in some strange rendering error (Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.). I have looked through the documentation but I have not found anything that could help me.
The closest I found was an onClick in https://www.telerik.com/kendo-react-ui/components/buttons/api/ButtonItemProps/ but I don't understand if it's applicable here, and if so, how to implement it.
Maybe there is no alternative?