Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added renderTaglist #71

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions example/src/demos/CustomTagList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React, { useCallback, useState } from 'react'
import { ReactTags } from '../../../src'
import { suggestions } from '../countries'

function isValid(value) {
return /^[a-z]{4,12}$/i.test(value)
}

export function CustomTagList() {
const [selected, setSelected] = useState([])

const onAdd = useCallback(
(newTag) => {
setSelected([...selected, newTag])
},
[selected]
)

const onDelete = useCallback(
(tagIndex) => {
setSelected(selected.filter((_, i) => i !== tagIndex))
},
[selected]
)

const onValidate = useCallback((value) => isValid(value), [])

function getTagByTagKey(key, child) {
return {
suggestion: suggestions.find(
(suggestion) => `${suggestion.value}-${suggestion.label}` === key
),
child,
}
}

function groupChildrenByFirstCharacter(mappedSuggestions) {
return mappedSuggestions.reduce((acc, { suggestion, child }) => {
if (suggestion) {
const firstChar = suggestion.label.charAt(0).toUpperCase()
if (!acc[firstChar]) {
acc[firstChar] = []
}
acc[firstChar].push({ suggestion, child })
}
return acc
}, {})
}

function CustomTagList({ children, label, classNames, listRef }) {
const mappedSuggestions = children.map((child) => getTagByTagKey(child.key, child))
const groupedSuggestions = groupChildrenByFirstCharacter(mappedSuggestions)

return (
<>
{Object.keys(groupedSuggestions).map((key) => (
<div key={key} className="tag-group">
<h2>Countries starting with the letter "{key}"</h2>
<ul className={classNames.tagList} aria-label={label} ref={listRef} role="list">
{groupedSuggestions[key].map(({ suggestion, child }) => (
<li className={classNames.tagListItem} key={suggestion.value} role="listitem">
{child}
</li>
))}
</ul>
</div>
))}
</>
)
}

return (
<>
<p>Select the countries you have visited below. They will be grouped alphabetically:</p>
<ReactTags
allowNew
ariaDescribedBy="custom-tagList-description"
collapseOnSelect
id="custom-tagList-demo"
labelText="Enter new tags"
onAdd={onAdd}
onDelete={onDelete}
onValidate={onValidate}
selected={selected}
suggestions={suggestions}
renderTagList={CustomTagList}
/>
</>
)
}
Loading
Loading