Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Property grid]: update deps #1090

Merged
merged 15 commits into from
Nov 14, 2024
Merged
7 changes: 7 additions & 0 deletions apps/learning-snippets/src/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import globalJsdom from "global-jsdom";
import * as jsdom from "jsdom";
import sinonChai from "sinon-chai";

// polyfill ResizeObserver
global.ResizeObserver = class ResizeObserver {
public observe() {}
public unobserve() {}
public disconnect() {}
};

// get rid of various xhr errors in the console
globalJsdom(undefined, {
virtualConsole: new jsdom.VirtualConsole().sendTo(console, { omitJSDOMErrors: true }),
Expand Down
62 changes: 43 additions & 19 deletions packages/itwin/property-grid/src/hooks/UseContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/

import { useEffect, useState } from "react";
import { createContext, useContext, useEffect, useState } from "react";
import { DropdownMenu, Flex, MenuItem } from "@itwin/itwinui-react";
import { FavoritePropertiesScope, Presentation } from "@itwin/presentation-frontend";
import { copyToClipboard } from "../api/WebUtilities";
Expand Down Expand Up @@ -58,8 +58,15 @@ export interface PropertyGridContextMenuItemProps {
* @public
*/
export function PropertyGridContextMenuItem({ id, children, title, onSelect }: PropsWithChildren<PropertyGridContextMenuItemProps>) {
const { close } = useContextMenuContext();

const handleOnClick = () => {
onSelect();
close();
};

return (
<MenuItem key={id} onClick={onSelect} title={title}>
<MenuItem key={id} onClick={handleOnClick} title={title}>
{children}
</MenuItem>
);
Expand Down Expand Up @@ -202,7 +209,7 @@ export interface UseContentMenuProps extends ContextMenuProps {

interface ContextMenuDefinition {
position: { x: number; y: number };
menuItems: (close: () => void) => JSX.Element[];
menuItems: JSX.Element[];
}

/**
Expand All @@ -220,12 +227,7 @@ export function useContextMenu({ dataProvider, imodel, contextMenuItems }: UseCo
}

const field = await dataProvider.getFieldByPropertyDescription(args.propertyRecord.property);
const items = (close: () => void) =>
contextMenuItems.map((item, index) => (
<Flex key={index} onClick={close}>
{item({ imodel, dataProvider, record: args.propertyRecord, field })}
</Flex>
));
const items = contextMenuItems.map((item, index) => <Flex key={index}>{item({ imodel, dataProvider, record: args.propertyRecord, field })}</Flex>);
saskliutas marked this conversation as resolved.
Show resolved Hide resolved

onFeatureUsed("context-menu");
setContextMenu({
Expand All @@ -241,16 +243,18 @@ export function useContextMenu({ dataProvider, imodel, contextMenuItems }: UseCo

const close = () => setContextMenu(undefined);
return (
<DropdownMenu
menuItems={contextMenu.menuItems}
visible={true}
style={{
transform: `translate(${contextMenu.position.x}px, ${contextMenu.position.y}px)`,
}}
onVisibleChange={(visible) => !visible && close()}
>
<></>
</DropdownMenu>
<ContextMenuContextProvider close={close}>
<DropdownMenu
menuItems={contextMenu.menuItems}
visible={true}
style={{
transform: `translate(${contextMenu.position.x}px, ${contextMenu.position.y}px)`,
}}
onVisibleChange={(visible) => !visible && close()}
>
<></>
</DropdownMenu>
</ContextMenuContextProvider>
);
};

Expand All @@ -259,3 +263,23 @@ export function useContextMenu({ dataProvider, imodel, contextMenuItems }: UseCo
renderContextMenu,
};
}

interface ContextMenuContext {
close: () => void;
}

const contextMenuContext = createContext<ContextMenuContext>({
close: () => {},
});

function ContextMenuContextProvider({ close, children }: PropsWithChildren<ContextMenuContext>) {
const [value] = useState(() => ({
close,
}));

return <contextMenuContext.Provider value={value}>{children}</contextMenuContext.Provider>;
}

function useContextMenuContext() {
return useContext(contextMenuContext);
}
saskliutas marked this conversation as resolved.
Show resolved Hide resolved
Loading