-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial SearchRouter component and context to display it
- Loading branch information
Showing
9 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import Icon from '@components/Icon'; | ||
import * as Expensicons from '@components/Icon/Expensicons'; | ||
import {PressableWithoutFeedback} from '@components/Pressable'; | ||
import useTheme from '@hooks/useTheme'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import * as SearchUtils from '@libs/SearchUtils'; | ||
import {useSearchRouterContext} from './SearchRouterContext'; | ||
|
||
function SearchButton() { | ||
const styles = useThemeStyles(); | ||
const theme = useTheme(); | ||
const {toggleSearchRouter} = useSearchRouterContext(); | ||
|
||
if (!SearchUtils.shouldDisplayNewSearchRouter()) { | ||
return; | ||
} | ||
|
||
return ( | ||
<PressableWithoutFeedback | ||
accessibilityLabel="" | ||
style={[styles.flexRow, styles.mr2, styles.touchableButtonImage]} | ||
onPress={() => { | ||
toggleSearchRouter(); | ||
}} | ||
> | ||
<Icon | ||
src={Expensicons.MagnifyingGlass} | ||
fill={theme.icon} | ||
/> | ||
</PressableWithoutFeedback> | ||
); | ||
} | ||
|
||
SearchButton.displayName = 'SearchButton'; | ||
|
||
export default SearchButton; |
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,64 @@ | ||
import React, {useState} from 'react'; | ||
import {View} from 'react-native'; | ||
import Modal from '@components/Modal'; | ||
import type {SearchQueryJSON} from '@components/Search/types'; | ||
import useResponsiveLayout from '@hooks/useResponsiveLayout'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import * as SearchUtils from '@libs/SearchUtils'; | ||
import CONST from '@src/CONST'; | ||
import type {SearchDataTypes} from '@src/types/onyx/SearchResults'; | ||
import {useSearchRouterContext} from './SearchRouterContext'; | ||
import SearchRouterInput from './SearchRouterInput'; | ||
|
||
type SearchRouterProps = { | ||
type?: SearchDataTypes; | ||
}; | ||
|
||
function SearchRouter({type}: SearchRouterProps) { | ||
const styles = useThemeStyles(); | ||
const {isSmallScreenWidth} = useResponsiveLayout(); | ||
|
||
const {isSearchRouterDisplayed, toggleSearchRouter} = useSearchRouterContext(); | ||
const [, setCurrentQuery] = useState<SearchQueryJSON | undefined>(undefined); | ||
|
||
const modalType = isSmallScreenWidth ? CONST.MODAL.MODAL_TYPE.CENTERED_UNSWIPEABLE : CONST.MODAL.MODAL_TYPE.POPOVER; | ||
|
||
const onSearch = (userQuery: string) => { | ||
if (!userQuery) { | ||
setCurrentQuery(undefined); | ||
return; | ||
} | ||
|
||
const query = type ? `type:${type} ${userQuery}` : userQuery; | ||
const queryJSON = SearchUtils.buildSearchQueryJSON(query); | ||
|
||
if (queryJSON) { | ||
// eslint-disable-next-line | ||
console.log('parsedQuery', queryJSON); | ||
|
||
setCurrentQuery(queryJSON); | ||
} else { | ||
// Handle query parsing error | ||
} | ||
}; | ||
|
||
return ( | ||
<Modal | ||
type={modalType} | ||
fullscreen | ||
isVisible={isSearchRouterDisplayed} | ||
popoverAnchorPosition={{right: 20, top: 20}} | ||
onClose={() => { | ||
toggleSearchRouter(); | ||
}} | ||
> | ||
<View style={[styles.flex1, styles.p5]}> | ||
<SearchRouterInput onSearch={onSearch} /> | ||
</View> | ||
</Modal> | ||
); | ||
} | ||
|
||
SearchRouter.displayName = 'SearchRouter'; | ||
|
||
export default SearchRouter; |
33 changes: 33 additions & 0 deletions
33
src/components/Search/SearchRouter/SearchRouterContext.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,33 @@ | ||
import React, {useCallback, useContext, useState} from 'react'; | ||
import type ChildrenProps from '@src/types/utils/ChildrenProps'; | ||
|
||
const defaultSearchContext = { | ||
isSearchRouterDisplayed: false, | ||
toggleSearchRouter: () => {}, | ||
}; | ||
|
||
type SearchRouterContext = typeof defaultSearchContext; | ||
|
||
const Context = React.createContext<SearchRouterContext>(defaultSearchContext); | ||
|
||
function SearchRouterContextProvider({children}: ChildrenProps) { | ||
const [isSearchRouterDisplayed, setIsSearchRouterDisplayed] = useState(false); | ||
|
||
const toggleSearchRouter = useCallback(() => { | ||
setIsSearchRouterDisplayed(!isSearchRouterDisplayed); | ||
}, [isSearchRouterDisplayed]); | ||
|
||
const routerContext = { | ||
isSearchRouterDisplayed, | ||
toggleSearchRouter, | ||
}; | ||
return <Context.Provider value={routerContext}>{children}</Context.Provider>; | ||
} | ||
|
||
function useSearchRouterContext() { | ||
return useContext(Context); | ||
} | ||
|
||
SearchRouterContextProvider.displayName = 'SearchRouterContextProvider'; | ||
|
||
export {SearchRouterContextProvider, useSearchRouterContext}; |
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,50 @@ | ||
import React, {useState} from 'react'; | ||
import {View} from 'react-native'; | ||
import TextInput from '@components/TextInput'; | ||
import useTheme from '@hooks/useTheme'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import CONST from '@src/CONST'; | ||
|
||
type SearchRouterInputProps = { | ||
onSearch: (searchTerm: string) => void; | ||
}; | ||
|
||
function SearchRouterInput({onSearch}: SearchRouterInputProps) { | ||
const styles = useThemeStyles(); | ||
const theme = useTheme(); | ||
|
||
const [value, setValue] = useState(''); | ||
|
||
const onChangeText = (text: string) => { | ||
setValue(text); | ||
onSearch(text); | ||
}; | ||
|
||
return ( | ||
<View style={[]}> | ||
<TextInput | ||
value={value} | ||
onChangeText={onChangeText} | ||
textInputContainerStyles={[{borderWidth: 0}]} | ||
containerStyles={[]} | ||
hideFocusedState | ||
inputStyle={[ | ||
styles.w80, | ||
styles.h13, | ||
styles.ph4, | ||
{ | ||
width: 400, | ||
borderRadius: 8, | ||
borderWidth: 4, | ||
borderColor: theme.borderFocus, | ||
}, | ||
]} | ||
role={CONST.ROLE.PRESENTATION} | ||
/> | ||
</View> | ||
); | ||
} | ||
|
||
SearchRouterInput.displayName = 'SearchRouterInput'; | ||
|
||
export default SearchRouterInput; |
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