1.0.1 • Published 1 year ago

tag-input-box v1.0.1

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

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

NameExplanationTypeRequiredDefault
classNameClass name for the container div.stringNo""
itemsThe state variable for the list of tags.arrayYes-
setItemsThe set function for the items state.functionYes-
validatorUsed to perform validation on the inputs before adding them as tags.functionNo() => true
labelThe text label displayed with the input.stringNofalse
labelPosition"Top" or "Bottom" relative to the input.stringNofalse
separatorsA list of acceptable separators between tags.arrayNo[","]
forceLowerCaseDetermines if tags are set to lower case when submitted.booleanNofalse

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 ClassCSS SelectorTarget ElementExplanation
.TIB_Container.ExampleContainer divThis includes everything: the input and p elements.
.TIB_InputContainer.Example > divInput and tagsThis includes the input box (identifiable by the visible border).
.TIB_Label.Example pLabelThe label element only.
.TIB_Tag.Example > div > div > divTagsAll tags within the input container.
.TIB_XButton.Example buttonthe X buttonThe clear "X" button to the right of the input.
1.0.1

1 year ago

1.0.0

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.7

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.1.0

2 years ago