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

ezdynamic-react-selector

v2.17.1

Published

Easy Dynamic react selector, it gives you the freedom to custom your selections according to your own style.

Downloads

32

Readme

ezdynamic-react-selector

Easy Dynamic react selector component.

Introduction

It's a versatile React component for dynamic selection scenarios. It provides an easy-to-use interface for creating and managing selection components with customizable options and flexibility.

Key Features:

  • Dynamic Selection: Allows users to dynamically add, remove, and select items.
  • Customizable: Supports various customization options for styling and behavior.
  • Flexible Integration: Easily integrates with existing React applications and state management solutions with any desired design.

Install


npm install ezdynamic-react-selector

Usage

you should always import styles before it:

import "ezdynamic-react-selector/dist/index.css";
import { Selector } from "ezdynamic-react-selector";

Here are examples of how you can use it.

Sinlge Selection:

Examples

Set up the selected array as:

const selectedArr = {
  title: ReactNode;
  value: string;
}[]
First way:
import "ezdynamic-react-selector/dist/index.css";
import { Selector } from "ezdynamic-react-selector";
import styles from "./App.module.css";
import { useState } from "react";

const arr = [
  {
    title: "one",
    value: "one",
  },
  {
    title: "two",
    value: "two",
  },
  {
    title: "three",
    value: "three",
  },
  {
    title: "four",
    value: "four",
  },
  {
    title: "five",
    value: "five",
  },
  {
    title: "six ",
    value: "six",
  },
];

function App() {
  const [open, setOpen] = useState(false);
  const [selectedValue, setSelectedValue] = useState("");
  const handleToggle = () => {
    setOpen(!open);
  };
  return (
    <div
      className={styles.App}
      style={{
        padding: "20px",
        display: "flex",
        justifyContent: "center",
        alignItems: "center",
      }}
    >
      <div
        style={{
          display: "flex",
          alignItems: "center",
        }}
      >
        <Selector
          placeholder="Select Value"
          label={selectedValue}
          openMenu={open}
          onToggle={() => handleToggle()}
          list={arr}
          onSelect={(value) => setSelectedValue(value)}
          stylesControl={{
            selector: styles.selector,
            dropdown: styles.dropdown,
            placeholder: styles.placeholder,
          }}
        />
      </div>
    </div>
  );
}

export default App;
// As Example

.selector {
}
.dropdown {
}
.placeholder {
}

Seocnd way:

or you can simply wrap your code inside of it and customize the selection condition as follows:

NOTE This approach simplifies coding and allows you to fully customize it to suit your needs, which is the core value of this package.

in this case you have to write your own style for the dropdown menu

<Selector
  placeholder="Select Value"
  label={selectedValue}
  openMenu={open}
  onToggle={() => handleToggle()}
>
  <div>
    {arr.map((item, index) => (
      <Fragment key={index}>
        <div
          className={`${styles.item} ${
            selectedValue === item.value ? styles.selected : ""
          }`}
          onClick={() => setSelectedValue(item.value)}
        >
          <span>{item.title}</span>
          {selectedValue === item.value && (
            <span
              onClick={(e) => {
                e.stopPropagation();
                setSelectedValue("");
              }}
            >
              &#10060;
            </span>
          )}
        </div>
        {arr.length - 1 !== index && <hr />}
      </Fragment>
    ))}
  </div>
</Selector>

CSS code as:

.item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  border-radius: 4px;
  padding: 4px 7px;
}

.selected {
  background-color: #a419161c;
}

Multiple Selections:

Same as before but the label is changed to the selections the user would picked.

import "ezdynamic-react-selector/dist/index.css";
import { Selector } from "ezdynamic-react-selector";
import styles from "./App.module.css";
import { Fragment, useState } from "react";

const arr = [
  {
    title: "one",
    value: "one",
  },
  {
    title: "two",
    value: "two",
  },
  {
    title: "three",
    value: "three",
  },
  {
    title: "four",
    value: "four",
  },
  {
    title: "five",
    value: "five",
  },
  {
    title: "six ",
    value: "six",
  },
];

function App() {
  const [open, setOpen] = useState(false);
  const [selectedList, setSelectedList] = useState([]);

  const handleToggle = () => {
    setOpen(!open);
  };

  const removeItemFromList = (e, value) => {
    e.stopPropagation();
    setSelectedList((prev) => prev.filter((item) => item !== value));
  };

  return (
    <div
      className={styles.App}
      style={{
        padding: "20px",
        display: "flex",
        justifyContent: "center",
        alignItems: "center",
      }}
    >
      <div
        style={{
          display: "flex",
          alignItems: "center",
        }}
      >
        <Selector
          placeholder="Select Value"
          label={
            selectedList.length > 0 && (
              <div className={styles.selectedList}>
                {selectedList.map((item, index) => (
                  <span key={index}>{item}</span>
                ))}
              </div>
            )
          }
          openMenu={open}
          onToggle={() => handleToggle()}
        >
          <div>
            {arr.map((item, index) => (
              <Fragment key={index}>
                <div
                  className={`${styles.item} ${
                    selectedList.includes(item.value) ? styles.selected : ""
                  }`}
                  onClick={() =>
                    !selectedList.includes(item.value) &&
                    setSelectedList((prev) => [...prev, item.value])
                  }
                >
                  <span>{item.title}</span>
                  {selectedList.includes(item.value) && (
                    <span onClick={(e) => removeItemFromList(e, item.value)}>
                      &#10060;
                    </span>
                  )}
                </div>
                {arr.length - 1 !== index && <hr />}
              </Fragment>
            ))}
          </div>
        </Selector>
      </div>
    </div>
  );
}

export default App;
.selectedList {
  display: flex;
  gap: 10px;
}

Built With

Author

License

This project is licensed under the MIT License