Skip to content

Commit

Permalink
refactor: ♻️ added disableBackspaceRemove (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
harshzalavadiya authored Oct 1, 2022
1 parent a1e4ff3 commit 7f666ea
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,21 @@ export default Example;

## 👀 Props

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

## 💅 Themeing

Expand Down
21 changes: 17 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface TagsInputProps {
onChange?: (tags: string[]) => void;
onBlur?: any;
seprators?: string[];
disableBackspaceRemove?: boolean;
onExisting?: (tag: string) => void;
onRemoved?: (tag: string) => void;
disabled?: boolean;
Expand Down Expand Up @@ -74,6 +75,7 @@ export const TagsInput = ({
onChange,
onBlur,
seprators,
disableBackspaceRemove,
onExisting,
onRemoved,
disabled,
Expand All @@ -90,16 +92,21 @@ export const TagsInput = ({

useEffect(() => {
if (JSON.stringify(value) !== JSON.stringify(tags)) {
setTags(value)
setTags(value);
}
}, [value])
}, [value]);

const handleOnKeyUp = e => {
e.stopPropagation();

const text = e.target.value;

if (e.key === "Backspace" && tags.length && !text) {
if (
!text &&
!disableBackspaceRemove &&
tags.length &&
e.key === "Backspace"
) {
e.target.value = isEditOnRemove ? `${tags.at(-1)} ` : "";
setTags([...tags.slice(0, -1)]);
}
Expand All @@ -125,7 +132,13 @@ export const TagsInput = ({
return (
<div aria-labelledby={name} className={cc("rti--container", RTIContainer)}>
{tags.map(tag => (
<Tag key={tag} className={classNames?.tag} text={tag} remove={onTagRemove} disabled={disabled} />
<Tag
key={tag}
className={classNames?.tag}
text={tag}
remove={onTagRemove}
disabled={disabled}
/>
))}

<input
Expand Down
1 change: 1 addition & 0 deletions stories/tags-input.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const Page = () => {
name="fruits"
placeHolder="enter fruits"
disabled={disabled}
disableBackspaceRemove={true}
isEditOnRemove={isEditOnRemove}
beforeAddValidate={beforeAddValidate}
/>
Expand Down

0 comments on commit 7f666ea

Please sign in to comment.