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

react-html-datalist

v2.0.4

Published

Simple React component for HTML datalist tag

Downloads

69

Readme

React HTML Datalist

NPM Snyk Vulnerabilities for npm package npm bundle size

React HTML Datalist is a simple component that makes it easier to work with html <datalist> tag in React. The usage of this component is very similar to that of the <select> tag.

What problem does it solve?

Unlike the <select> tag in html, a <datalist> tag does not provide the flexibility to manage different value and text for an <option>.

For example, option for select can be given as: <option value="1">Pizza</option>

but option for datalist is given as: <option value="Pizza" />

Hence, React HTML Datalist makes it easier to manage both text and value for options in datalist.

Installation

It's easy to install react-html-datalist, no extra piece of code is needed. Simply run the command:

npm install react-html-datalist

Usage

To use the react-html-datalist component you can simply import it and pass the related props. Moreover, the component can be used with both class and function components. Refer to the examples below.

Examples

# Function Component

import React, { useState } from "react";
import ReactDOM from "react-dom";
import ReactHTMLDatalist from "react-html-datalist";

const App = props => {
  const [detail, setDetail] = useState({ food_id: "" });

  const handleChange = e => {
    setDetail({ ...detail, [e.target.name]: e.target.value });
  };

  return (
    <div className="App">
      <h1>React HTML Datalist</h1>
      <p>
        <b>Selected food id:</b> {detail.food_id || "(none)"}
      </p>

      <small>Search or select in the datalist: </small>

      <ReactHTMLDatalist
            name={"food_id"}
            onChange={handleChange}
            classNames={"classone classtwo"}
            options={[
              { text: "Pizza", value: "1" },
              { text: "Burger", value: "2" },
              { text: "Sandwich", value: "3" },
              { text: "French Fries", value: "4" }
            ]}
      />
    </div>
  );
};

export default App;
Try react-html-datalist with function components (hooks) on codesandbox:

Edit react-html-datalist-with-react-hooks

# Class Component

import React from "react";
import ReactDOM from "react-dom";
import ReactHTMLDatalist from "react-html-datalist";

class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      food_id: ""
    };
  }

  handleChange = e => {
    this.setState({ [e.target.name]: e.target.value });
  };

  render() {
  
    var { food_id } = this.state;

    return (
      <div className="App">
        <h1>React HTML Datalist</h1>
        <p>
          <b>Selected food id:</b> {food_id || "(none)"}
        </p>

        <small>Search or select in the datalist: </small>

        <ReactHTMLDatalist
              name={"food_id"}
              onChange={this.handleChange}
              classNames={"classone classtwo"}
              options={[
                { text: "Pizza", value: "1" },
                { text: "Burger", value: "2" },
                { text: "Sandwich", value: "3" },
                { text: "French Fries", value: "4" }
              ]}
        />
      </div>
    );
  }
}

export default App;
Try react-html-datalist with class components on codesandbox:

Edit react-html-datalist-with-class-components

Props

The component can be provided with the following props:

| prop | datatype | example | is required | | ------ | ------ | ------ | ------ | | name | string | "myform" | yes | | classNames | string | "class-one class2" | no | | styles | object | { color: "#0cc" } | no | | onChange | function | handleChange | yes | | options | array | [ { text:"pizza" ,value:1 }, { text: "burger", value: 2 } ] | yes |

Note: The component does not require a value prop. It manages value on it's own via the provided onChange prop.

License

MIT

Enjoy!