Skip to content

Commit

Permalink
add support for a custom context menu handler (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
heswell authored Jul 13, 2023
1 parent bb75350 commit 0203ef6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions vuu-ui/packages/vuu-data-ag-grid/src/useViewportRowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SuggestionFetcher,
useTypeaheadSuggestions,
useVuuMenuActions,
VuuMenuActionHandler,
VuuServerMenuOptions,
} from "@finos/vuu-data-react";

Expand Down Expand Up @@ -82,6 +83,7 @@ export interface ViewportRowModelHookProps {
| VuuUIMessageInRPCEditReject
| VuuUIMessageInRPCEditResponse
) => void;
vuuMenuActionHandler?: VuuMenuActionHandler;
}

type GroupByConfigChange = {
Expand All @@ -97,6 +99,7 @@ export const useViewportRowModel = ({
dataSource,
onRpcResponse,
onFeatureEnabled,
vuuMenuActionHandler,
}: ViewportRowModelHookProps) => {
const menuRef = useRef<VuuMenu>();
const getTypeaheadSuggestionsRef = useRef<SuggestionFetcher>(
Expand Down Expand Up @@ -139,6 +142,7 @@ export const useViewportRowModel = ({
);

const { buildViewserverMenuOptions, handleMenuAction } = useVuuMenuActions({
clientSideMenuActionHandler: vuuMenuActionHandler,
dataSource,
menuActionConfig,
onRpcResponse: handleRpcResponse,
Expand Down
34 changes: 18 additions & 16 deletions vuu-ui/packages/vuu-data-react/src/hooks/useVuuMenuActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,6 @@ const { KEY } = metadataKeys;

const NO_CONFIG: MenuActionConfig = {};

// const contextSortPriorities = {
// "selected-rows": 0,
// cell: 1,
// row: 2,
// grid: 3,
// };

// const byContext = (menu1: VuuMenuItem, menu2: VuuMenuItem) => {
// return (
// contextSortPriorities[menu1.context] - contextSortPriorities[menu2.context]
// );
// };

export const isVisualLinksAction = (
action: GridAction
): action is DataSourceVisualLinksMessage => action.type === "vuu-links";
Expand Down Expand Up @@ -179,12 +166,14 @@ const getMenuRpcRequest = (
}
};

export type VuuMenuActionHandler = (type: string, options: unknown) => boolean;

export interface ViewServerHookResult {
buildViewserverMenuOptions: MenuBuilder<
TableMenuLocation,
VuuServerMenuOptions
>;
handleMenuAction: (type: string, options: unknown) => boolean;
handleMenuAction: VuuMenuActionHandler;
}

export interface MenuActionConfig {
Expand All @@ -194,6 +183,16 @@ export interface MenuActionConfig {
}

export interface VuuMenuActionHookProps {
/**
* By default, vuuMenuActions will be handled automatically. When activated, a
* message will be sent to server and response will be handled here too.
* This prop allows client to provide a custom handler for a menu Item. This will
* take priority and if handler returns true, no further processing for the menu
* item will be handled by Vuu. This can also be used to prevent an item from being
* actioned, even when no custom handling is intended. If the handler returns false,
* Vuu will process the menuItem.
*/
clientSideMenuActionHandler?: VuuMenuActionHandler;
dataSource: DataSource;
menuActionConfig?: MenuActionConfig;
onRpcResponse?: (
Expand Down Expand Up @@ -308,6 +307,7 @@ const buildMenuDescriptor = (
};

export const useVuuMenuActions = ({
clientSideMenuActionHandler,
dataSource,
menuActionConfig = NO_CONFIG,
onRpcResponse,
Expand Down Expand Up @@ -352,7 +352,9 @@ export const useVuuMenuActions = ({

const handleMenuAction = useCallback(
(type: string, options: unknown) => {
if (type === "MENU_RPC_CALL") {
if (clientSideMenuActionHandler?.(type, options)) {
return true;
} else if (type === "MENU_RPC_CALL") {
const rpcRequest = getMenuRpcRequest(options as VuuMenuItem);
dataSource.menuRpcCall(rpcRequest).then((rpcResponse) => {
if (onRpcResponse && rpcResponse) {
Expand All @@ -372,7 +374,7 @@ export const useVuuMenuActions = ({
}
return false;
},
[dataSource, onRpcResponse]
[clientSideMenuActionHandler, dataSource, onRpcResponse]
);

return {
Expand Down

0 comments on commit 0203ef6

Please sign in to comment.