A Pen by james quinn on CodePen.
Created
June 8, 2020 17:57
-
-
Save jameswquinn/09ae785c40477e8c5bfd6b5f0c940ee9 to your computer and use it in GitHub Desktop.
Add Tags - React Hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#root |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const TagsInput = props => { | |
const [tags, setTags] = React.useState(props.tags); | |
const removeTags = indexToRemove => { | |
setTags([...tags.filter((_, index) => index !== indexToRemove)]); | |
}; | |
const addTags = event => { | |
if (event.target.value !== "") { | |
setTags([...tags, event.target.value]); | |
props.selectedTags([...tags, event.target.value]); | |
event.target.value = ""; | |
} | |
}; | |
return ( | |
<div className="tags-input"> | |
<ul id="tags"> | |
{tags.map((tag, index) => ( | |
<li key={index} className="tag"> | |
<span className='tag-title'>{tag}</span> | |
<span className='tag-close-icon' | |
onClick={() => removeTags(index)} | |
> | |
x | |
</span> | |
</li> | |
))} | |
</ul> | |
<input | |
type="text" | |
onKeyUp={event => event.key === "Enter" ? addTags(event) : null} | |
placeholder="Press enter to add tags" | |
/> | |
</div> | |
); | |
}; | |
const App = () => { | |
const selectedTags = tags => { | |
console.log(tags); | |
}; | |
return ( | |
<div className="App"> | |
<TagsInput selectedTags={selectedTags} tags={['Nodejs', 'MongoDB']}/> | |
</div> | |
); | |
}; | |
ReactDOM.render(<App />, document.getElementById("root")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import url("https://fonts.googleapis.com/css?family=Overpass"); | |
* { | |
box-sizing: border-box; | |
font-family: "Overpass", sans-serif; | |
} | |
body { | |
height: 100vh; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
font-family: "Overpass", sans-serif; | |
} | |
.tags-input { | |
display: flex; | |
align-items: flex-start; | |
flex-wrap: wrap; | |
min-height: 48px; | |
width: 480px; | |
padding: 0 8px; | |
border: 1px solid rgb(214, 216, 218); | |
border-radius: 6px; | |
&:focus-within { | |
border: 1px solid #0052cc; | |
} | |
input { | |
flex: 1; | |
border: none; | |
height: 46px; | |
font-size: 14px; | |
padding: 4px 0 0 0; | |
&:focus { | |
outline: transparent; | |
} | |
} | |
} | |
#tags { | |
display: flex; | |
flex-wrap: wrap; | |
padding: 0; | |
margin: 8px 0 0 0; | |
} | |
.tag { | |
width: auto; | |
height: 32px; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
color: #fff; | |
padding: 0 8px; | |
font-size: 14px; | |
list-style: none; | |
border-radius: 6px; | |
margin: 0 8px 8px 0; | |
background: #0052cc; | |
.tag-title { | |
margin-top: 3px; | |
} | |
.tag-close-icon { | |
display: block; | |
width: 16px; | |
height: 16px; | |
line-height: 16px; | |
text-align: center; | |
font-size: 14px; | |
margin-left: 8px; | |
color: #0052cc; | |
border-radius: 50%; | |
background: #fff; | |
cursor: pointer; | |
} | |
} | |
@media screen and (max-width: 567px) { | |
.tags-input { | |
width: calc(100vw - 32px); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment