From d8f06ea8fe87dc18248a83565e678972b4ce4ea1 Mon Sep 17 00:00:00 2001 From: awesomeYG <993631441@qq.com> Date: Fri, 29 Dec 2023 13:23:45 +0800 Subject: [PATCH] fix: autocomplete select --- src/layouts/Header/index.tsx | 50 +++++++++++++++++++++++------------- src/pages/_app.tsx | 5 ++++ src/utils/tools.ts | 7 ----- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/layouts/Header/index.tsx b/src/layouts/Header/index.tsx index e96e5d3..cffa43a 100644 --- a/src/layouts/Header/index.tsx +++ b/src/layouts/Header/index.tsx @@ -125,11 +125,25 @@ const Header: React.FC<{}> = () => { > setOpenSearch(false)} onOpen={() => setOpenSearch(true)} + onKeyDown={(event) => { + if (event.key === 'Enter') { + // Prevent's default 'Enter' behavior. + event.defaultMuiPrevented = true; + const tool = allTools.find( + (item) => item.label === (event.target as any)?.value || '' + ); + if (tool) { + setOpenSearch(false); + router.push(tool.path); + } + // your handler code + } + }} filterOptions={(menu, state) => menu.filter((item) => { return ( @@ -143,30 +157,30 @@ const Header: React.FC<{}> = () => { }) } options={allTools} - renderOption={(props: object, option: Tool) => ( - - { + return ( + setOpenSearch(false)} + {...props} > - - - - )} + + + + + ); + }} sx={{ height: '100%' }} + autoHighlight renderInput={(params) => ( + + {currentItem?.label + ? currentItem?.label + ' - 百川在线工具箱' + : '百川云常用工具'} +