Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface ReactTagInputProps {
editable?: boolean;
readOnly?: boolean;
removeOnBackspace?: boolean;
addOnBlur?: boolean
delimiters?: [number]
}

interface State {
Expand All @@ -33,10 +35,10 @@ export default class ReactTagInput extends React.Component<ReactTagInputProps, S
onInputKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {

const { input } = this.state;
const { validator, removeOnBackspace } = this.props;
const { validator, removeOnBackspace, delimiters } = this.props;

// On enter
if (e.keyCode === 13) {
// Check if default Enter or one of the delimiter keys was hit
if (e.keyCode === 13 || delimiters?.includes(e.keyCode)) {

// Prevent form submission if tag input is nested in <form>
e.preventDefault();
Expand Down Expand Up @@ -69,6 +71,29 @@ export default class ReactTagInput extends React.Component<ReactTagInputProps, S

}

onBlur = () => {

const { input } = this.state
const { validator, addOnBlur } = this.props

if (addOnBlur) {

// If input is blank, do nothing
if (input === "") { return; }

// Check if input is valid
const valid = validator !== undefined ? validator(input) : true
if (!valid) {
return;
}

// Add input to tag list
this.addTag(input)

}

}

addTag = (value: string) => {
const tags = [ ...this.props.tags ];
if (!tags.includes(value)) {
Expand Down Expand Up @@ -131,6 +156,7 @@ export default class ReactTagInput extends React.Component<ReactTagInputProps, S
placeholder={placeholder || "Type and press enter"}
onChange={this.onInputChange}
onKeyDown={this.onInputKeyDown}
onBlur={this.onBlur}
/>
}
</div>
Expand Down