2.0.6 • Published 2 years ago

react-tokenfield v2.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-tokenfield

Create token fields with copy/paste and keyboard support

NPM JavaScript Style Guide

Features

  • Select All with Ctrl+A
  • Copy & paste tokens with Ctrl+C and Ctrl+V
  • Keyboard navigation, delete tokens with keyboard (arrow keys, Tab/Shift + Tab)
  • Customizable token renderer
  • Validation using regular expression
  • Customizable token delimiters
  • Customizable token style
  • Auto Complete

Install

npm install --save react-tokenfield

Usage

import React, { useEffect, useState } from 'react'
import { TokenField } from 'react-tokenfield'

interface User {
  firstName: string
  lastName: string
  email: string
  image: string
}

const App = () => {
  const [tokens, setTokens] = useState<string[]>([])
  const [users, setUsers] = useState<User[]>([])
  useEffect(() => {
    window
      .fetch('https://dummyjson.com/users')
      .then((res: Response) => res.json())
      .then((res) => setUsers(res.users))
  }, [])

  function renderOptions(str: string): React.ReactElement {
    return (
      <div
        className='options'
      >
        {users
          .filter(
            (user) =>
              user.email.toLowerCase().startsWith(str.toLowerCase()) ||
              user.firstName.toLowerCase().startsWith(str.toLowerCase()) ||
              user.lastName.toLowerCase().startsWith(str.toLowerCase())
          )
          .map((user) => (
            <div key={user.email} className='user-info' data-value={user.email}>
              <img alt={user.email} src={user.image} />
              <div>
                <div>
                  <b>{`${user.firstName} ${user.lastName}`}</b>
                </div>
                <div>{user.email}</div>
              </div>
            </div>
          ))}
      </div>
    )
  }
  const emailPattern: string =
    '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$'

  return (
    <div
      style={{
        width: '600px',
        fontFamily: 'Arial',
        padding: '10px',
        margin: '0 auto'
      }}
    >
      <TokenField
        placeholder='Type an email'
        onChange={({ tokens }) => setTokens(tokens)}
        pattern={emailPattern}
        delimiters=',; '
        showRemoveButton={false}
        options={{
          render: renderOptions,
          selectedClassName: 'selected'
        }}
        tokens={tokens}
      />
    </div>
  )
}

export default App

Properties

Property NameTypeDescription
placeholderstringa short hint that describes the expected value of an input
autoFocusbooleanapply auto focus on the tokenfield
delimitersstringa string that contains all related delimiters for example ',;-', the first delimiter is the main delimiter that's means that when you copy tokens the copied token will be separated with the main delimeter
tokensstring[]The array of string tokens
patternstringThe pattern specifies a regular expression that token should match
showRemoveButtonbooleanShow remove botton
optionsobjectOptions for the autocomplete How to use options..
onChangefunctionCallback function that get the tokens and invalid tokens
getTokenCSSfunctionCallback function that return CSS style for token, the function gets the token state of the token(selected,valid,invalid).
tokenFieldCSSobjectCustom CSS style for the tokenfield control.

How to use options

Options has 2 properties

  • renderer A callback function that gets the part of the string that the user inserts and returns a ReactElement that contains the options as child elements, each child element represents the option value and should add the data-value property, the data-value conatins the value that will add as a new/updated token.
  • selectedClassName the css class name that will be set on the selected option

Snapshot

alt text

Demo

License

MIT © Shahar Levi

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.1

2 years ago

2.0.0-beta.3

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0

2 years ago

1.1.7

2 years ago

1.1.6-beta.3

2 years ago

1.1.6-beta.2

2 years ago

1.1.6-beta.1

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.0

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.0.0

9 years ago