Giter VIP home page Giter VIP logo

custom-reactjs-dropdown-components's Introduction

This package features two custom dropdown menu components for ReactJS.

WARNING: Breaking changes take effect from version 1.1.7. If you are using any of the earlier versions, refer to the previous README files.

Online demo

Single-selection Multi-selection
Single-selection searchable Multi-selection searchable

Installation

npm install --save reactjs-dropdown-component

Usage

Import the component you want to use;

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

If you are using NextJS, import them dynamically instead;

import dynamic from 'next/dynamic';

const Dropdown = dynamic(
  async () => {
    const module = await import('reactjs-dropdown-component');
    const DD = module.Dropdown;

    return ({ forwardedRef, ...props }) => <DD ref={forwardedRef} {...props} />;
  },
  { ssr: false },
);

const DropdownMultiple = dynamic(
  async () => {
    const module = await import('reactjs-dropdown-component');
    const DDM = module.DropdownMultiple;

    return ({ forwardedRef, ...props }) => <DDM ref={forwardedRef} {...props} />;
  },
  { ssr: false },
);

The shape of the objects in the data array should be as follows:

const locations = [
  {
    label: 'New York',
    value: 'newYork',
  },
  {
    label: 'Oslo',
    value: 'oslo',
  },
  {
    label: 'Istanbul',
    value: 'istanbul',
  }
];

Use a function to pass to onChange prop. For <Dropdown> component item is an object, whereas for <DropdownMultiple> it is an array of objects.

  onChange = (item, name) => {
    ...
  }

Finally use the components as follows:

<Dropdown
  name="location"
  title="Select location"
  list={locations}
  onChange={this.onChange}
/>

<DropdownMultiple
  name="location"
  title="Select location"
  titleSingular="location"
  list={locations}
  onChange={this.onChange}
/>

Note that when multiple options are selected in <DropdownMultiple>, titleSingular value automatically becomes the plural form of the noun. If you want to use a custom plural title, define titlePlural as well.

<DropdownMultiple
  name="location"
  title="Velg sted"
  titleSingular="Sted"
  titlePlural="Steder"
  list={locations}
  onChange={this.onChange}
/>

Search functionality

Using searchable prop enables the search bar. Pass an array of strings corresponding to place holder and not found message respectively.

<Dropdown
  name="location"
  title="Select location"
  searchable={["Search for location", "No matching location"]}
  list={locations}
  onChange={this.onChange}
/>

Selecting item(s) by default

Use the select prop to define the initally selected item(s).

For <Dropdown>

select={{value: 'istanbul'}}

For <DropdownMultiple>

select={[{value: 'oslo'}, {value: 'istanbul'}]}

Calling internal functions

Pass a ref and use it to call the internal functions.

For <Dropdown>

<Dropdown
  ref={this.dropdownRef}
  ...
/>

this.dropdownRef.current.selectSingleItem({ value: 'oslo' });
this.dropdownRef.current.clearSelection();

For <DropdownMultiple>

<DropdownMultiple
  ref={this.dropdownRef}
  ...
/>

this.dropdownRef.current.selectMultipleItems([
  { value: 'istanbul' }
  { value: 'oslo' },
]);

this.dropdownRef.current.selectAll();
this.dropdownRef.current.deselectAll();

Custom styling

Use the following keys in an object passed to styles prop

wrapper
header
headerTitle
headerArrowUpIcon
headerArrowDownIcon
checkIcon
list
listSearchBar
scrollList
listItem
listItemNoResult

Example:

<Dropdown
  name="location"
  title="Select location"
  list={locations}
  onChange={this.onChange}
  styles={{
    headerTitle: { color: 'red' }
  }}
/>

Note that styles prop is meant for basic styling. Use stylesheet by targeting the specific class names for more complicated stylings.

Use id prop to set an additional class name to every element in the dropdown menu. That way you can style multiple dropdown menus with different stylings rules.

In order to define your own arrow and check icons, use arrowUpIcon, arrowDownIcon and checkIcon props. These props accept an element type.

custom-reactjs-dropdown-components's People

Contributors

dbilgili avatar dependabot[bot] 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

custom-reactjs-dropdown-components's Issues

Item selected at true but still showing default title

Hello,

not sure if this is an issue or just a feature which isn't developped but here is my problem.
When building a DropDown I flag one of the item as selected, but the title is still showing the default title and not the key of the selected item.
I can confirm that the item is selected since when opening the dropdown it shows the ticker right to the item key.
I thought that maybe it was triggering a method internally to automatically display the item's key instead of the default title when an item is flagged as selected.
Do I have to change the default title myself ?

Thank you.

Ran into problems with resetThenSet

Might just be me as I didn't fork this repo, but when I tried:
temp[id].selected = true;
I got an error saying that temp[id] was undefined on the last item of every list is used.
I got around this quite easily though. Just replacing that line with:
temp.find(item => item.id === id).selected = true;

As I could only think that it was an indexing issue of some kind.

Cannot read select values from variable

I have a profiles form that i need to edit sizes. Im passing the selected sizes in a variable and getting this result:

[{"value":"3.5"},{"value":"4"},{"value":"4.5"}]

If i just basically copy and paste that string into the component, it works:
select={[{"value":"3.5"},{"value":"4"},{"value":"4.5"}]}

But is not working if use it like this:
select={sizesToEdit}

Face issue when selecting item - react-beautiful-dnd

Hello

I face the following error when i try and select a list item

react-beautiful-dnd An error has occurred while a drag is occurring.Any existing drag will be cancelled.> Uncaught Error: A cross-origin error was thrown.

Extra scroll bar

Open the Online Demo
Click on Regular Drop Down menu expample,
issue1
issue2

P.S : Removing the overflow from .dd-list should remove this, worked for me

Icon color change

Hi, Is it not possible to change the color of the icon?

I did write styles={{checkIcon: color: 'white'}} and it isn't working. Even used fill.

disable ability

Hi Mr. Bilgili,
I hope you are fine and doing well,
I want to ask if there is a prop to able or disable the dropdown menu based on a boolean value; because it will be very useful in a lot of cases.

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.