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(sheets-formula-ui): preserve hovered function selection on mouse leave (#7899)

wpxp123456ba6b3dbdf28e
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx b/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx
index 22b220b8ff..40abd7a0fa 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx
+++ b/packages/sheets-formula-ui/src/views/formula-editor/search-function/SearchFunction.tsx
@@ -65,13 +65,6 @@ function SearchFunctionFactory(props: ISearchFunctionProps, ref: any) {
setActive(index);
}
- function handleLiMouseLeave() {
- if (!isEnableMouseEnterOrOut.current) {
- return;
- }
- setActive(-1);
- }
-
useEffect(() => {
if (!searchList.length) {
return;
@@ -103,7 +96,9 @@ function SearchFunctionFactory(props: ISearchFunctionProps, ref: any) {
case KeyCode.TAB:
case KeyCode.ENTER: {
const item = searchList[active];
- handleFunctionSelect(item.name, item.functionType);
+ if (item) {
+ handleFunctionSelect(item.name, item.functionType);
+ }
break;
}
case KeyCode.ESC: {
@@ -214,7 +209,6 @@ function SearchFunctionFactory(props: ISearchFunctionProps, ref: any) {
'univer-bg-gray-200 dark:!univer-bg-gray-600': active === index,
})}
onMouseEnter={() => handleLiMouseEnter(index)}
- onMouseLeave={handleLiMouseLeave}
onMouseMove={debounceResetMouseState}
onClick={() => {
handleFunctionSelect(item.name, item.functionType);
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/search-function/__tests__/SearchFunction.spec.tsx b/packages/sheets-formula-ui/src/views/formula-editor/search-function/__tests__/SearchFunction.spec.tsx
index 42b4ac97c2..d84b0d0a13 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/search-function/__tests__/SearchFunction.spec.tsx
+++ b/packages/sheets-formula-ui/src/views/formula-editor/search-function/__tests__/SearchFunction.spec.tsx
@@ -391,6 +391,45 @@ describe('SearchFunction', () => {
expect(document.body.textContent).not.toContain('Adds selected values.');
});
+ it.each([KeyCode.TAB, KeyCode.ENTER])('keeps the hovered suggestion selected after leaving the panel for key %s', async (keyCode) => {
+ const { injector, editor, commandService } = createSearchFunctionTestBed();
+
+ await act(async () => {
+ root.render(
+ <RediContext.Provider value={{ injector }}>
+ <SearchFunction
+ isFocus
+ editor={editor}
+ sequenceNodes={[{ nodeType: sequenceNodeType.FUNCTION, token: 'SU' } as never]}
+ onSelect={(result) => SearchFunctionState.selected.push(result)}
+ />
+ </RediContext.Provider>
+ );
+ });
+
+ await waitForSearchList(editor);
+ const items = Array.from(popupRoot.querySelectorAll('li'));
+ expect(items).toHaveLength(2);
+
+ await act(async () => {
+ items[0].dispatchEvent(new MouseEvent('mousemove', { bubbles: true }));
+ items[0].dispatchEvent(new MouseEvent('mouseout', { bubbles: true, relatedTarget: items[1] }));
+ items[1].dispatchEvent(new MouseEvent('mouseover', { bubbles: true, relatedTarget: items[0] }));
+ });
+ expect(items[1].classList.contains('univer-bg-gray-200')).toBe(true);
+ expect(items[0].classList.contains('univer-bg-gray-200')).toBe(false);
+
+ await act(async () => {
+ items[1].dispatchEvent(new MouseEvent('mouseout', { bubbles: true, relatedTarget: document.body }));
+ });
+ expect(items[1].classList.contains('univer-bg-gray-200')).toBe(true);
+
+ await runFormulaSearchKeyboardCommand(commandService, editor, keyCode);
+
+ expect(SearchFunctionState.selected).toEqual([{ text: 'SUBTOTAL(', offset: -7 }]);
+ expect(popupRoot.querySelector('li')).toBeNull();
+ });
+
it('closes formula suggestions without selecting a function when escape is pressed', async () => {
const { injector, editor, commandService } = createSearchFunctionTestBed();