Skip to content

Commit 7f666ea

Browse files
refactor: ♻️ added disableBackspaceRemove (#28)
1 parent a1e4ff3 commit 7f666ea

3 files changed

Lines changed: 33 additions & 18 deletions

File tree

README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,21 @@ export default Example;
5555

5656
## 👀 Props
5757

58-
| Prop | Description | Type | Default |
59-
| ------------------- | ------------------------------------------------------------------------------- | -------------------------------------------------- | ----------- |
60-
| `name` | value for name of input | `string` | |
61-
| `placeholder` | placeholder for text input | `string` | |
62-
| `value` | initial tags | `string[]` | `[]` |
63-
| `onChange` | onChange callback (added/removed) | `string[]` | |
64-
| `classNames` | className for styling input and tags (i.e {tag:'tag-cls', input: 'input-cls'}) | `object[tag, input]` | |
65-
| `onKeyUp` | input `onKeyUp` callback | `event` | |
66-
| `onBlur` | input `onBlur` callback | `event` | |
67-
| `seprators` | when to add tag (i.e. `Space`,`Enter`) | `string[]` | `["Enter"]` |
68-
| `onExisting` | if tag is already added then callback | `(tag: string) => void` | |
69-
| `onRemoved` | on tag removed callback | `(tag: string) => void` | |
70-
| `beforeAddValidate` | Custom validation before adding tag | `(tag: string, existingTags: string[]) => boolean` | |
71-
| `isEditOnRemove` | Remove the tag but keep the word in the input to edit it on using Backscape Key | `boolean` | `false` |
58+
| Prop | Description | Type | Default |
59+
| ------------------- | ------------------------------------------------------------------------------- | -------------------------------------------------- | --------------- |
60+
| `name` | value for name of input | `string` | |
61+
| `placeholder` | placeholder for text input | `string` | |
62+
| `value` | initial tags | `string[]` | `[]` |
63+
| `onChange` | onChange callback (added/removed) | `string[]` | |
64+
| `classNames` | className for styling input and tags (i.e {tag:'tag-cls', input: 'input-cls'}) | `object[tag, input]` | |
65+
| `onKeyUp` | input `onKeyUp` callback | `event` | |
66+
| `onBlur` | input `onBlur` callback | `event` | |
67+
| `seprators` | when to add tag (i.e. `Space`,`Enter`) | `string[]` | `["Enter"]` |
68+
| `removers` | Remove last tag if textbox empty and `Backspace` is pressed | `string[]` | `["Backspace"]` |
69+
| `onExisting` | if tag is already added then callback | `(tag: string) => void` | |
70+
| `onRemoved` | on tag removed callback | `(tag: string) => void` | |
71+
| `beforeAddValidate` | Custom validation before adding tag | `(tag: string, existingTags: string[]) => boolean` | |
72+
| `isEditOnRemove` | Remove the tag but keep the word in the input to edit it on using Backscape Key | `boolean` | `false` |
7273

7374
## 💅 Themeing
7475

src/index.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface TagsInputProps {
1111
onChange?: (tags: string[]) => void;
1212
onBlur?: any;
1313
seprators?: string[];
14+
disableBackspaceRemove?: boolean;
1415
onExisting?: (tag: string) => void;
1516
onRemoved?: (tag: string) => void;
1617
disabled?: boolean;
@@ -74,6 +75,7 @@ export const TagsInput = ({
7475
onChange,
7576
onBlur,
7677
seprators,
78+
disableBackspaceRemove,
7779
onExisting,
7880
onRemoved,
7981
disabled,
@@ -90,16 +92,21 @@ export const TagsInput = ({
9092

9193
useEffect(() => {
9294
if (JSON.stringify(value) !== JSON.stringify(tags)) {
93-
setTags(value)
95+
setTags(value);
9496
}
95-
}, [value])
97+
}, [value]);
9698

9799
const handleOnKeyUp = e => {
98100
e.stopPropagation();
99101

100102
const text = e.target.value;
101103

102-
if (e.key === "Backspace" && tags.length && !text) {
104+
if (
105+
!text &&
106+
!disableBackspaceRemove &&
107+
tags.length &&
108+
e.key === "Backspace"
109+
) {
103110
e.target.value = isEditOnRemove ? `${tags.at(-1)} ` : "";
104111
setTags([...tags.slice(0, -1)]);
105112
}
@@ -125,7 +132,13 @@ export const TagsInput = ({
125132
return (
126133
<div aria-labelledby={name} className={cc("rti--container", RTIContainer)}>
127134
{tags.map(tag => (
128-
<Tag key={tag} className={classNames?.tag} text={tag} remove={onTagRemove} disabled={disabled} />
135+
<Tag
136+
key={tag}
137+
className={classNames?.tag}
138+
text={tag}
139+
remove={onTagRemove}
140+
disabled={disabled}
141+
/>
129142
))}
130143

131144
<input

stories/tags-input.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const Page = () => {
2929
name="fruits"
3030
placeHolder="enter fruits"
3131
disabled={disabled}
32+
disableBackspaceRemove={true}
3233
isEditOnRemove={isEditOnRemove}
3334
beforeAddValidate={beforeAddValidate}
3435
/>

0 commit comments

Comments
 (0)