Skip to content

Commit

Permalink
fix: Add default suggest position if search is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed Sep 25, 2024
1 parent 642e180 commit 93a7dea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/components/ConstructorPage/useNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ReactNode} from 'react';
import type {NavigationData} from '@gravity-ui/page-constructor';
import type {NavigationData, NavigationItemModel} from '@gravity-ui/page-constructor';
import type {DocBasePageData} from '@diplodoc/components';
import type {WithNavigation} from '../App';
import type {Props as HeaderControlsProps} from '../HeaderControls';
Expand All @@ -8,7 +8,11 @@ import React, {useMemo} from 'react';
import {ControlSizes, CustomNavigation, MobileDropdown} from '@diplodoc/components';

import {HEADER_HEIGHT} from '../../constants';
import {useRouter} from '../';
import {useRouter, useSearch} from '../';

function findItem(right: NavigationItemModel[], left: NavigationItemModel[], type: string) {
return right.some((item) => item.type === type) || left.some((item) => item.type === type);
}

export const useNavigation = (
data: DocBasePageData<WithNavigation>,
Expand All @@ -21,10 +25,16 @@ export const useNavigation = (
const {header = {}, logo} = navigation;
const {leftItems = [], rightItems = []} = header as NavigationData['header'];

const withControls = rightItems.some((item: {type: string}) => item.type === 'controls');
const withControls = findItem(rightItems, leftItems, 'controls');
const withSearch = findItem(rightItems, leftItems, 'search');

const search = useSearch();
const router = useRouter();

if (search && !withSearch) {
rightItems.unshift({type: 'search'} as unknown as NavigationItemModel);
}

const navigationData = useMemo(
() => ({
withBorder: true,
Expand Down
6 changes: 5 additions & 1 deletion src/components/Search/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {SearchConfig, WorkerApi, WorkerConfig} from './types';

import {createContext} from 'react';
import {createContext, useContext} from 'react';

export type {SearchConfig, WorkerConfig, WorkerApi};

Expand All @@ -11,3 +11,7 @@ SearchContext.displayName = 'SearchContext';
export const SearchProvider = SearchContext.Provider;

export {Search} from './Search';

export function useSearch() {
return useContext(SearchContext);
}
2 changes: 1 addition & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {SearchContext, SearchProvider} from './Search';
export {SearchContext, SearchProvider, useSearch} from './Search';
export {RouterContext, RouterProvider, useRouter} from './Router';

export {App} from './App';

0 comments on commit 93a7dea

Please sign in to comment.