Skip to content

Commit 3265b6c

Browse files
authored
Add option to provide callback function to iModel action disable prop (#197)
* Enhance context menu options: add disabled state * Add option to provide callback function for iModel action disable prop
1 parent 38d0cc3 commit 3265b6c

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@itwin/imodel-browser-react",
5+
"comment": "Add option to provide callback function to iModel action disable prop",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@itwin/imodel-browser-react"
10+
}

packages/apps/storybook/src/imodel-browser/IModelGrid.stories.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,32 @@ IndividualContextMenu.args = {
213213
children: "displayName contains 'R'",
214214
visible: (iModel) => iModel.displayName?.includes("R") ?? false,
215215
key: "withR",
216-
onClick: (iModel) => alert("Contains R" + iModel?.displayName),
216+
onClick: (iModel) => alert("Contains R: " + iModel?.displayName),
217217
},
218218
{
219219
children: "Add description",
220220
visible: (iModel) => !iModel.description,
221221
key: "addD",
222-
onClick: (iModel) => alert("Add description" + iModel?.displayName),
222+
onClick: (iModel) => alert("Add description: " + iModel?.displayName),
223223
},
224224
{
225225
children: "Edit description",
226226
visible: (iModel) => !!iModel.description,
227227
key: "editD",
228-
onClick: (iModel) => alert("Edit description" + iModel?.displayName),
228+
onClick: (iModel) => alert("Edit description: " + iModel?.displayName),
229+
},
230+
],
231+
};
232+
233+
export const DisabledContextMenu = Template.bind({});
234+
DisabledContextMenu.args = {
235+
apiOverrides: { serverEnvironmentPrefix: "qa" },
236+
iModelActions: [
237+
{
238+
children: "Disabled if name contains 'T'",
239+
disabled: (iModel) => iModel.displayName?.includes("T") ?? false,
240+
key: "withT",
241+
onClick: (iModel) => alert("Does not contain T: " + iModel?.displayName),
229242
},
230243
],
231244
};

packages/modules/imodel-browser/src/utils/_buildMenuOptions.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import React from "react";
88
type MenuItemProps = React.ComponentPropsWithoutRef<typeof MenuItem>;
99

1010
export interface ContextMenuBuilderItem<T = any>
11-
extends Omit<MenuItemProps, "onClick" | "value"> {
11+
extends Omit<MenuItemProps, "onClick" | "value" | "disabled"> {
1212
key: string;
1313
visible?: boolean | ((value: T) => boolean);
1414
onClick?: ((value?: T, refetchData?: () => void) => void) | undefined;
15+
disabled?: MenuItemProps["disabled"] | ((value: T) => boolean);
1516
}
1617

1718
/** Build MenuItem array for the value for each provided options
@@ -27,10 +28,11 @@ export const _buildManagedContextMenuOptions: <T>(
2728
?.filter?.(({ visible }) => {
2829
return typeof visible === "function" ? visible(value) : visible ?? true;
2930
})
30-
.map(({ key, visible, onClick, ...contextMenuProps }) => {
31+
.map(({ key, visible, onClick, disabled, ...contextMenuProps }) => {
3132
return (
3233
<MenuItem
3334
{...contextMenuProps}
35+
disabled={typeof disabled === "function" ? disabled(value) : disabled}
3436
onClick={(e?: React.MouseEvent) => {
3537
e?.stopPropagation();
3638
closeMenu?.();

0 commit comments

Comments
 (0)