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

tag-input-box

v1.0.1

Published

The Tag Input Box is a **React** component providing the user with an input field used to enter and modify tags. These tags are contained within the input box, and can be selected/modified/deleted. There are key presses that can be used to perform these

Downloads

3

Readme

Tag Input Box

Introduction

The Tag Input Box is a React component providing the user with an input field used to enter and modify tags. These tags are contained within the input box, and can be selected/modified/deleted. There are key presses that can be used to perform these actions with ease. , label, separators, forceLowerCase

Props

| Name | Explanation | Type | Required | Default | |----------------|----------------------------------------------------------------------|----------|----------|--------------| | className | Class name for the container div. | string | No | "" | | items | The state variable for the list of tags. | array | Yes | - | | setItems | The set function for the items state. | function | Yes | - | | validator | Used to perform validation on the inputs before adding them as tags. | function | No | () => true | | label | The text label displayed with the input. | string | No | false | | labelPosition | "Top" or "Bottom" relative to the input. | string | No | false | | separators | A list of acceptable separators between tags. | array | No | [","] | | forceLowerCase | Determines if tags are set to lower case when submitted. | boolean | No | false |

Controlling State

you must use a state variable to control the input. Simply create a state variable with the following syntax:

const [items, setItems] = useState([]);

You can then pass the state and its set function to TagInputBox as the items and setItems props:

<TagInputBox
    items={ items }
    setItems={ setItems }
    ...
/>

Example

Here is a working example allowing the user to enter email addresses.

// A basic regex performing email validation, returning true or false for valid or invalid email
const isEmailValid = input => /^[^@]+@[^@]+\.[^@]+$/.test(input);

// Defining the state variable
const [emails, setEmails] = useState([]);

return (
    // JSX to be returned
    <TagInputBox
        items={ items }
        setItems={ setEmails }
        validator={ isEmailValid }
        label="Emails:"
        separators={ [",", ";"] }
    />
)

The input box will initially render as empty.

Email Input 1

You can type an email into the box.

Email Input 2

To submit the email, we can either type a separator (comma or semicolon, as provided for the separators prop), or we can press the Enter key. At this point, the email is tested against the validator.

If the validator returns true, the email is added as a tag.

Email Input 3

Tag Functionality

Tags can be modified/deleted using the following built-in functionality:

  • Clicking a tag will make it editable, i.e. put its text back in the input box.
  • Holding CTRL and clicking a tag will select it.
  • Clicking the X: if there is text in the input box, it will clear it. If there are selected tags, it will delete them.
  • Pressing Delete when the input is empty will delete all selected tags.
  • Pressing Backspace when the input is empty will make the last tag editable.
  • Pressing CTRL and Backspace does the same as clicking the X.
  • Presing CTRL C, V or X copies, pastes or cuts either text or tags.

CSS Classes

You have the option to pass a classname to the component. This will be applied to the container div. You can then use the selectors in the table below. (This is not always necessary, as you may be able to target different parts of the component with their class names).

<TagInputBox className="Example" ... />

| Element Class | CSS Selector | Target Element | Explanation | |---------------------|----------------------------|----------------|-------------------------------------------------------------------| | .TIB_Container | .Example | Container div | This includes everything: the input and p elements. | | .TIB_InputContainer | .Example > div | Input and tags | This includes the input box (identifiable by the visible border). | | .TIB_Label | .Example p | Label | The label element only. | | .TIB_Tag | .Example > div > div > div | Tags | All tags within the input container. | | .TIB_XButton | .Example button | the X button | The clear "X" button to the right of the input. |