Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(ui): support dynamic menu command params (#7293)

Univer68ff703d07aa
diff --git a/packages/ui/src/services/menu/menu.ts b/packages/ui/src/services/menu/menu.ts
index d9d064b87b..45154af1a1 100644
--- a/packages/ui/src/services/menu/menu.ts
+++ b/packages/ui/src/services/menu/menu.ts
@@ -63,7 +63,7 @@ interface IMenuItemBase<TLocaleKey extends string, TValue> {
hidden$?: Observable<boolean>;
disabled$?: Observable<boolean>;
- params?: IMenuCommandParams;
+ params?: IMenuCommandParams | (() => IMenuCommandParams | undefined);
/** On observable value that should emit the value of the corresponding selection component. */
value$?: Observable<TValue>;
}
@@ -78,7 +78,7 @@ export interface IValueOption<TLocaleKey extends string = string, TValue = undef
id?: string;
value?: string | number;
value$?: Observable<TValue>;
- params?: IMenuCommandParams | ((value?: string | number) => IMenuCommandParams);
+ params?: IMenuCommandParams | ((value?: string | number) => IMenuCommandParams | undefined);
slot?: boolean;
label?: MenuLabel; // custom component, send to CustomLabel label property
icon?: string;
diff --git a/packages/ui/src/views/components/ribbon/ToolbarItem.tsx b/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
index 6d733be48e..c8a4e7455f 100644
--- a/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
+++ b/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
@@ -239,8 +239,8 @@ export const ToolbarItem = forwardRef<ITooltipWrapperRef, IDisplayMenuItem<IMenu
commandId = option.id;
}
- const commandParams = typeof option.params === 'function' ? option.params(option.value) : option.params ?? { value: option.value };
- executeCommand(commandId, commandParams);
+ const commandParams = typeof option.params === 'function' ? option.params(option.value) : option.params;
+ executeCommand(commandId, commandParams ?? { value: option.value });
}
function handleSelectionsValueChange(value: string | number) {
@@ -252,7 +252,8 @@ export const ToolbarItem = forwardRef<ITooltipWrapperRef, IDisplayMenuItem<IMenu
if (disabled) return;
if (menuType === MenuItemType.BUTTON_SELECTOR) {
- executeCommand(bId, params ?? { value });
+ const commandParams = typeof params === 'function' ? params() : params;
+ executeCommand(bId, commandParams ?? { value });
}
}
@@ -328,8 +329,6 @@ export const ToolbarItem = forwardRef<ITooltipWrapperRef, IDisplayMenuItem<IMenu
function renderButtonType() {
const isCustomComponent = componentManager.get(typeof label === 'string' ? label : label?.name ?? '');
- const commandValue = params ?? (typeof value === 'undefined' ? undefined : { value });
-
return (
<ToolbarButton
data-u-command={id}
@@ -337,7 +336,10 @@ export const ToolbarItem = forwardRef<ITooltipWrapperRef, IDisplayMenuItem<IMenu
noIcon={!icon}
active={activated}
disabled={disabled}
- onClick={() => executeCommand(props.commandId ?? props.id, commandValue)}
+ onClick={() => {
+ const commandParams = typeof params === 'function' ? params() : params;
+ executeCommand(props.commandId ?? props.id, commandParams ?? (typeof value === 'undefined' ? undefined : { value }));
+ }}
onDoubleClick={() => props.subId && executeCommand(props.subId)}
>
{isCustomComponent
diff --git a/packages/ui/src/views/components/ribbon/__tests__/ToolbarItem.spec.tsx b/packages/ui/src/views/components/ribbon/__tests__/ToolbarItem.spec.tsx
index 921a58df26..dc90b6d460 100644
--- a/packages/ui/src/views/components/ribbon/__tests__/ToolbarItem.spec.tsx
+++ b/packages/ui/src/views/components/ribbon/__tests__/ToolbarItem.spec.tsx
@@ -94,6 +94,26 @@ function renderWithDependencies(element: ReactElement) {
afterEach(cleanup);
describe('ToolbarItem', () => {
+ it('resolves menu params when clicking a button', () => {
+ const { getByRole, commandService } = renderWithDependencies(
+ <ToolbarItem
+ id="test-button"
+ type={MenuItemType.BUTTON}
+ title="Dynamic params"
+ params={() => ({ unitId: 'unit-1' })}
+ />
+ );
+
+ fireEvent.click(getByRole('button', { name: 'Dynamic params' }));
+
+ expect(commandService.calls).toEqual([
+ {
+ commandId: 'test-button',
+ params: { unitId: 'unit-1' },
+ },
+ ]);
+ });
+
it('forwards menu params when clicking a button selector main action', () => {
const rule = { type: 'checkbox' };
const { container, commandService } = renderWithDependencies(