Sort Filter Sheet
Bottom sheet for drafting sort and filter changes, with explicit clear and apply controls.
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 sort-filter-sheetSource files
Expand a file to read the source the installer copies into your app.
components/blocks/sort-filter-sheet/SortFilterSheet.tsx
import { useState } from 'react'
import { View } from 'react-native'
import { Button } from '@/components/ui/button'
import { FilterChip, FilterChipGroup } from '@/components/ui/filter-chip-group'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { Sheet, SheetClose, SheetContent, SheetTrigger } from '@/components/ui/sheet'
import { Text } from '@/components/ui/text'
export interface SortFilterOption {
id: string
label: string
}
export interface SortFilterValue {
sort: string | null
filters: readonly string[]
}
export interface SortFilterSheetProps {
value: SortFilterValue
onApply: (value: SortFilterValue) => void
/** IDs must be nonempty and unique within each option list. */
sorts?: readonly SortFilterOption[]
filters?: readonly SortFilterOption[]
/** Clear all resets to this sort. Defaults to the first available sort. */
defaultSort?: string
title?: string
closeLabel?: string
labels?: Partial<Record<'trigger' | 'sort' | 'filters' | 'apply' | 'clear', string>>
}
export function SortFilterSheet({
value,
onApply,
sorts = [],
filters = [],
defaultSort,
title = 'Sort and filter',
closeLabel = 'Close filters',
labels,
}: SortFilterSheetProps) {
const [draft, setDraft] = useState<SortFilterValue | null>(null)
const defaultSortId = sorts.find((option) => option.id === defaultSort)?.id ?? sorts[0]?.id ?? null
const sortLabel = labels?.sort ?? 'Sort by'
const filtersLabel = labels?.filters ?? 'Filters'
return (
<Sheet
open={draft !== null}
onOpenChange={(open) =>
setDraft(
open
? {
sort: sorts.some((option) => option.id === value.sort) ? value.sort : defaultSortId,
filters: [...value.filters],
}
: null,
)
}
>
<SheetTrigger
variant="outline"
disabled={sorts.length === 0 && filters.length === 0}
accessibilityLabel={labels?.trigger ?? title}
>
{labels?.trigger ?? title}
</SheetTrigger>
<SheetContent title={title} closeLabel={closeLabel}>
{draft ? (
<>
{sorts.length > 0 ? (
<View className="gap-[4px]">
<Text size="sm" className="font-semibold" accessibilityRole="header">
{sortLabel}
</Text>
<RadioGroup
value={draft.sort ?? ''}
accessibilityLabel={sortLabel}
onValueChange={(sort) => setDraft((current) => current && { ...current, sort })}
className="gap-0"
>
{sorts.map(({ id, label }) => (
<RadioGroupItem key={id} value={id} label={label} />
))}
</RadioGroup>
</View>
) : null}
{filters.length > 0 ? (
<View className="gap-[8px]">
<Text size="sm" className="font-semibold" accessibilityRole="header">
{filtersLabel}
</Text>
<FilterChipGroup
value={draft.filters}
onValueChange={(selection) => setDraft((current) => current && { ...current, filters: selection })}
>
{filters.map(({ id, label }) => (
<FilterChip key={id} value={id} label={label} />
))}
</FilterChipGroup>
</View>
) : null}
<View className="gap-[8px] border-t border-border pt-[16px]">
<SheetClose
accessibilityLabel={labels?.apply ?? 'Apply'}
onPress={() =>
onApply({
sort: sorts.some((option) => option.id === draft.sort) ? draft.sort : defaultSortId,
filters: [...new Set(draft.filters.filter((id) => filters.some((option) => option.id === id)))],
})
}
>
{labels?.apply ?? 'Apply'}
</SheetClose>
<Button
variant="ghost"
accessibilityLabel={labels?.clear ?? 'Clear all'}
onPress={() => setDraft({ sort: defaultSortId, filters: [] })}
>
{labels?.clear ?? 'Clear all'}
</Button>
</View>
</>
) : null}
</SheetContent>
</Sheet>
)
}
components/blocks/sort-filter-sheet/index.ts
export {
SortFilterSheet,
type SortFilterOption,
type SortFilterSheetProps,
type SortFilterValue,
} from './SortFilterSheet'