-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/layouts/modules/global-search/components/SearchFooter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import classNames from 'classnames'; | ||
import { Divider } from 'antd'; | ||
import style from './footer.module.scss'; | ||
|
||
const SearchFooter = () => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<> | ||
<Divider className="my-2px" /> | ||
<div className="h-44px flex-center gap-14px"> | ||
<span className="flex-y-center"> | ||
<IconMdiKeyboardReturn className={classNames(style['operate-shadow'], style['operate-item'])} /> | ||
<span>{t('common.confirm')}</span> | ||
</span> | ||
<span className="flex-y-center"> | ||
<IconMdiArrowUpThin className={classNames([style['operate-shadow'], style['operate-item']])} /> | ||
<IconMdiArrowDownThin className={classNames(style['operate-shadow'], style['operate-item'])} /> | ||
<span>{t('common.switch')}</span> | ||
</span> | ||
<span className="flex-y-center"> | ||
<IconMdiKeyboardEsc className={classNames(style['operate-shadow'], style['operate-item'])} /> | ||
<span>{t('common.close')}</span> | ||
</span> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(SearchFooter); |
160 changes: 160 additions & 0 deletions
160
src/layouts/modules/global-search/components/SearchModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import classNames from 'classnames'; | ||
import { useDebounceFn, useKeyPress } from 'ahooks'; | ||
import { getIsMobile } from '@/store/slice/app'; | ||
import SearchFooter from './SearchFooter'; | ||
import SearchResult from './SearchResult'; | ||
|
||
interface Props { | ||
show: boolean; | ||
onClose: () => void; | ||
} | ||
|
||
/** | ||
* Transform menu to searchMenus | ||
* | ||
* @param menus - menus | ||
* @param treeMap | ||
*/ | ||
function transformMenuToSearchMenus(menus: App.Global.Menu[], treeMap: App.Global.Menu[] = []) { | ||
if (menus && menus.length === 0) return []; | ||
return menus.reduce((acc, cur) => { | ||
if (!cur.children) { | ||
acc.push(cur); | ||
} | ||
if (cur.children && cur.children.length > 0) { | ||
transformMenuToSearchMenus(cur.children, treeMap); | ||
} | ||
return acc; | ||
}, treeMap); | ||
} | ||
|
||
const SearchModal: FC<Props> = memo(({ show, onClose }) => { | ||
const isMobile = useAppSelector(getIsMobile); | ||
|
||
const { t } = useTranslation(); | ||
|
||
const { allMenus } = useMixMenuContext(); | ||
|
||
const [keyword, setKeyword] = useState<string>(''); | ||
|
||
const [resultOptions, setResultOptions] = useState<App.Global.Menu[]>([]); | ||
|
||
const [activeRouteName, setActiveRouteName] = useState<string>(''); | ||
|
||
const searchMenus = useMemo(() => transformMenuToSearchMenus(allMenus), [allMenus]); | ||
|
||
function handleClose() { | ||
// handle with setTimeout to prevent user from seeing some operations | ||
setTimeout(() => { | ||
onClose(); | ||
setResultOptions([]); | ||
setKeyword(''); | ||
}, 200); | ||
} | ||
|
||
function search() { | ||
const result = searchMenus.filter(menu => { | ||
const trimKeyword = keyword.toLocaleLowerCase().trim(); | ||
return trimKeyword && menu.title?.includes(trimKeyword); | ||
}); | ||
const activeName = result[0]?.key ?? ''; | ||
|
||
setResultOptions(result); | ||
setActiveRouteName(activeName); | ||
} | ||
|
||
/** key up */ | ||
function handleUp() { | ||
handleKeyPress(-1); // 方向 -1 表示向上 | ||
} | ||
|
||
/** key down */ | ||
function handleDown() { | ||
handleKeyPress(1); // 方向 1 表示向下 | ||
} | ||
|
||
function getActivePathIndex() { | ||
return resultOptions.findIndex(item => item.key === activeRouteName); | ||
} | ||
|
||
function handleKeyPress(direction: 1 | -1) { | ||
const { length } = resultOptions; | ||
if (length === 0) return; | ||
|
||
const index = getActivePathIndex(); | ||
if (index === -1) return; | ||
|
||
const activeIndex = (index + direction + length) % length; // 确保 index 在范围内循环 | ||
const activeKey = resultOptions[activeIndex].key; | ||
|
||
setActiveRouteName(activeKey); | ||
} | ||
|
||
const router = useRouterPush(); | ||
|
||
const handleSearch = useDebounceFn(search, { wait: 300 }); | ||
|
||
/** key enter */ | ||
function handleEnter() { | ||
if (resultOptions.length === 0 || activeRouteName === '') return; | ||
handleClose(); | ||
router.menuPush(activeRouteName); | ||
} | ||
|
||
useKeyPress('Escape', handleClose); | ||
useKeyPress('Enter', handleEnter); | ||
useKeyPress('uparrow', handleUp); | ||
useKeyPress('downarrow', handleDown); | ||
|
||
return ( | ||
<AModal | ||
footer={isMobile ? null : <SearchFooter />} | ||
styles={{ content: { paddingBottom: 0, height: isMobile ? '100vh' : '100%' } }} | ||
style={isMobile ? { margin: 0, padding: 0, maxWidth: '100%' } : undefined} | ||
height={isMobile ? '100%' : 400} | ||
open={show} | ||
width={isMobile ? '100%' : 630} | ||
className={classNames({ 'top-0px rounded-0': isMobile })} | ||
onCancel={onClose} | ||
closable={false} | ||
> | ||
<ASpace.Compact className="w-full"> | ||
<AInput | ||
allowClear | ||
onInput={handleSearch.run} | ||
prefix={<IconUilSearch className="text-15px text-#c2c2c2" />} | ||
placeholder={t('common.keywordSearch')} | ||
onChange={e => setKeyword(e.target.value)} | ||
value={keyword} | ||
/> | ||
{isMobile && ( | ||
<AButton | ||
onClick={handleClose} | ||
type="primary" | ||
ghost | ||
> | ||
{t('common.cancel')} | ||
</AButton> | ||
)} | ||
</ASpace.Compact> | ||
|
||
<div className="mt-20px"> | ||
{resultOptions.length === 0 ? ( | ||
<AEmpty /> | ||
) : ( | ||
resultOptions.map(item => ( | ||
<SearchResult | ||
enter={handleEnter} | ||
key={item.key} | ||
active={item.key === activeRouteName} | ||
setActiveRouteName={setActiveRouteName} | ||
menu={item} | ||
/> | ||
)) | ||
)} | ||
</div> | ||
</AModal> | ||
); | ||
}); | ||
|
||
export default SearchModal; |
35 changes: 35 additions & 0 deletions
35
src/layouts/modules/global-search/components/SearchResult.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import classNames from 'classnames'; | ||
|
||
interface Props { | ||
menu: App.Global.Menu; | ||
active: boolean; | ||
setActiveRouteName: (name: string) => void; | ||
enter: () => void; | ||
} | ||
|
||
const SearchResult: FC<Props> = memo(({ menu, active, setActiveRouteName, enter }) => { | ||
function handleMouseEnter() { | ||
setActiveRouteName(menu.key); | ||
} | ||
|
||
return ( | ||
<div | ||
className={classNames( | ||
'mt-8px h-56px flex-y-center cursor-pointer justify-between rounded-4px bg-#e5e7eb px-14px dark:bg-dark', | ||
{ 'bg-primary': active }, | ||
{ 'text-#fff': active } | ||
)} | ||
onMouseEnter={handleMouseEnter} | ||
onClick={enter} | ||
> | ||
<span className="ml-5px flex-1"> | ||
{menu.icon} | ||
{menu.label} | ||
</span> | ||
|
||
<IconAntDesignEnterOutlined /> | ||
</div> | ||
); | ||
}); | ||
|
||
export default SearchResult; |
10 changes: 10 additions & 0 deletions
10
src/layouts/modules/global-search/components/footer.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.operate-shadow { | ||
box-shadow: | ||
inset 0 -2px #cdcde6, | ||
inset 0 0 1px 1px #fff, | ||
0 1px 2px 1px #1e235a66; | ||
} | ||
|
||
.operate-item { | ||
--uno: mr-6px p-2px text-20px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters