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.

feat: filter defined name list if input event (#6332)

Roman Sergeevcd2bcb272a11
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx b/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
index 45c162f28e..1906aeb591 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedName.tsx
@@ -18,7 +18,7 @@ import type { Workbook, Worksheet } from '@univerjs/core';
import type { IDefinedNamesServiceParam } from '@univerjs/engine-formula';
import type { ChangeEvent, KeyboardEvent } from 'react';
import type { IScrollToCellCommandParams } from '../../commands/commands/set-scroll.command';
-import { AbsoluteRefType, ICommandService, IUniverInstanceService, ThemeService, UniverInstanceType } from '@univerjs/core';
+import { AbsoluteRefType, debounce, ICommandService, IUniverInstanceService, ThemeService, UniverInstanceType } from '@univerjs/core';
import { borderRightClassName, clsx, Dropdown } from '@univerjs/design';
import { deserializeRangeWithSheet, IDefinedNamesService, isReferenceString, LexerTreeBuilder, serializeRangeWithSheet } from '@univerjs/engine-formula';
import { MoreDownIcon } from '@univerjs/icons';
@@ -42,6 +42,9 @@ export function DefinedName({ disable }: { disable: boolean }) {
const unitId = workbook?.getUnitId();
const themeService = useDependency(ThemeService);
+ const [open, setOpen] = useState(false);
+ const [isInputEvent, setIsInputEvent] = useState(false);
+
const getDefinedNameMap = () => {
const definedNameMap = definedNamesService.getDefinedNameMap(unitId);
if (definedNameMap) {
@@ -124,8 +127,27 @@ export function DefinedName({ disable }: { disable: boolean }) {
};
}, []); // Empty dependency array means this effect runs once on mount and clean up on unmount
+ const handleDefinedNamesList = debounce((value: string) => {
+ const hasMatch = definedNames.find((i) => i.name.toLowerCase().includes(value.toLowerCase()));
+
+ if (hasMatch && !open) {
+ setOpen(true);
+ } else if (!hasMatch && open) {
+ setOpen(false);
+ }
+ }, 100);
+
function handleChangeSelection(e: ChangeEvent<HTMLInputElement>) {
- setInputValue(e.target.value.trim());
+ const value = e.target.value.trim();
+ setInputValue(value);
+ setIsInputEvent(true);
+
+ if (!value) {
+ if (open) setOpen(false);
+ return;
+ }
+
+ handleDefinedNamesList(value);
}
// TODO: need implemented set defined name if value not referenceString
@@ -149,8 +171,11 @@ export function DefinedName({ disable }: { disable: boolean }) {
if (e.key === 'Enter') {
confirm();
+ setIsInputEvent(false);
+ setOpen(false);
} else if (e.key === 'Escape') {
e.preventDefault();
+ setIsInputEvent(false);
resetValue();
}
}
@@ -204,15 +229,19 @@ export function DefinedName({ disable }: { disable: boolean }) {
value={inputValue}
onChange={handleChangeSelection}
onKeyDown={handleKeyDown}
+ onBlur={() => setIsInputEvent(false)}
/>
<Dropdown
overlay={(
<div className="univer-z-[1001]">
- <DefinedNameOverlay />
+ <DefinedNameOverlay search={inputValue} isInputEvent={isInputEvent} />
</div>
)}
disabled={disable}
+ open={open}
+ onOpenChange={setOpen}
+ onOpenAutoFocus={(e) => e.preventDefault()}
>
<a
className={clsx(`
diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx
index 5654b6d6a4..9a6d84b3a5 100644
--- a/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx
+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameOverlay.tsx
@@ -22,11 +22,11 @@ import { borderBottomClassName, clsx, scrollbarClassName } from '@univerjs/desig
import { IDefinedNamesService } from '@univerjs/engine-formula';
import { SetWorksheetShowCommand } from '@univerjs/sheets';
import { ISidebarService, useDependency } from '@univerjs/ui';
-import { useEffect, useState } from 'react';
+import { useEffect, useMemo, useState } from 'react';
import { SidebarDefinedNameOperation } from '../../commands/operations/sidebar-defined-name.operation';
import { DEFINED_NAME_CONTAINER } from './component-name';
-export function DefinedNameOverlay() {
+export function DefinedNameOverlay({ search, isInputEvent }: { search: string; isInputEvent: boolean }) {
const commandService = useDependency(ICommandService);
const localeService = useDependency(LocaleService);
const definedNamesService = useDependency(IDefinedNamesService);
@@ -72,6 +72,12 @@ export function DefinedNameOverlay() {
};
}, []);
+ const filteredDefinedNames = useMemo(() => {
+ return search && isInputEvent
+ ? definedNames.filter((i) => i.name.toLowerCase().includes(search.toLowerCase()))
+ : definedNames;
+ }, [search, isInputEvent, definedNames]);
+
const openSlider = () => {
commandService.executeCommand(SidebarDefinedNameOperation.id, { value: 'open' });
};
@@ -97,7 +103,7 @@ export function DefinedNameOverlay() {
<ul
className={clsx('univer-m-0 univer-max-h-[360px] univer-list-none univer-overflow-y-auto univer-p-0', scrollbarClassName)}
>
- {definedNames.map((definedName, index) => {
+ {filteredDefinedNames.map((definedName, index) => {
return (
<li
key={index}