Filter Chip Group
Finite, composable multi-select filter chips with controlled and uncontrolled modes and a reset action for editable choices.
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 filter-chip-groupSource files
Expand a file to read the source the installer copies into your app.
components/ui/filter-chip-group/FilterChipGroup.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Button, type ButtonProps } from '@/components/ui/button'
import { Chip, type ChipProps } from '@/components/ui/chip'
import { cn } from '@/lib/utils'
export interface FilterChipGroupProps extends ViewProps {
className?: string
value?: readonly string[]
defaultValue?: readonly string[]
onValueChange?: (value: string[]) => void
disabled?: boolean
}
export interface FilterChipProps extends Omit<
ChipProps,
'selected' | 'onPress' | 'onLongPress' | 'onRemove' | 'removeLabel'
> {
/** Unique filter value among direct children and fragments of the group. */
value: string
}
interface GroupContext {
disabled: boolean
value: readonly string[]
canReset: boolean
toggle: (value: string) => void
reset: () => void
}
const FilterContext = React.createContext<GroupContext | null>(null)
function useFilters() {
const context = React.useContext(FilterContext)
if (!context) throw new Error('Filter chip controls must be inside FilterChipGroup')
return context
}
function collectChoices(children: React.ReactNode, choices = new Map<string, boolean>()) {
React.Children.forEach(children, (child) => {
if (!React.isValidElement(child)) return
if (child.type === React.Fragment) {
collectChoices((child.props as { children?: React.ReactNode }).children, choices)
} else if (child.type === FilterChip) {
const props = child.props as FilterChipProps
choices.set(props.value, !!props.disabled)
}
})
return choices
}
function normalize(values: readonly string[], choices: ReadonlyMap<string, boolean>) {
return [...new Set(values.filter((value) => choices.has(value)))]
}
export function FilterChipGroup({
className,
value,
defaultValue = [],
onValueChange,
disabled = false,
children,
...props
}: FilterChipGroupProps) {
const choices = React.useMemo(() => collectChoices(children), [children])
const [localValue, setLocalValue] = React.useState(() => normalize(defaultValue, choices))
const currentLocal = React.useRef(localValue)
const controlled = value !== undefined
const blocked = disabled || (controlled && !onValueChange)
const selection = normalize(value ?? localValue, choices)
React.useEffect(() => {
if (controlled) return
const previous = currentLocal.current
const next = normalize(previous, choices)
if (next.length === previous.length) return
currentLocal.current = next
setLocalValue(next)
onValueChange?.([...next])
}, [choices, controlled, onValueChange])
function change(next: string[]) {
if (!controlled) {
currentLocal.current = next
setLocalValue(next)
}
onValueChange?.([...next])
}
return (
<FilterContext.Provider
value={{
disabled: blocked,
value: selection,
canReset: !blocked && selection.some((filter) => !choices.get(filter)),
toggle(filter) {
if (blocked || !choices.has(filter) || choices.get(filter)) return
const current = normalize(value ?? currentLocal.current, choices)
change(current.includes(filter) ? current.filter((active) => active !== filter) : [...current, filter])
},
reset() {
if (blocked) return
const current = normalize(value ?? currentLocal.current, choices)
const next = current.filter((filter) => choices.get(filter))
if (next.length !== current.length) change(next)
},
}}
>
<View {...props} className={cn('flex-row flex-wrap items-center gap-2', className)}>
{children}
</View>
</FilterContext.Provider>
)
}
export function FilterChip({ value, disabled, ...props }: FilterChipProps) {
const group = useFilters()
return (
<Chip
{...props}
selected={group.value.includes(value)}
disabled={group.disabled || disabled}
onPress={() => group.toggle(value)}
/>
)
}
export function FilterChipReset({ disabled, onPress, children = 'Clear filters', ...props }: ButtonProps) {
const group = useFilters()
const blocked = !!disabled || !group.canReset
return (
<Button
variant="ghost"
size="sm"
{...props}
disabled={blocked}
onPress={(event) => {
if (blocked) return
onPress?.(event)
if (!event?.isDefaultPrevented()) group.reset()
}}
>
{children}
</Button>
)
}
components/ui/filter-chip-group/index.ts
export {
FilterChipGroup,
FilterChip,
FilterChipReset,
type FilterChipGroupProps,
type FilterChipProps,
} from './FilterChipGroup'