Skip to content

Commit 495524b

Browse files
harshzalavadiyaCrafterSamaJulmer Olivero
authored
Feat edit on remove (#16)
* [Improvement] - Keep the word when use backspace (#11) * Enlarge the TODO comment * refactoring follow comments and add keep words when use backspace optional Co-authored-by: Julmer Olivero <julmer.olivero@meru.com> Co-authored-by: Harsh Zalavadiya <harshzalavadiya@users.noreply.github.com> * refactor: ♻️ `keepWordsOnBackspace` to `isEditOnRemove` Co-authored-by: Julmer Olivero Ocando <jolivero.03@gmail.com> Co-authored-by: Julmer Olivero <julmer.olivero@meru.com>
1 parent 84dd1ff commit 495524b

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,18 @@ 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-
| `onBlur` | input `onBlur` callback | `event` | |
65-
| `seprators` | when to add tag (i.e. `Space`,`Enter`) | `string[]` | `["Enter"]` |
66-
| `onExisting` | if tag is already added then callback | `(tag: string) => void` | |
67-
| `onRemoved` | on tag removed callback | `(tag: string) => void` | |
68-
| `beforeAddValidate` | Custom validation before adding tag | `(tag: string, existingTags: string[]) => boolean` | |
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+
| `onBlur` | input `onBlur` callback | `event` | |
65+
| `seprators` | when to add tag (i.e. `Space`,`Enter`) | `string[]` | `["Enter"]` |
66+
| `onExisting` | if tag is already added then callback | `(tag: string) => void` | |
67+
| `onRemoved` | on tag removed callback | `(tag: string) => void` | |
68+
| `beforeAddValidate` | Custom validation before adding tag | `(tag: string, existingTags: string[]) => boolean` | |
69+
| `isEditOnRemove` | Remove the tag but keep the word in the input to edit it on using Backscape Key | `boolean` | `false` |
6970

7071
## 💅 Themeing
7172

src/index.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export interface TagsInputProps {
1414
onExisting?: (tag: string) => void;
1515
onRemoved?: (tag: string) => void;
1616
disabled?: boolean;
17+
isEditOnRemove?: boolean;
1718
beforeAddValidate?: (tag: string, existingTags: string[]) => boolean;
1819
}
1920

@@ -71,9 +72,10 @@ export const TagsInput = ({
7172
onExisting,
7273
onRemoved,
7374
disabled,
75+
isEditOnRemove,
7476
beforeAddValidate,
7577
}: TagsInputProps) => {
76-
const [tags, setTags] = useState(value || []);
78+
const [tags, setTags] = useState<any>(value || []);
7779

7880
useEffect(() => {
7981
onChange && onChange(tags);
@@ -85,7 +87,8 @@ export const TagsInput = ({
8587
const text = e.target.value;
8688

8789
if (e.key === "Backspace" && tags.length && !text) {
88-
setTags(tags.slice(0, -1));
90+
e.target.value = isEditOnRemove ? `${tags.at(-1)} ` : "";
91+
setTags([...tags.slice(0, -1)]);
8992
}
9093

9194
if (text && (seprators || defaultSeprators).includes(e.key)) {

stories/tags-input.stories.tsx

+16-5
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ export default {
99
export const Page = () => {
1010
const [selected, setSelected] = useState(["papaya"]);
1111
const [disabled, setDisabled] = useState(false);
12+
const [isEditOnRemove, setisEditOnRemove] = useState(false);
1213

13-
const beforeAddValidate = (text) => {
14-
if(text.length < 3) {
15-
alert("too short!")
14+
const beforeAddValidate = text => {
15+
if (text.length < 3) {
16+
alert("too short!");
1617
return false;
1718
}
1819
return true;
19-
}
20+
};
2021

2122
return (
22-
<div>
23+
<div style={{ marginBottom: "32px" }}>
2324
<h1>Add Fruits</h1>
2425
<pre>{JSON.stringify(selected)}</pre>
2526
<TagsInput
@@ -28,6 +29,7 @@ export const Page = () => {
2829
name="fruits"
2930
placeHolder="enter fruits"
3031
disabled={disabled}
32+
isEditOnRemove={isEditOnRemove}
3133
beforeAddValidate={beforeAddValidate}
3234
/>
3335
<div style={{ margin: "2rem 0", display: "flex", flexFlow: "row" }}>
@@ -39,6 +41,15 @@ export const Page = () => {
3941
</button>
4042
<pre>Disable: {JSON.stringify(disabled)}</pre>
4143
</div>
44+
<div style={{ margin: "2rem 0", display: "flex", flexFlow: "row" }}>
45+
<button
46+
onClick={() => setisEditOnRemove(!isEditOnRemove)}
47+
style={{ marginRight: "2rem" }}
48+
>
49+
Toggle Keep Words on Backspace
50+
</button>
51+
<pre>Keep Words on Backspace: {JSON.stringify(isEditOnRemove)}</pre>
52+
</div>
4253
</div>
4354
);
4455
};

0 commit comments

Comments
 (0)