Update TagInput.md
This commit is contained in:
@ -1,29 +1,26 @@
|
|||||||
---
|
---
|
||||||
title: TagInput
|
title: TagInput
|
||||||
tags: visual,input,intermediate
|
tags: input,visual,state,intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
Renders an input field to add tags.
|
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 with tags passed as `props`.
|
||||||
- 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 `addTab` method, which will be executed on pressing the `Enter` key.
|
- Define the `addTags` method, which will be executed on pressing the `Enter` key.
|
||||||
- `addTab` method uses the `setTabs` 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 `addTags` method uses the `setTabs` 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.
|
||||||
- Define the `removeTab` method, which will be executed on clicking the delete icon in the tag.
|
- Define the `removeTags` method, which will be executed on clicking the delete icon in the tag.
|
||||||
- Use `Array.prototype.filter()` in `removeTab` method to remove the tag using the `index` of the tag to filter it out from tags array.
|
- Use `Array.prototype.filter()` in `removeTags` method to remove the tag using the `index` of the tag to filter it out from `tags` array.
|
||||||
|
|
||||||
```css
|
```css
|
||||||
.tag-input {
|
.tag-input {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: flex-start;
|
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
min-height: 48px;
|
min-height: 48px;
|
||||||
width: 480px;
|
|
||||||
padding: 0 8px;
|
padding: 0 8px;
|
||||||
border: 1px solid rgb(214, 216, 218);
|
border: 1px solid #d6d8da;
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tag-input input {
|
.tag-input input {
|
||||||
flex: 1;
|
flex: 1;
|
||||||
border: none;
|
border: none;
|
||||||
@ -34,14 +31,12 @@ Renders an input field to add tags.
|
|||||||
outline: transparent;
|
outline: transparent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#tags {
|
#tags {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 8px 0 0;
|
margin: 8px 0 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tag {
|
.tag {
|
||||||
width: auto;
|
width: auto;
|
||||||
height: 32px;
|
height: 32px;
|
||||||
@ -56,11 +51,9 @@ Renders an input field to add tags.
|
|||||||
margin: 0 8px 8px 0;
|
margin: 0 8px 8px 0;
|
||||||
background: #0052cc;
|
background: #0052cc;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tag-title {
|
.tag-title {
|
||||||
margin-top: 3px;
|
margin-top: 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tag-close-icon {
|
.tag-close-icon {
|
||||||
display: block;
|
display: block;
|
||||||
width: 16px;
|
width: 16px;
|
||||||
@ -81,36 +74,32 @@ function TagInput(props) {
|
|||||||
const [tags, setTags] = React.useState(props.tags);
|
const [tags, setTags] = React.useState(props.tags);
|
||||||
const removeTags = indexToRemove => {
|
const removeTags = indexToRemove => {
|
||||||
setTags([...tags.filter((_, index) => index !== indexToRemove)]);
|
setTags([...tags.filter((_, index) => index !== indexToRemove)]);
|
||||||
}
|
};
|
||||||
const addTags = event => {
|
const addTags = event => {
|
||||||
if (event.target.value !== '') {
|
if (event.target.value !== "") {
|
||||||
setTags([...tags, event.target.value]);
|
setTags([...tags, 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) => (
|
{tags.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'
|
<span className="tag-close-icon" onClick={() => removeTags(index)}>
|
||||||
onClick={() => removeTags(index)}
|
|
||||||
>
|
|
||||||
x
|
x
|
||||||
</span>
|
</span>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
<input
|
<input
|
||||||
type='text'
|
type="text"
|
||||||
onKeyUp={event =>
|
onKeyUp={event => (event.key === "Enter" ? addTags(event) : null)}
|
||||||
event.key === 'Enter' ? addTags(event) : null
|
placeholder="Press enter to add tags"
|
||||||
}
|
|
||||||
placeholder='Press enter to add tags'
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user