Update TagInput
This commit is contained in:
@ -5,12 +5,12 @@ tags: components,input,state,intermediate
|
|||||||
|
|
||||||
Renders a tag input field.
|
Renders a tag input field.
|
||||||
|
|
||||||
- Define a `TagInput` component and use `React.useState()` hook to initialize an array with tags passed as `props`.
|
- Define a `TagInput` component and use `React.useState()` hook to initialize an array from `tags`.
|
||||||
- Use `Array.prototype.map()` on collected nodes to render the list of tags.
|
- Use `Array.prototype.map()` on collected nodes to render the list of tags.
|
||||||
- Define the `addTags` method, which will be executed on pressing the `Enter` key.
|
- Define the `addTagData` method, which will be executed on pressing the `Enter` key.
|
||||||
- The `addTags` method uses the `setTags` method to add the new tag using the spread (`...`) operator to prepend the existing tags and adds the new tag at the end of the `tags` array.
|
- The `addTagData` method uses the `setTagData` method to add the new tag using the spread (`...`) operator to prepend the existing tags and adds the new tag at the end of the `tagData` array.
|
||||||
- Define the `removeTags` method, which will be executed on clicking the delete icon in the tag.
|
- Define the `removeTagData` method, which will be executed on clicking the delete icon in the tag.
|
||||||
- Use `Array.prototype.filter()` in `removeTags` method to remove the tag using the `index` of the tag to filter it out from `tags` array.
|
- Use `Array.prototype.filter()` in `removeTagData` method to remove the tag using the `index` of the tag to filter it out from `tagData` array.
|
||||||
|
|
||||||
```css
|
```css
|
||||||
.tag-input {
|
.tag-input {
|
||||||
@ -70,24 +70,24 @@ Renders a tag input field.
|
|||||||
```
|
```
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
function TagInput(props) {
|
const TagInput = ({ tags }) => {
|
||||||
const [tags, setTags] = React.useState(props.tags);
|
const [tagData, setTagData] = React.useState(tags);
|
||||||
const removeTags = indexToRemove => {
|
const removeTagData = indexToRemove => {
|
||||||
setTags([...tags.filter((_, index) => index !== indexToRemove)]);
|
setTagData([...tagData.filter((_, index) => index !== indexToRemove)]);
|
||||||
};
|
};
|
||||||
const addTags = event => {
|
const addTagData = event => {
|
||||||
if (event.target.value !== "") {
|
if (event.target.value !== '') {
|
||||||
setTags([...tags, event.target.value]);
|
setTagData([...tagData, event.target.value]);
|
||||||
event.target.value = "";
|
event.target.value = '';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
<div className="tag-input">
|
<div className="tag-input">
|
||||||
<ul id="tags">
|
<ul id="tags">
|
||||||
{tags.map((tag, index) => (
|
{tagData.map((tag, index) => (
|
||||||
<li key={index} className="tag">
|
<li key={index} className="tag">
|
||||||
<span className="tag-title">{tag}</span>
|
<span className="tag-title">{tag}</span>
|
||||||
<span className="tag-close-icon" onClick={() => removeTags(index)}>
|
<span className="tag-close-icon" onClick={() => removeTagData(index)}>
|
||||||
x
|
x
|
||||||
</span>
|
</span>
|
||||||
</li>
|
</li>
|
||||||
@ -95,12 +95,12 @@ function TagInput(props) {
|
|||||||
</ul>
|
</ul>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
onKeyUp={event => (event.key === "Enter" ? addTags(event) : null)}
|
onKeyUp={event => (event.key === 'Enter' ? addTagData(event) : null)}
|
||||||
placeholder="Press enter to add tags"
|
placeholder="Press enter to add tags"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```jsx
|
```jsx
|
||||||
|
|||||||
Reference in New Issue
Block a user