Recent Searches
A native search section with reusable history, explicit per-search removal, and a clear history action.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add recent-searchesSource files
Expand a file to read the source the installer copies into your app.
components/blocks/recent-searches/RecentSearches.tsx
import * as React from 'react'
import { Keyboard, View, type ViewProps } from 'react-native'
import { X } from 'lucide-react-native'
import { Button } from '@/components/ui/button'
import { IconButton } from '@/components/ui/icon-button'
import { List, ListItem, ListItemContent, ListItemTitle, ListSeparator } from '@/components/ui/list'
import { SearchBar } from '@/components/ui/search-bar'
import { Text } from '@/components/ui/text'
import { cn } from '@/lib/utils'
const defaultLabels = {
field: 'Search',
placeholder: 'Enter a search',
submit: 'Search',
clearQuery: 'Clear search',
history: 'Recent searches',
clearHistory: 'Clear history',
empty: 'No recent searches.',
remove: (query: string) => `Remove “${query}” from recent searches`,
}
export interface RecentSearch {
id: string
query: string
}
export interface RecentSearchesProps extends Omit<ViewProps, 'children'> {
className?: string
/** Caller-owned history, in display order, with stable unique IDs. */
history?: readonly RecentSearch[]
query?: string
defaultQuery?: string
onQueryChange?: (query: string) => void
/** Called for a submitted search or a separately selected history row. */
onSearch: (query: string) => void
onRemove?: (id: string) => void
onClearHistory?: () => void
labels?: Partial<typeof defaultLabels>
}
export function RecentSearches({
className,
history = [],
query,
defaultQuery = '',
onQueryChange,
onSearch,
onRemove,
onClearHistory,
labels,
...props
}: RecentSearchesProps) {
const copy = { ...defaultLabels, ...labels }
const [localQuery, setLocalQuery] = React.useState(defaultQuery)
const value = query ?? localQuery
const canSubmit = !!value.trim()
const entries = history.filter((entry) => entry.query.trim())
function change(next: string) {
if (query === undefined) setLocalQuery(next)
onQueryChange?.(next)
}
function submit(next: string) {
const term = next.trim()
if (!term) return
if (term !== value) change(term)
Keyboard.dismiss()
onSearch(term)
}
return (
<View {...props} className={cn('gap-4', className)}>
<View className="gap-2">
<SearchBar
value={value}
accessibilityLabel={copy.field}
placeholder={copy.placeholder}
clearLabel={copy.clearQuery}
onChangeText={change}
submitBehavior="blurAndSubmit"
onSubmitEditing={(event) => submit(event.nativeEvent.text)}
/>
<Button accessibilityLabel={copy.submit} disabled={!canSubmit} onPress={() => submit(value)}>
{copy.submit}
</Button>
</View>
<View className="gap-2">
<View className="min-h-[48px] flex-row flex-wrap items-center justify-between gap-x-3">
<Text accessibilityRole="header" className="font-semibold">
{copy.history}
</Text>
{entries.length > 0 && onClearHistory ? (
<Button variant="ghost" accessibilityLabel={copy.clearHistory} onPress={onClearHistory}>
{copy.clearHistory}
</Button>
) : null}
</View>
{entries.length ? (
<List>
{entries.map((entry, index) => (
<React.Fragment key={entry.id}>
{index > 0 ? <ListSeparator /> : null}
<View className="flex-row items-center">
<ListItem
accessibilityLabel={entry.query}
onPress={() => submit(entry.query)}
className="min-w-0 flex-1"
>
<ListItemContent>
<ListItemTitle>{entry.query}</ListItemTitle>
</ListItemContent>
</ListItem>
{onRemove ? (
<IconButton
icon={X}
accessibilityLabel={copy.remove(entry.query)}
onPress={() => onRemove(entry.id)}
className="mr-1"
/>
) : null}
</View>
</React.Fragment>
))}
</List>
) : (
<Text size="sm" variant="muted">
{copy.empty}
</Text>
)}
</View>
</View>
)
}
components/blocks/recent-searches/index.ts
export { RecentSearches, type RecentSearch, type RecentSearchesProps } from './RecentSearches'
Dependencies
Registry items
npm packages
lucide-react-native