Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
react-tag-autocomplete
Advanced tools
React Tag Autocomplete is a simple tagging component ready to drop in your React projects.
React Tag Autocomplete is a simple tagging component ready to drop in your React projects. Originally based on the React Tags project by Prakhar Srivastav this version removes the drag-and-drop re-ordering functionality, adds appropriate roles and ARIA states and introduces a resizing text input. View demo.
Please note, this version is in beta, you can check out the latest stable version here 📢
This is a Node.js module available through the npm registry. Before installing, download and install Node.js.
Installation is done using the npm install command:
npm install --save react-tag-autocomplete@pre-release
Here's a sample implementation that initializes the component with a list of preselected tags
and a suggestions
list. For further customization details, see options.
import React from 'react'
import ReactDOM from 'react-dom'
import ReactTags from 'react-tag-autocomplete'
class App extends React.Component {
constructor (props) {
super(props)
this.state = {
tags: [
{ id: 1, name: "Apples" },
{ id: 2, name: "Pears" }
],
suggestions: [
{ id: 3, name: "Bananas" },
{ id: 4, name: "Mangos" },
{ id: 5, name: "Lemons" },
{ id: 6, name: "Apricots" }
]
}
}
onDelete (i) {
const tags = this.state.tags.slice(0)
tags.splice(i, 1)
this.setState({ tags })
}
onAddition (tag) {
const tags = [].concat(this.state.tags, tag)
this.setState({ tags })
}
render () {
return (
<ReactTags
tags={this.state.tags}
suggestions={this.state.suggestions}
onDelete={this.onDelete.bind(this)}
onAddition={this.onAddition.bind(this)} />
)
}
}
ReactDOM.render(<App />, document.getElementById('app'))
id
tags
suggestions
suggestionsFilter
placeholderText
removeButtonText
noSuggestionsText
autoresize
delimiters
minQueryLength
maxSuggestionsLength
classNames
onAddition
onDelete
onInput
onFocus
onBlur
onValidate
addOnBlur
allowNew
allowBackspace
tagComponent
inputAttributes
The ID attribute given to the listbox element. Default: ReactTags
.
An array of selected tags. Each tag is an object which must have an id
and a name
property. Defaults to []
.
const tags = [
{ id: 1, name: 'Apples' },
{ id: 2, name: 'Pears' }
]
An array of tag suggestions. Each suggestion is an object which must have an id
and a name
property and an optional disabled
property to make the suggestion non-selectable. Defaults to []
.
const suggestions = [
{ id: 3, name: 'Bananas' },
{ id: 4, name: 'Mangos' },
{ id: 5, name: 'Lemons' },
{ id: 6, name: 'Apricots', disabled: true }
]
A callback function to filter suggestion items with. The callback receives two arguments; a suggestion
and the current query
and must return a boolean value.
If no function is supplied the default filter is applied. Defaults to null
.
The placeholder string shown for the input. Defaults to 'Add new tag'
.
The title text to add to the remove selected tag button. Default 'Click to remove tag'
.
Message shown if there are no matching suggestions. Defaults to null
.
Boolean parameter to control whether the text-input should be automatically resized to fit its value. Defaults to true
.
Array of keys matching KeyboardEvent.key
values. When a corresponding key is pressed it will trigger tag selection or creation. Defaults to ['Enter', 'Tab']
.
Minimum query length required to show the suggestions list. Defaults to 2
.
Maximum number of suggestions to display. Defaults to 6
.
Override the default class names used by the component. Defaults to:
{
root: 'react-tags',
rootFocused: 'is-focused',
selected: 'react-tags__selected',
selectedTag: 'react-tags__selected-tag',
selectedTagName: 'react-tags__selected-tag-name',
search: 'react-tags__search',
searchWrapper: 'react-tags__search-wrapper',
searchInput: 'react-tags__search-input',
suggestions: 'react-tags__suggestions',
suggestionActive: 'is-active',
suggestionDisabled: 'is-disabled'
}
Function called when the user wants to add a tag. Receives the tag.
function onAddition(tag) {
const tags = [...this.state.tags, tag]
this.setState({ tags })
}
Function called when the user wants to delete a tag. Receives the tag index.
function onDelete(i) {
const tags = this.state.tags.slice(0)
tags.splice(i, 1)
this.setState({ tags })
}
Optional event handler when the input value changes. Receives the current query.
function onInput(query) {
if (!this.state.busy) {
this.setState({ busy: true })
return fetch(`query=${query}`).then((result) => {
this.setState({ busy: false })
})
}
}
Optional callback function for when the input receives focus. Receives no arguments.
Optional callback function for when focus on the input is lost. Receives no arguments.
Optional validation function that determines if tag should be added. Receives the tag object and must return a boolean.
function onValidate(tag) {
return tag.name.length >= 5;
}
Creates a tag from the current input value when focus on the input is lost. Defaults to false
.
Enable users to add new (not suggested) tags. Defaults to false
.
Enable users to delete selected tags when backspace is pressed while focussed on the text input when empty. Defaults to true
.
Provide a custom tag component to render. Defaults to null
.
Provide a custom suggestion component to render. Default: null
.
An object containing additional attributes that will be applied to the text input. Please note that this prop cannot overwrite existing attributes, it can only add new ones. Defaults to {}
.
It is possible to customize the appearance of the component, the included styles found in /example/styles.css
are only an example.
The component is written in ES6 and uses Rollup as its build tool. Tests are written with Jasmine using JSDOM.
npm install
npm run dev # will open http://localhost:8080 and watch files for changes
To see all changes refer to the changelog.
on
prefixes, e.g. the handleAddition()
callback should now be called onAddition()
.delimiters
option is now an array of KeyboardEvent.key
values and not KeyboardEvent.keyCode
codes, e.g. [13, 9]
should now be written as ['Enter', 'Tab']
. See https://keycode.info/ for more information.placeholder
option has been renamed placeholderText
delimiterChars
option has been removed, use the delimiters
option instead.clearInputOnDelete
option has been removed and is now the default behaviourautofocus
option has been removed.FAQs
A simple, accessible, tagging component ready to drop into your React projects.
The npm package react-tag-autocomplete receives a total of 351 weekly downloads. As such, react-tag-autocomplete popularity was classified as not popular.
We found that react-tag-autocomplete demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.