Coder Social home page Coder Social logo

bvaughn / react-virtualized-select Goto Github PK

View Code? Open in Web Editor NEW
1.2K 21.0 169.0 4.31 MB

HOC that uses react-virtualized and react-select to display large lists of options in a drop-down

Home Page: https://bvaughn.github.io/react-virtualized-select/

License: MIT License

JavaScript 94.48% HTML 0.92% CSS 4.60%

react-virtualized-select's Introduction

This component is no longer supported

Hello! This package is built on react-virtualized (a library that I no longer support) and vesion 1.0 of react-select (which is no longer the current version). As such, I've decided to stop supporting this package. GitHub issues and pull requests may be ignored.

If you are interested in taking over maintenance of this package, please send me an email (see my GitHub profile) and I'd be happy to add you as a collaborator.

React Virtualized Select

NPM version NPM license NPM total downloads NPM monthly downloads PayPal donate button Patreon donate button

react-virtualized-select example

Getting started

Install react-virtualized-select using npm.

npm install react-virtualized-select --save

ES6, CommonJS, and UMD builds are available with each distribution. For example:

// Make sure to import default styles.
// This only needs to be done once; probably during bootstrapping process.
import 'react-select/dist/react-select.css'
import 'react-virtualized-select/styles.css'

// Then import the virtualized Select HOC
import VirtualizedSelect from 'react-virtualized-select'

Alternately you can load a global-friendly UMD build:

<link rel="stylesheet" href="path-to-react-select/dist/react-select.css">
<link rel="stylesheet" href="path-to-react-virtualized/styles.css">
<link rel="stylesheet" href="path-to-react-virtualized-select/styles.css">

<script src="path-to-react-virtualized-select/dist/umd/react-virtualized-select.js"></script>

Example

react-select-virtualized works just like react-select. You pass it an array of options, along with almost any other parameters supported by the Select component.

Try this example in Code Sandbox.

// Import default styles.
// This only needs to be done once; probably during bootstrapping process.
import "react-select/dist/react-select.css";
import "react-virtualized-select/styles.css";

import React from "react";
import ReactDOM from "react-dom";
import Select from "react-virtualized-select";

// Dummy array of test values.
const options = Array.from(new Array(1000), (_, index) => ({
  label: `Item ${index}`,
  value: index
}));

ReactDOM.render(
  <Select options={options} />,
  document.getElementById("root")
);

React Virtualized Select Props

The additional parameters introduced by react-select-virtualized are optional. They are:

Property Type Description
async PropTypes.bool Use Select.Async internally; if this property is specified then a loadOptions method should also be used.
maxHeight PropTypes.number Max height of options menu; defaults to 200 pixels.
optionHeight PropTypes.number or PropTypes.func Option height (defaults to 35 pixels). Dynamic height can be supported via a function with the signature ({ option: Object }): number
optionRenderer PropTypes.func Custom option renderer; (see below for signature).
selectComponent PropTypes.func Use a specific select HOC (eg Select, Select.Creatable, Select.Async or Select.AsyncCreatable); defaults to Select (or Select.Async if async flag is true).

Unsupported props

optionComponent is not supported for react-select-virtualized; optionRenderer must be used instead, see below for usage.

Custom Option Renderer

You can override the built-in option renderer by specifying your own optionRenderer property. Your renderer should return a React element that represents the specified option. It will be passed the following named parameters:

Property Type Description
focusedOption Object The option currently-focused in the dropdown. Use this property to determine if your rendered option should be highlighted or styled differently.
focusedOptionIndex number Index of the currently-focused option.
focusOption Function Callback to update the focused option; for example, you may want to call this function on mouse-over.
key string A unique identifier for each element created by the renderer.
labelKey string Attribute of option that contains the display text.
option Object The option to be rendered.
options Array<Object> Array of options (objects) contained in the select menu.
selectValue Function Callback to update the selected values; for example, you may want to call this function on click.
style Object Styles that must be passed to the rendered option. These styles are specifying the position of each option (required for correct option displaying in the dropdown).
valueArray Array<Object> Array of the currently-selected options. Use this property to determine if your rendered option should be highlighted or styled differently.
valueKey string Attribute of option that contains the value.

optionRenderer example

It should be noted that in order to successfully set the active index in your custom renderer, you need to call the selectValue prop. A common pattern is to bind onto your onClick handler in your custom element. The example that follows also provides the required style prop (as noted above), which is necessary to properly position the element. Refer to the full example for the complete usage.

function Option({
  style,
  option,
  selectValue,
}) {
  return (
    <a
      style={style}
      onClick={() => selectValue(option)}
    >
      {option.value}
    </a>
  );
}

react-virtualized-select's People

Contributors

andrewmartin avatar armed avatar bvaughn avatar denisborovikov avatar geirsagberg avatar jakeboone02 avatar kant avatar leojh avatar maksimdegtyarev avatar mbrevda avatar nerlin avatar npmcdn-to-unpkg-bot avatar rgrove avatar seanfrisbey avatar shockitv avatar syynth avatar the-spyke avatar tomaswitek avatar turnerniles avatar yuriyyakym avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.