Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance search input #1400

Merged
merged 3 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/expo/app/(app)/(drawer)/(tabs)/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default function Search() {
onSelect={handleSearchSelect}
placeholder="Search for a place"
ref={ref}
shouldNavigateBackOnClear={Platform.OS !== 'web'}
/>
</SafeAreaView>
{/* Add search virtual list for feed, packs, trips, places with prop to determine which to display */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import { usePlacesAutoComplete } from './usePlacesAutoComplete';
interface PlacesAutocompleteProps {
onSelect?: (geoJSON: any) => void;
placeholder?: string;
shouldNavigateBackOnClear?: any;
}

export const PlacesAutocomplete = forwardRef<any, PlacesAutocompleteProps>(
function PlacesAutoComplete({ onSelect, placeholder }, ref) {
function PlacesAutoComplete(
{ onSelect, placeholder, shouldNavigateBackOnClear = false },
ref,
) {
const { data, handleSelect, search, setSearch } =
usePlacesAutoComplete(onSelect);
const inputRef = useRef<TextInput>(null);
Expand All @@ -35,6 +39,7 @@ export const PlacesAutocomplete = forwardRef<any, PlacesAutocompleteProps>(
onChange={setSearch}
searchString={search}
ref={inputRef}
shouldNavigateBackOnClear={shouldNavigateBackOnClear}
/>
);
},
Expand Down
25 changes: 22 additions & 3 deletions packages/app/components/SearchInput/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
useState,
} from 'react';
import { Platform, type TextInput } from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { MaterialIcons, MaterialCommunityIcons } from '@expo/vector-icons';
import useSearchInput from './useSearchInput';
import useTheme from 'app/hooks/useTheme';
Expand Down Expand Up @@ -37,6 +38,7 @@ interface SearchInputProps {
placeholder?: string;
canCreateNewItem?: boolean;
resultItemComponent: React.ReactElement;
shouldNavigateBackOnClear?: boolean;
}

export const SearchInput = forwardRef<TextInput, SearchInputProps>(
Expand All @@ -50,11 +52,14 @@ export const SearchInput = forwardRef<TextInput, SearchInputProps>(
onChange,
searchString,
canCreateNewItem = false,
shouldNavigateBackOnClear = false,
},
inputRef,
) {
const inputContainerRef = useRef<HTMLDivElement | null>(null);
const [inputWidth, setInputWidth] = useState<number | undefined>(undefined);
const [isFocused, setIsFocused] = useState(false);
const navigation = Platform.OS === 'web' ? null : useNavigation();

useLayoutEffect(() => {
if (inputContainerRef.current) {
Expand All @@ -74,6 +79,16 @@ export const SearchInput = forwardRef<TextInput, SearchInputProps>(
const options = useSearchOptions(results, searchString, canCreateNewItem);
const { isDark, currentTheme } = useTheme();

const handleFocus = () => setIsFocused(true);
const handleBlur = () => setIsFocused(false);

const handleClearSearchWithOptionalNavigation = () => {
handleClearSearch();
if (shouldNavigateBackOnClear && navigation?.canGoBack()) {
navigation.goBack();
}
};

if (Platform.OS === 'web') {
return (
<Popover size="$20" allowFlip placement="bottom" open={isVisible}>
Expand All @@ -93,8 +108,10 @@ export const SearchInput = forwardRef<TextInput, SearchInputProps>(
placeholder={placeholder ?? 'Search'}
value={searchString}
onChange={(e) => handleSearchChange(e.target.value)}
onFocus={handleFocus}
onBlur={handleBlur}
/>
{searchString && (
{(searchString || isFocused) && (
<MaterialIcons
name="close"
style={{
Expand Down Expand Up @@ -192,6 +209,8 @@ export const SearchInput = forwardRef<TextInput, SearchInputProps>(
lightTheme
showCancel={true}
selectTextOnFocus
onFocus={handleFocus}
onBlur={handleBlur}
style={{
backgroundColor: 'transparent',
borderRightWidth: 0,
Expand All @@ -205,9 +224,9 @@ export const SearchInput = forwardRef<TextInput, SearchInputProps>(
fontSize: 20,
}}
/>
{searchString && searchString.trim().length > 0 && (
{(searchString || isFocused) && (
<RIconButton
onPress={handleClearSearch}
onPress={handleClearSearchWithOptionalNavigation}
style={{ backgroundColor: 'transparent' }}
>
<MaterialIcons
Expand Down
Loading