npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rn-select2

v0.1.9

Published

A highly customizable and feature-rich Select component for React Native applications.

Downloads

676

Readme

NPM

npm version GitHub stars CodeQL Release & Publish to NPM

RN-Select2 (React Native select2)

rn-select2 is a powerful and flexible dropdown component for React Native, inspired by the popular Select2 jQuery plugin. It provides a seamless and intuitive selection experience for both single and multi-select scenarios.

Features

  • Single and multi-select support
  • Grouped and non-grouped data structures
  • Search functionality with highlighting
  • Customizable styling for all sub-components
  • Smooth animations for a polished user experience
  • Clear button for easy reset of selections
  • Fully typed with TypeScript for improved development experience

Demo

RN-Select2 offers a rich set of features for both single and multi-select scenarios. Here's a visual demonstration of its capabilities:

Image

| | | | |---------|---------------------------------------------------------------------------------------------------------------------------------------------------|---| | Single Selection | Multi Selection | Search Highlighting | | Flat List View | Custom Styling | |

Video

| Android | iOS | |------------------------|-----| | | |

Table of Contents

Installation

To install the package, run:

npm install rn-select2
# or
yarn add rn-select2

This package has a peer dependency on react-native-reanimated. If you haven't already installed it, you'll need to add it to your project:

npm install react-native-reanimated
# or
yarn add react-native-reanimated

Follow the react-native-reanimated installation instructions to complete the setup.

Basic Usage

Here's a simple example of how to use RN-Select2:

import React, { useState } from 'react';
import { View } from 'react-native';
import RNSelect2 from 'rn-select2';

const countries = {
  type: 'list',
  data: [
    { value: 'us', label: 'United States' },
    { value: 'ca', label: 'Canada' },
    { value: 'mx', label: 'Mexico' },
  ]
};

const App = () => {
  const [selectedCountry, setSelectedCountry] = useState(null);

  return (
    <View style={{ padding: 20 }}>
      <RNSelect2
        data={countries}
        onSelect={(item) => setSelectedCountry(item)}
        placeholder="Select a country"
      />
    </View>
  );
};

export default App;

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | data | RNSelect2Data | Required | The data to populate the select component. Can be grouped or non-grouped. | | onSelect | (item: RNSelect2BaseItem \| RNSelect2BaseItem[] \| null) => void | Required | Callback function called when an item is selected or deselected. The type of the argument depends on the multiSelect prop (see note below). | | placeholder | string | 'Select an item...' | Placeholder text displayed when no item is selected. | | multiSelect | boolean | false | Enable multi-select mode. | | styles | RNSelect2Styles | {} | Custom styles for various parts of the component. See Styling section for details. | | searchPlaceholder | string | 'Search...' | Placeholder text for the search input. | | closeOnSelect | boolean | true | Whether to close the dropdown after an item is selected (only applies to single select mode). | | disabled | boolean | false | Disable the select component. |

Note on onSelect:

  • When multiSelect is false, the callback receives a single RNSelect2BaseItem object or null (when selection is cleared).
  • When multiSelect is true, the callback receives an array of RNSelect2BaseItem objects (which may be empty when all selections are cleared).

Data Structure

The data prop should be an object with the following structure:

interface RNSelect2Data {
  type: 'list' | 'group';
  data: RNSelect2BaseItem[] | RNSelect2Group[];
}

interface RNSelect2BaseItem {
  value: string;
  label: string;
}

interface RNSelect2Group {
  title: string;
  items: RNSelect2BaseItem[];
}

Styling

You can customize the appearance of RN-Select2 by passing a styles prop. Here's the structure with expected types:

interface RNSelect2Styles {
  container?: ViewStyle;
  topBar?: {
    container?: ViewStyle;
    text?: TextStyle;
    placeholder?: TextStyle;
    arrow?: TextStyle;
    clearButton?: TextStyle;
  };
  searchBar?: {
    container?: ViewStyle;
    input?: TextStyle;
    clearButton?: TextStyle;
  };
  dropdown?: {
    container?: ViewStyle;
  };
  listItem?: {
    container?: ViewStyle;
    text?: TextStyle;
  };
  listItemHeader?: {
    container?: ViewStyle;
    text?: TextStyle;
  };
  tag?: {
    container?: ViewStyle;
    text?: TextStyle;
    removeIcon?: TextStyle;
  };
}

This structure allows you to customize each part of the component individually. All style properties are optional, so you can choose to style only specific parts of the component.

Examples

Grouped Data with Multi-Select

const groupedData = {
  type: 'group',
  data: [
    {
      title: 'North America',
      items: [
        { value: 'us', label: 'United States' },
        { value: 'ca', label: 'Canada' },
        { value: 'mx', label: 'Mexico' },
      ]
    },
    {
      title: 'Europe',
      items: [
        { value: 'uk', label: 'United Kingdom' },
        { value: 'fr', label: 'France' },
        { value: 'de', label: 'Germany' },
      ]
    }
  ]
};

<RNSelect2
  data={groupedData}
  onSelect={(items) => console.log('Selected:', items)}
  placeholder="Select countries"
  multiSelect={true}
/>

Custom Styling

const customStyles = {
  topBar: {
    container: { backgroundColor: '#f0f0f0', borderRadius: 8 },
    text: { color: '#333', fontSize: 16 },
    placeholder: { color: '#999' },
    arrow: { color: '#666' }
  },
  listItem: {
    container: { borderBottomWidth: 1, borderBottomColor: '#eee' },
    text: { fontSize: 15 }
  },
  tag: {
    container: { backgroundColor: '#007AFF', borderRadius: 16 },
    text: { color: 'white' },
    removeIcon: { color: 'white' }
  }
};

<RNSelect2
  data={data}
  onSelect={handleSelect}
  placeholder="Custom styled select"
  styles={customStyles}
/>

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.