let's just ALF the hell out of it
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
import React from 'react'
|
import {forwardRef} from 'react'
|
||||||
import {TextInput, View} from 'react-native'
|
import {
|
||||||
|
type NativeSyntheticEvent,
|
||||||
|
type TextInput,
|
||||||
|
type TextInputKeyPressEventData,
|
||||||
|
View,
|
||||||
|
} from 'react-native'
|
||||||
import {msg} from '@lingui/macro'
|
import {msg} from '@lingui/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
|
|
||||||
@@ -17,14 +22,26 @@ type SearchInputProps = Omit<TextField.InputProps, 'label'> & {
|
|||||||
* Called when the user presses the (X) button
|
* Called when the user presses the (X) button
|
||||||
*/
|
*/
|
||||||
onClearText?: () => void
|
onClearText?: () => void
|
||||||
|
/**
|
||||||
|
* Called when the user presses the Escape key
|
||||||
|
*/
|
||||||
|
onEscape?: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SearchInput = React.forwardRef<TextInput, SearchInputProps>(
|
export const SearchInput = forwardRef<TextInput, SearchInputProps>(
|
||||||
function SearchInput({value, label, onClearText, ...rest}, ref) {
|
function SearchInput({value, label, onClearText, onEscape, ...rest}, ref) {
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const showClear = value && value.length > 0
|
const showClear = value && value.length > 0
|
||||||
|
|
||||||
|
const onKeyPress = (
|
||||||
|
evt: NativeSyntheticEvent<TextInputKeyPressEventData>,
|
||||||
|
) => {
|
||||||
|
if (evt.nativeEvent.key === 'Escape') {
|
||||||
|
onEscape?.()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={[a.w_full, a.relative]}>
|
<View style={[a.w_full, a.relative]}>
|
||||||
<TextField.Root>
|
<TextField.Root>
|
||||||
@@ -42,6 +59,7 @@ export const SearchInput = React.forwardRef<TextInput, SearchInputProps>(
|
|||||||
autoCorrect={false}
|
autoCorrect={false}
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
autoCapitalize="none"
|
autoCapitalize="none"
|
||||||
|
onKeyPress={onKeyPress}
|
||||||
style={[
|
style={[
|
||||||
showClear
|
showClear
|
||||||
? {
|
? {
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import {memo, useCallback, useDeferredValue, useState} from 'react'
|
import {memo, useDeferredValue, useRef, useState} from 'react'
|
||||||
import React from 'react'
|
|
||||||
import {
|
import {
|
||||||
ActivityIndicator,
|
ActivityIndicator,
|
||||||
type StyleProp,
|
type StyleProp,
|
||||||
|
type TextInput,
|
||||||
View,
|
View,
|
||||||
type ViewStyle,
|
type ViewStyle,
|
||||||
} from 'react-native'
|
} from 'react-native'
|
||||||
import {msg} from '@lingui/macro'
|
import {msg, Trans} from '@lingui/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import {StackActions, useNavigation} from '@react-navigation/native'
|
import {StackActions, useNavigation} from '@react-navigation/native'
|
||||||
|
import type React from 'react'
|
||||||
|
|
||||||
import {type NavigationProp} from '#/lib/routes/types'
|
import {type NavigationProp} from '#/lib/routes/types'
|
||||||
import {useModerationOpts} from '#/state/preferences/moderation-opts'
|
import {useModerationOpts} from '#/state/preferences/moderation-opts'
|
||||||
@@ -16,6 +17,7 @@ import {useActorAutocompleteQuery} from '#/state/queries/actor-autocomplete'
|
|||||||
import {SearchProfileCard} from '#/screens/Search/components/SearchProfileCard'
|
import {SearchProfileCard} from '#/screens/Search/components/SearchProfileCard'
|
||||||
import {atoms as a, useTheme} from '#/alf'
|
import {atoms as a, useTheme} from '#/alf'
|
||||||
import {SearchInput} from '#/components/forms/SearchInput'
|
import {SearchInput} from '#/components/forms/SearchInput'
|
||||||
|
import {MagnifyingGlass2_Stroke2_Corner0_Rounded as SearchIcon} from '#/components/icons/MagnifyingGlass2'
|
||||||
import {Link, type LinkProps} from '#/components/Link'
|
import {Link, type LinkProps} from '#/components/Link'
|
||||||
import {Text} from '#/components/Typography'
|
import {Text} from '#/components/Typography'
|
||||||
|
|
||||||
@@ -55,7 +57,9 @@ export function DesktopSearch() {
|
|||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const navigation = useNavigation<NavigationProp>()
|
const navigation = useNavigation<NavigationProp>()
|
||||||
const [isActive, setIsActive] = useState(false)
|
|
||||||
|
const searchInputRef = useRef<TextInput>(null)
|
||||||
|
const [isFocused, setIsFocused] = useState(false)
|
||||||
const [query, setQuery] = useState('')
|
const [query, setQuery] = useState('')
|
||||||
const deferredQuery = useDeferredValue(query)
|
const deferredQuery = useDeferredValue(query)
|
||||||
const {data: autocompleteData, isFetching} = useActorAutocompleteQuery(
|
const {data: autocompleteData, isFetching} = useActorAutocompleteQuery(
|
||||||
@@ -65,52 +69,72 @@ export function DesktopSearch() {
|
|||||||
|
|
||||||
const moderationOpts = useModerationOpts()
|
const moderationOpts = useModerationOpts()
|
||||||
|
|
||||||
const onChangeText = useCallback((text: string) => {
|
const onChangeText = (text: string) => {
|
||||||
setQuery(text)
|
setQuery(text)
|
||||||
setIsActive(text.length > 0)
|
}
|
||||||
}, [])
|
|
||||||
|
|
||||||
const onPressCancelSearch = useCallback(() => {
|
const onPressCancelSearch = () => {
|
||||||
setQuery('')
|
setQuery('')
|
||||||
setIsActive(false)
|
}
|
||||||
}, [setQuery])
|
|
||||||
|
|
||||||
const onSubmit = useCallback(() => {
|
const onEscape = () => {
|
||||||
setIsActive(false)
|
setQuery('')
|
||||||
|
searchInputRef.current?.blur()
|
||||||
|
}
|
||||||
|
|
||||||
|
const onSubmit = () => {
|
||||||
if (!deferredQuery.length) return
|
if (!deferredQuery.length) return
|
||||||
navigation.dispatch(StackActions.push('Search', {q: deferredQuery}))
|
navigation.dispatch(StackActions.push('Search', {q: deferredQuery}))
|
||||||
}, [deferredQuery, navigation])
|
|
||||||
|
|
||||||
const onSearchProfileCardPress = React.useCallback(() => {
|
|
||||||
setQuery('')
|
setQuery('')
|
||||||
setIsActive(false)
|
}
|
||||||
}, [])
|
|
||||||
|
const onSearchProfileCardPress = () => {
|
||||||
|
setQuery('')
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={[a.w_full]}>
|
<View style={[a.w_full, a.z_10]}>
|
||||||
<SearchInput
|
<SearchInput
|
||||||
|
ref={searchInputRef}
|
||||||
value={query}
|
value={query}
|
||||||
onChangeText={onChangeText}
|
onChangeText={onChangeText}
|
||||||
onClearText={onPressCancelSearch}
|
onClearText={onPressCancelSearch}
|
||||||
|
onEscape={onEscape}
|
||||||
onSubmitEditing={onSubmit}
|
onSubmitEditing={onSubmit}
|
||||||
|
onFocus={() => setIsFocused(true)}
|
||||||
|
onBlur={() => setIsFocused(false)}
|
||||||
/>
|
/>
|
||||||
{deferredQuery !== '' && isActive && moderationOpts && (
|
{(deferredQuery !== '' || isFocused) && moderationOpts && (
|
||||||
|
<View style={[a.w_full]}>
|
||||||
<View
|
<View
|
||||||
style={[
|
style={[
|
||||||
t.atoms.bg,
|
t.atoms.bg,
|
||||||
t.atoms.border_contrast_low,
|
t.atoms.border_contrast_low,
|
||||||
a.mt_sm,
|
|
||||||
a.w_full,
|
a.w_full,
|
||||||
a.border,
|
a.border,
|
||||||
|
a.mt_sm,
|
||||||
a.rounded_sm,
|
a.rounded_sm,
|
||||||
a.overflow_hidden,
|
a.overflow_hidden,
|
||||||
a.absolute,
|
a.absolute,
|
||||||
|
a.shadow_lg,
|
||||||
|
a.zoom_fade_in,
|
||||||
]}>
|
]}>
|
||||||
|
{deferredQuery.length === 0 ? (
|
||||||
|
<View style={[a.py_xl, a.gap_sm, a.align_center]}>
|
||||||
|
<SearchIcon size="2xl" style={[t.atoms.text_contrast_low]} />
|
||||||
|
<Text
|
||||||
|
style={[a.text_sm, t.atoms.text_contrast_low, a.text_center]}>
|
||||||
|
<Trans>Start typing to search</Trans>
|
||||||
|
</Text>
|
||||||
|
</View>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
<SearchLinkCard
|
<SearchLinkCard
|
||||||
label={_(msg`Search for "${deferredQuery}"`)}
|
label={_(msg`Search for "${deferredQuery}"`)}
|
||||||
to={{screen: 'Search', params: {q: deferredQuery}}}
|
to={{screen: 'Search', params: {q: deferredQuery}}}
|
||||||
style={[
|
style={[
|
||||||
((autocompleteData?.length ?? 0) > 0 || isFetching) && a.border_b,
|
((autocompleteData?.length ?? 0) > 0 || isFetching) &&
|
||||||
|
a.border_b,
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
{isFetching && !autocompleteData?.length ? (
|
{isFetching && !autocompleteData?.length ? (
|
||||||
@@ -127,6 +151,9 @@ export function DesktopSearch() {
|
|||||||
/>
|
/>
|
||||||
))
|
))
|
||||||
)}
|
)}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
|
|||||||
Reference in New Issue
Block a user