0.3.0 • Published 2 years ago

@dirkpostma/react-native-select v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@dirkpostma/react-native-select

Component to select items from a list for React Native. The input can be a list of any type of objects.

Installation

npm install @dirkpostma/react-native-select

Usage

import React, { useState } from 'react';
import Select from '@dirkpostma/react-native-select'; 

interface Person {
  id: number;
  name: string;
}

const people: Person[] = [
  { id: 1, name: 'Bob' },
  { id: 2, name: 'Alice' },
];

const App = () => {
  const [selectedPeople, setSelectedPeople] = useState<Person[]>([]);

  return (
    <Select
      items={people}
      keyExtractor={(person) => person.id}
      labelExtractor={(person) => person.name}
      onSelect={setSelectedPeople}
      multiselect={true}
      renderItem={({ key, label, onPress, selected }) => (
        <Pressable
          key={key}
          onPress={onPress}
          style={{ 
            padding: 10, 
            borderBottomWidth: 1,
            borderColor: selected ? 'blue' : 'lightgray'
          }}
        >
          <Text>{label}</Text>
        </Pressable>
      )}
    />
  );
};

Properties

PropertyTypeRequiredDescription
itemsT[]YesAn array of objects representing the selectable options.
keyExtractor(item: T) => string | numberYesA function used to extract a unique key for each item in the list.
labelExtractor(item: T) => stringYesA function to extract the label to be displayed for each item.
onSelect(selectedItems: T[]) => voidYesA callback function invoked when the selection changes. Provides an array of the currently selected items.
renderItem(props: RenderItemProps) => React.ReactNodeYesA function that renders each item in the list. See below for RenderItemProps details.
multiselectbooleanNoDetermines whether the select component allows single-select (default: false) or multi-select behavior.

RenderItemProps Interface

PropertyTypeDescription
keystring | numberThe unique key of the item being rendered.
labelstringThe display label of the item.
onPress() => voidFunction to be called when the item is pressed or selected.
selectedbooleanIndicates whether the item is currently selected.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago