UIPackage React Native
Search
Menu

Search Bar

Search input with controlled or default text, clear action, keyboard submit props, and themed focus/error states.

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 search-bar

Source files

Expand a file to read the source the installer copies into your app.

components/ui/search-bar/SearchBar.tsx
import * as React from 'react'
import { TextInput, View, type ViewProps } from 'react-native'
import { Search, X } from 'lucide-react-native'
import { Input, type InputProps } from '@/components/ui/input'
import { IconButton } from '@/components/ui/icon-button'
import { useThemeColors } from '@/lib/theme'
import { cn } from '@/lib/utils'

export interface SearchBarProps extends Omit<InputProps, 'prefixIcon' | 'suffixIcon' | 'containerClassName' | 'style'> {
  inputClassName?: string
  clearLabel?: string
  style?: ViewProps['style']
}

export const SearchBar = React.forwardRef<React.ComponentRef<typeof TextInput>, SearchBarProps>(
  (
    {
      value,
      defaultValue = '',
      onChangeText,
      className,
      inputClassName,
      style,
      clearLabel = 'Clear search',
      editable = true,
      invalid,
      placeholder = 'Search',
      accessibilityLabel = 'Search',
      autoCapitalize = 'none',
      autoCorrect = false,
      returnKeyType = 'search',
      onFocus,
      onBlur,
      ...props
    },
    ref,
  ) => {
    const colors = useThemeColors()
    const [internal, setInternal] = React.useState(defaultValue)
    const [focused, setFocused] = React.useState(false)
    const input = React.useRef<React.ComponentRef<typeof TextInput>>(null)
    const resolved = value ?? internal
    const canEdit = editable && !props.readOnly
    const setRef = React.useCallback(
      (node: React.ComponentRef<typeof TextInput> | null) => {
        input.current = node
        if (typeof ref === 'function') return ref(node)
        if (ref) ref.current = node
      },
      [ref],
    )
    function change(next: string) {
      if (!canEdit) return
      if (value === undefined) setInternal(next)
      onChangeText?.(next)
    }

    return (
      <View
        className={cn(
          'flex-row items-center rounded-md border border-muted-foreground bg-background',
          focused && !invalid && 'border-ring bg-muted',
          invalid && 'border-destructive',
          !editable && 'opacity-50',
          className,
        )}
        style={style}
      >
        <View className="min-w-0 flex-1">
          <Input
            ref={setRef}
            {...props}
            value={resolved}
            onChangeText={change}
            editable={editable}
            invalid={invalid}
            placeholder={placeholder}
            accessibilityLabel={accessibilityLabel}
            autoCapitalize={autoCapitalize}
            autoCorrect={autoCorrect}
            returnKeyType={returnKeyType}
            prefixIcon={<Search size={20} color={colors.mutedForeground} aria-hidden />}
            containerClassName="border-0 opacity-100"
            className={inputClassName}
            onFocus={(event) => {
              setFocused(true)
              onFocus?.(event)
            }}
            onBlur={(event) => {
              setFocused(false)
              onBlur?.(event)
            }}
          />
        </View>
        {resolved && canEdit ? (
          <IconButton
            icon={X}
            accessibilityLabel={clearLabel}
            onPress={() => {
              change('')
              input.current?.focus()
            }}
          />
        ) : null}
      </View>
    )
  },
)
SearchBar.displayName = 'SearchBar'
components/ui/search-bar/index.ts
export * from './SearchBar'

Dependencies

Registry items

npm packages

  • lucide-react-native
Browse controls and interactions