Code Monkey home page Code Monkey logo

react-dropdown-input's Introduction

npm package

React-Dropdown-Input

React-dropdown-input displays a "combobox" for React. More explicitly, it shows a text input box; once the user starts to type, a dropdown menu opens with matching options. The user can choose one of those options either by clicking one, or by using the arrow keys and hitting Enter.

It is styled with bootstrap, using the React-Bootstrap package; it actually displays a ReactBootstrap.Input element with a ReactBootstrap.DropdownMenu of possible options.

Demo

http://racingtadpole.github.io/react-dropdown-input/example/

Installation

npm install react-dropdown-input --save

Sample Usage

var searchNames = ['Sydney', 'Melbourne', 'Brisbane', 
    'Adelaide', 'Perth', 'Hobart'];
//...
<DropdownInput 
    options={searchNames}
    defaultValue={this.props.initialValue}
    menuClassName='dropdown-input'
    onSelect={this.handleSelectName}
    placeholder='Search...'
/>

In more detail...

The options are simply passed as a javascript array (or immutablejs object) to the options prop.

Supply one or both of these callbacks: onSelect & onChange.

  • onSelect fires when an option is clicked, or when Enter is pressed. It passes the object:

      { value: input text,
        index: option index, or -1 if user entered their own text and pressed Enter
      }
    
  • onChange fires whenever the input text value changes, either due to a click or typing. It passes the object:

      { value: input text }
    

Other props you can pass:

  • filter: a function that determines which options to show, given the input text (see defaultFilter in the code for the default).
  • menuClassName: a class for the menu, which you need for the css styling below; eg. 'dropdown-input'.
  • max: the maximum number of options to display.
  • maxText: text of a disabled MenuItem to show at the end of a list, if the max is exceeded replaces '#' with the number not shown; defaults to '+# more not shown'.

You can also pass <DropdownInput> all the properties that <ReactBootstrap.Input> allows, eg. ButtonAfter.

IMPORTANT NOTE ABOUT CSS

You need to turn off Bootstrap's hover highlighting css for this element; we do it manually using the active class instead. You may also need to re-enable the hover highlighting on the active class. Eg. in sass, add this:

.dropdown-input.dropdown-menu > li > a {
  &:hover,
  &:focus {
    color: $dropdown-link-color;  // #333
    background-color: $dropdown-bg; // #fff
  }
}
.dropdown-input.dropdown-menu > .active > a {
  &:hover,
  &:focus {
    text-decoration: none;
    color: $dropdown-link-hover-color;  // #fff
    background-color: $dropdown-link-hover-bg;  // #337ab7
  }
}

If you're showing maxText, you may also want to make sure it can't be selected too:

.dropdown-input.dropdown-menu>.active.disabled>a {
  text-decoration: none;
  color: $dropdown-link-disabled-color; // #777
  background-color: $dropdown-bg; // #fff
}

Release History

  • 0.1.0 Initial release
  • 0.1.1 Point to js (not jsx), update README
  • 0.1.2 Update example
  • 0.1.3 Align package.json version number with git tag
  • 0.1.4 Added maxText property
  • 0.1.5 Added eslint to dev
  • 0.1.6 Corrected number not shown
  • 0.1.7 Don't pass options and menuClassName props through to Input
  • 0.1.8 Added working tests using jest
  • 0.1.9 Use self-closing tag in ReadMe
  • 0.1.10 Remove extra comments, rename var as DropdownInput
  • 0.1.11 Prevent form submission if open

react-dropdown-input's People

Contributors

karlguillotte avatar racingtadpole avatar

Stargazers

 avatar

Watchers

 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.