-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft/working on the project tag input component,and feedback
- Loading branch information
mosesmulumba
committed
Jul 5, 2024
1 parent
c53bf2c
commit 077a0e1
Showing
7 changed files
with
172 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
.tagInputContainer { | ||
border: 1px solid #000; | ||
padding: 5px; | ||
border-radius: 5px; | ||
display: inline-block; | ||
width: 99%; | ||
min-height: 4vh; | ||
} | ||
|
||
.tagList { | ||
display: flex; | ||
justify-items: flex-start; | ||
max-width: 27.5rem; | ||
} | ||
|
||
.tagList ,.tag{ | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
gap: 5px; | ||
} | ||
|
||
.tag { | ||
background-color: #e0e0e0; | ||
border-radius: 3px; | ||
padding: 2px 5px; | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.tag:hover{ | ||
background-color: #ccc; | ||
color: #000; | ||
} | ||
|
||
.tag button { | ||
background: none; | ||
border: none; | ||
margin-left: 5px; | ||
cursor: pointer; | ||
} | ||
|
||
.input { | ||
border: none; | ||
outline: none; | ||
flex: 1; | ||
} | ||
|
||
.suggestions { | ||
border: 1px solid #ccc; | ||
border-radius: 5px; | ||
margin-top: 5px; | ||
position: absolute; | ||
background: white; | ||
z-index: 1; | ||
width: 300px; | ||
} | ||
|
||
.suggestion { | ||
padding: 5px; | ||
cursor: pointer; | ||
} | ||
|
||
.suggestion:hover { | ||
background-color: #f0f0f0; | ||
} | ||
.Element > div > .tagInputContainer , .tagList{ | ||
min-width: 35rem; | ||
max-width: 100%; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,79 @@ | ||
import React, { useState } from 'react'; | ||
import PrimaryButton from '../PrimaryButton/index.js'; | ||
import styles from './taginput.css'; | ||
import React, { useState } from "react"; | ||
import styles from "./TagInput.module.css"; | ||
|
||
const TagInput = () => { | ||
const [tag, setTag] = useState(''); | ||
const TagInput = ({ suggestions }) => { | ||
const [tags, setTags] = useState([]); | ||
const [error, setError] = useState(''); | ||
const [input, setInput] = useState(""); | ||
const [filteredSuggestions, setFilteredSuggestions] = useState([]); | ||
|
||
const handleInputChange = (e) => { | ||
setTag(e.target.value); | ||
}; | ||
const value = e.target.value; | ||
setInput(value); | ||
|
||
const handleAddTag = () => { | ||
if (tag.trim() === '') { | ||
setError('Tag cannot be empty'); | ||
return; | ||
if (value) { | ||
const filtered = suggestions.filter((suggestion) => | ||
suggestion.toLowerCase().includes(value.toLowerCase()) | ||
); | ||
setFilteredSuggestions(filtered); | ||
} else { | ||
setFilteredSuggestions([]); | ||
} | ||
}; | ||
|
||
setTags([...tags, { tag_id: Date.now(), tag }]); | ||
setTag(''); | ||
setError(''); | ||
const handleKeyDown = (e) => { | ||
if (e.key === "Enter" && input) { | ||
if (!tags.includes(input)) { | ||
setTags([...tags, input]); | ||
} | ||
setInput(""); | ||
setFilteredSuggestions([]); | ||
} | ||
}; | ||
|
||
const handleDeleteTag = (tagId) => { | ||
setTags(tags.filter(tag => tag.tag_id !== tagId)); | ||
const handleTagRemove = (index) => { | ||
setTags(tags.filter((_, i) => i !== index)); | ||
}; | ||
|
||
const handleKeyDown = (e) => { | ||
if (e.key === 'Enter') { | ||
handleAddTag(); | ||
const handleSuggestionClick = (suggestion) => { | ||
if (!tags.includes(suggestion)) { | ||
setTags([...tags, suggestion]); | ||
} | ||
setInput(""); | ||
setFilteredSuggestions([]); | ||
}; | ||
|
||
return ( | ||
<div className={`${styles.tagContainer}`}> | ||
<div className={`${styles.inputsection}`}> | ||
<input | ||
type="text" | ||
value={tag} | ||
onChange={handleInputChange} | ||
onKeyDown={handleKeyDown} | ||
placeholder="Enter a tag" | ||
/> | ||
<PrimaryButton onClick={handleAddTag} size="Primary-Btn"> | ||
Add Tag | ||
</PrimaryButton> | ||
</div> | ||
|
||
{error && <p style={{ color: 'red' }}>{error}</p>} | ||
<ul> | ||
{tags.map((tag) => ( | ||
<li key={tag.tag_id}> | ||
{tag.tag} | ||
<button onClick={() => handleDeleteTag(tag.tag_id)}>x</button> | ||
</li> | ||
<div className={styles.tagInputContainer}> | ||
<div className={styles.tagList}> | ||
{tags.map((tag, index) => ( | ||
<div key={index} className={styles.tag}> | ||
{tag} | ||
<button onClick={() => handleTagRemove(index)}>x</button> | ||
</div> | ||
))} | ||
</ul> | ||
</div> | ||
<input | ||
type="text" | ||
value={input} | ||
onChange={handleInputChange} | ||
onKeyDown={handleKeyDown} | ||
className={styles.input} | ||
/> | ||
{filteredSuggestions.length > 0 && ( | ||
<div className={styles.suggestions}> | ||
{filteredSuggestions.map((suggestion, index) => ( | ||
<div | ||
key={index} | ||
className={styles.suggestion} | ||
onClick={() => handleSuggestionClick(suggestion)} | ||
> | ||
{suggestion} | ||
</div> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default TagInput; | ||
export default TagInput; |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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