Skip to content

Commit

Permalink
Simplify custom tag list component demo
Browse files Browse the repository at this point in the history
  • Loading branch information
i-like-robots committed Jun 17, 2024
1 parent 9f8a69d commit d85141e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
53 changes: 19 additions & 34 deletions example/src/demos/CustomTagList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ 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([])

Expand All @@ -23,42 +19,33 @@ export function CustomTagList() {
[selected]
)

const onValidate = useCallback((value) => isValid(value), [])
function groupChildrenByFirstCharacter(children) {
const groups = {}

function getTagByTagKey(key, child) {
return {
suggestion: suggestions.find(
(suggestion) => `${suggestion.value}-${suggestion.label}` === key
),
child,
}
}
children.forEach((child) => {
const suggestion = suggestions.find((suggestion) =>
child.key.endsWith(`${suggestion.value}-${suggestion.label}`)
)
const firstChar = suggestion.label.charAt(0).toUpperCase()

groups[firstChar] ??= []
groups[firstChar].push({ suggestion, 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
}, {})
return groups
}

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

return (
<>
{Object.keys(groupedSuggestions).map((key) => (
{Object.keys(groupedTags).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">
<p>{`Countries starting with the letter "${key}":`}</p>
<ul className={classNames.tagList} {...tagListProps}>
{groupedTags[key].map(({ suggestion, child }) => (
<li className={classNames.tagListItem} key={suggestion.value}>
{child}
</li>
))}
Expand All @@ -73,14 +60,12 @@ export function CustomTagList() {
<>
<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}
Expand Down
5 changes: 3 additions & 2 deletions example/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@
margin: 0;
}

.tag-group > h2 {
font-size: 1rem;
.tag-group > p {
margin: 0;
font-size: 0.8125rem;
line-height: 1.5rem;
color: #00000080;
}

0 comments on commit d85141e

Please sign in to comment.