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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(sheets-ui): close and align named range dropdown (#7821)
wpxp1234566de25322d1e0
diff --git a/packages/design/src/components/dropdown/Dropdown.tsx b/packages/design/src/components/dropdown/Dropdown.tsxindex 3050f44e7e..a7c3ef2601 100644--- a/packages/design/src/components/dropdown/Dropdown.tsx+++ b/packages/design/src/components/dropdown/Dropdown.tsx@@ -14,13 +14,14 @@* limitations under the License.*/-import type { ComponentProps, ReactNode } from 'react';+import type { ComponentProps, ReactNode, RefObject } from 'react';import { useState } from 'react';-import { PopoverContent, PopoverPrimitive, PopoverTrigger } from './PopoverPrimitive';+import { PopoverAnchor, PopoverContent, PopoverPrimitive, PopoverTrigger } from './PopoverPrimitive';export interface IDropdownProps extends ComponentProps<typeof PopoverContent> {children: ReactNode;overlay: ReactNode;+ anchorRef?: RefObject<HTMLElement | null>;disabled?: boolean;open?: boolean;onOpenChange?: (open: boolean) => void;@@ -30,6 +31,7 @@ export function Dropdown(props: IDropdownProps) {const {children,overlay,+ anchorRef,disabled,open: controlledOpen,onOpenChange: controlledOnOpenChange,@@ -56,6 +58,7 @@ export function Dropdown(props: IDropdownProps) {return (<PopoverPrimitive open={open} onOpenChange={handleChangeOpen}>+ {anchorRef && <PopoverAnchor virtualRef={anchorRef} />}<PopoverTrigger asChild>{children}</PopoverTrigger>diff --git a/packages/design/src/components/dropdown/__tests__/Dropdown.spec.tsx b/packages/design/src/components/dropdown/__tests__/Dropdown.spec.tsxnew file mode 100644index 0000000000..57c2fbbf33--- /dev/null+++ b/packages/design/src/components/dropdown/__tests__/Dropdown.spec.tsx@@ -0,0 +1,67 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++/**+ * @vitest-environment jsdom+ */++import { cleanup, render, screen, waitFor } from '@testing-library/react';+import { createRef } from 'react';+import { afterEach, describe, expect, it, vi } from 'vitest';+import { Dropdown } from '../Dropdown';++afterEach(() => {+ cleanup();+ vi.restoreAllMocks();+});++describe('Dropdown positioning', () => {+ it.each([+ { customAnchor: true, expectedLeft: 160 },+ { customAnchor: false, expectedLeft: 236 },+ ])('aligns to the selected anchor (customAnchor=$customAnchor)', async ({ customAnchor, expectedLeft }) => {+ vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function getRect(this: HTMLElement) {+ if (this.getAttribute('data-testid') === 'name-box') {+ return new DOMRect(160, 60, 96, 24);+ }+ if (this.tagName === 'BUTTON') {+ return new DOMRect(236, 60, 20, 24);+ }+ return new DOMRect();+ });++ const anchorRef = createRef<HTMLDivElement>();+ render(+ <div ref={anchorRef} data-testid="name-box">+ <input aria-label="Name box" />+ <Dropdown+ anchorRef={customAnchor ? anchorRef : undefined}+ align="start"+ avoidCollisions={false}+ overlay={<div>Named ranges</div>}+ open+ >+ <button type="button">Open named ranges</button>+ </Dropdown>+ </div>+ );++ await waitFor(() => {+ const positioner = screen.getByText('Named ranges').closest('[data-radix-popper-content-wrapper]');+ expect(positioner?.getAttribute('style')).toContain(`translate(${expectedLeft}px, 88px)`);+ });+ });+});diff --git a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx b/packages/sheets-ui/src/views/defined-name/DefinedName.tsxindex 6a28821762..c84b5b8e4f 100644--- a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx+++ b/packages/sheets-ui/src/views/defined-name/DefinedName.tsx@@ -76,6 +76,7 @@ export function DefinedName({ disable }: { disable: boolean }) {const [open, setOpen] = useState(false);const [isInputEvent, setIsInputEvent] = useState(false);+ const nameBoxRef = useRef<HTMLDivElement>(null);const inputRef = useRef<HTMLInputElement>(null);const getDefinedNameMap = () => {@@ -284,6 +285,7 @@ export function DefinedName({ disable }: { disable: boolean }) {return (<div+ ref={nameBoxRef}data-u-comp="defined-name"className={`univer-relative univer-box-border univer-flex univer-h-full univer-w-24 univer-border-r-gray-200@@ -308,9 +310,15 @@ export function DefinedName({ disable }: { disable: boolean }) {/><Dropdown+ align="start"+ anchorRef={nameBoxRef}overlay={(<div className="univer-z-[1001]">- <DefinedNameOverlay search={inputValue} isInputEvent={isInputEvent} />+ <DefinedNameOverlay+ search={inputValue}+ isInputEvent={isInputEvent}+ onClose={() => setOpen(false)}+ /></div>)}disabled={disable}diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsxindex 8dd28ead46..604f8bbbc8 100644--- a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx@@ -27,7 +27,13 @@ import { map, startWith } from 'rxjs';import { SidebarDefinedNameOperation } from '../../commands/operations/sidebar-defined-name.operation';import { DEFINED_NAME_CONTAINER } from './component-name';-export function DefinedNameOverlay({ search, isInputEvent }: { search: string; isInputEvent: boolean }) {+interface IDefinedNameOverlayProps {+ search: string;+ isInputEvent: boolean;+ onClose: () => void;+}++export function DefinedNameOverlay({ search, isInputEvent, onClose }: IDefinedNameOverlayProps) {const commandService = useDependency(ICommandService);const localeService = useDependency(LocaleService);const definedNamesService = useDependency(IDefinedNamesService);@@ -89,8 +95,9 @@ export function DefinedNameOverlay({ search, isInputEvent }: { search: string; ioverscan: 6,});- const openSlider = () => {- commandService.executeCommand(SidebarDefinedNameOperation.id, { value: 'open' });+ const openSlider = async () => {+ onClose();+ await commandService.executeCommand(SidebarDefinedNameOperation.id, { value: 'open' });};const focusDefinedName = async (definedName: IDefinedNamesServiceParam) => {@@ -135,7 +142,10 @@ export function DefinedNameOverlay({ search, isInputEvent }: { search: string; ihover:univer-bg-gray-100dark:hover:!univer-bg-gray-600`}- onClick={() => { focusDefinedName(definedName); }}+ onClick={async () => {+ onClose();+ await focusDefinedName(definedName);+ }}><divdata-u-comp="defined-name-overlay-row"