Mobilens 中文

Control · Inputs

Search bar

Every state

When
Behaviour

    Usage

    import { useState } from 'react';
    import { Text, View } from 'react-native';
    import { SearchBar } from '@/components/mobilens/SearchBar';
    
    export function ContactSearch({ names }: { names: string[] }) {
      const [query, setQuery] = useState('');
      const term = query.trim().toLocaleLowerCase();
      const matches = names.filter((name) => name.toLocaleLowerCase().includes(term));
      return (
        <View>
          <SearchBar value={query} onChangeText={setQuery} placeholder="Search contacts" />
          {matches.map((name, index) => <Text key={name + index}>{name}</Text>)}
          {!matches.length && <Text>No matching contacts</Text>}
        </View>
      );
    }
    

    API

    PropTypeDefaultDescription
    value string – Current query. An empty string represents all results.
    onChangeText (value: string) => void – Called for typing, paste, clear and cancel. Filter results using the new value.
    placeholder? string 'Search' Search hint and accessible name, for example "Search contacts".
    onSubmit? (query: string) => void – Receives the trimmed query when the user presses Search on the keyboard.
    onCancel? () => void – Enables the cancel action, called after clearing the query and dismissing the keyboard.
    clearLabel? string 'Clear search' Accessible name of the clear button.
    cancelLabel? string 'Cancel' Visible label for the cancel action.
    disabled? boolean false Prevents edits while still displaying the current query.
    autoFocus? boolean false Focus the field when it mounts.