Code Monkey home page Code Monkey logo

custom-reactjs-dropdown-components's Introduction

This package features two custom dropdown menu components for ReactJS.

Online demo

Single-selection Multi-selection
dd-single dd-multiple

Installation

npm install reactjs-dropdown-component --save

Make sure that you inserted the following link tag between the <head></head> tags inside /public/index.html of your react project. This is required for the FontAwesome component that the package depends on.

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

Usage

First, import the components:

import {DropdownMultiple, Dropdown} from 'reactjs-dropdown-component';

The structure of the state for the dropdown data should be as follows:

state = {
  location: [
    {
      id: 0,
      title: 'New York',
      selected: false,
      key: 'location'
    },
    {
      id: 1,
      title: 'Dublin',
      selected: false,
      key: 'location'
    },
    {
      id: 2,
      title: 'Istanbul',
      selected: false,
      key: 'location'
    }
  ],
  fruit: [
    {
      id: 0,
      title: 'Apple',
      selected: false,
      key: 'fruit'
    },
    {
      id: 1,
      title: 'Orange',
      selected: false,
      key: 'fruit'
    },
    {
      id: 2,
      title: 'Strawberry',
      selected: false,
      key: 'fruit'
    }
  ]
}

Then you need to include a function to control the state of the parent component.

This is for the single selection dropdown:

resetThenSet = (id, key) => {
  let temp = JSON.parse(JSON.stringify(this.state[key]));
  temp.forEach(item => item.selected = false);
  temp[id].selected = true;
  this.setState({
    [key]: temp
  });
}

And this is for the multi selection dropdown:

toggleSelected = (id, key) => {
  let temp = JSON.parse(JSON.stringify(this.state[key]));
  temp[id].selected = !temp[id].selected;
  this.setState({
    [key]: temp
  });
}

Finally use the components as follows:

<Dropdown
  title="Select fruit"
  list={this.state.fruit}
  resetThenSet={this.resetThenSet}
/>

<DropdownMultiple
  titleHelper="Location"
  title="Select location"
  list={this.state.location}
  toggleItem={this.toggleSelected}
/>

Refer to the GitHub repository for the full code example.

Custom Styling

Refer to the following styling file for overriding the default styles. You can create your own styling file with the same class names in order to do your custom styling.

custom-reactjs-dropdown-components's People

Watchers

 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.