Components
Repeatable rows
A list of fields that a person can add to and remove from, such as skills or languages.
'use client'
import { useState } from 'react'
import { FieldLegend, FieldSet } from '@dex/shared/components/field'
import { Input } from '@dex/shared/components/input'
import {
RepeatableRow,
RepeatableRows,
RepeatableRowsAddButton,
} from '@dex/shared/components/repeatable-rows'
export function RepeatableRowsDefault() {
const [skills, setSkills] = useState([
{ id: 1, name: 'PyTorch' },
{ id: 2, name: 'Kubernetes' },
{ id: 3, name: '' },
])
const rename = (id: number, name: string) =>
setSkills((current) => current.map((skill) => (skill.id === id ? { ...skill, name } : skill)))
return (
<FieldSet className="w-full max-w-md">
<FieldLegend>Skills</FieldLegend>
<RepeatableRows>
{skills.map((skill, index) => (
<RepeatableRow
key={skill.id}
removeLabel={`Remove skill ${index + 1}`}
onRemove={() => setSkills((current) => current.filter((item) => item.id !== skill.id))}
>
<Input
aria-label={`Skill ${index + 1}`}
value={skill.name}
placeholder="e.g. Python"
onChange={(event) => rename(skill.id, event.target.value)}
/>
</RepeatableRow>
))}
<RepeatableRowsAddButton
onClick={() => setSkills((current) => [...current, { id: Date.now(), name: '' }])}
>
Add skill
</RepeatableRowsAddButton>
</RepeatableRows>
</FieldSet>
)
}
'use client'
import { useRef, useState } from 'react'
import { FieldLegend, FieldSet } from '@dex/shared/components/field'
import { Input } from '@dex/shared/components/input'
import {
RepeatableRow,
RepeatableRows,
RepeatableRowsAddButton,
} from '@dex/shared/components/repeatable-rows'
export function RepeatableRowsOneEmptyRow() {
const [cities, setCities] = useState([{ id: 1, name: '' }])
const rowsRef = useRef<HTMLDivElement>(null)
const hasEveryCity = cities.every((city) => city.name.trim())
// Adding hides the add button and removing hides the bin, so focus moves into a row's input.
const focusRow = (index: number) => {
requestAnimationFrame(() => rowsRef.current?.querySelectorAll('input')[index]?.focus())
}
const rename = (id: number, name: string) =>
setCities((current) => current.map((city) => (city.id === id ? { ...city, name } : city)))
const remove = (id: number, index: number) => {
setCities((current) =>
current.length > 1
? current.filter((city) => city.id !== id)
: [{ id: Date.now(), name: '' }],
)
focusRow(Math.min(index, Math.max(cities.length - 2, 0)))
}
const add = () => {
setCities((current) => [...current, { id: Date.now(), name: '' }])
focusRow(cities.length)
}
return (
<FieldSet className="w-full max-w-md">
<FieldLegend>Which cities would you consider?</FieldLegend>
<RepeatableRows ref={rowsRef}>
{cities.map((city, index) => (
<RepeatableRow
key={city.id}
removeLabel={`Remove city ${index + 1}`}
isRemovable={cities.length > 1 || Boolean(city.name)}
onRemove={() => remove(city.id, index)}
>
<Input
aria-label={`City ${index + 1}`}
value={city.name}
placeholder="e.g. Manchester"
onChange={(event) => rename(city.id, event.target.value)}
/>
</RepeatableRow>
))}
{hasEveryCity && (
<RepeatableRowsAddButton onClick={add}>Add another</RepeatableRowsAddButton>
)}
</RepeatableRows>
</FieldSet>
)
}
'use client'
import { useState } from 'react'
import { Input } from '@dex/shared/components/input'
import {
RepeatableRow,
RepeatableRows,
RepeatableRowsAddButton,
RepeatableRowsHeader,
} from '@dex/shared/components/repeatable-rows'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@dex/shared/components/select'
const LEVELS = { native: 'Native', professional: 'Professional', limited: 'Limited' }
export function RepeatableRowsTwoFields() {
const [languages, setLanguages] = useState([
{ id: 1, name: 'English', level: 'native' },
{ id: 2, name: 'French', level: 'professional' },
])
const update = (id: number, change: { name?: string; level?: string }) =>
setLanguages((current) =>
current.map((language) => (language.id === id ? { ...language, ...change } : language)),
)
return (
<RepeatableRows className="w-full max-w-xl max-sm:gap-4">
<RepeatableRowsHeader
labels={[
{ id: 'rows-language', label: 'Language', className: 'flex-1' },
{ id: 'rows-proficiency', label: 'Proficiency', className: 'w-50 shrink-0' },
]}
/>
{languages.map((language) => (
<RepeatableRow
key={language.id}
removeLabel={`Remove ${language.name || 'language'}`}
onRemove={() =>
setLanguages((current) => current.filter((item) => item.id !== language.id))
}
>
<Input
aria-labelledby="rows-language"
className="sm:flex-1"
value={language.name}
onChange={(event) => update(language.id, { name: event.target.value })}
/>
<Select
items={LEVELS}
value={language.level || null}
onValueChange={(level) => update(language.id, { level })}
>
<SelectTrigger aria-labelledby="rows-proficiency" className="sm:w-50 sm:shrink-0">
<SelectValue placeholder="Choose level" />
</SelectTrigger>
<SelectContent>
{Object.entries(LEVELS).map(([value, label]) => (
<SelectItem key={value} value={value}>
{label}
</SelectItem>
))}
</SelectContent>
</Select>
</RepeatableRow>
))}
<RepeatableRowsAddButton
onClick={() =>
setLanguages((current) => [...current, { id: Date.now(), name: '', level: '' }])
}
>
Add language
</RepeatableRowsAddButton>
</RepeatableRows>
)
}
'use client'
import { useState } from 'react'
import { FieldLegend, FieldSet } from '@dex/shared/components/field'
import { Input } from '@dex/shared/components/input'
import {
RepeatableRow,
RepeatableRows,
RepeatableRowsAddButton,
} from '@dex/shared/components/repeatable-rows'
export function RepeatableRowsRowError() {
const [links, setLinks] = useState([
{ id: 1, url: 'https://github.com/samokafor' },
{ id: 2, url: 'my blog' },
])
const edit = (id: number, url: string) =>
setLinks((current) => current.map((link) => (link.id === id ? { ...link, url } : link)))
return (
<FieldSet className="w-full max-w-md">
<FieldLegend>Links</FieldLegend>
<RepeatableRows>
{links.map((link, index) => {
const isInvalid = Boolean(link.url) && !URL.canParse(link.url)
return (
<RepeatableRow
key={link.id}
removeLabel={`Remove link ${index + 1}`}
onRemove={() => setLinks((current) => current.filter((item) => item.id !== link.id))}
error={isInvalid && 'Enter a link like https://github.com/you.'}
>
<Input
type="url"
aria-label={`Link ${index + 1}`}
aria-invalid={isInvalid || undefined}
value={link.url}
placeholder="https://"
onChange={(event) => edit(link.id, event.target.value)}
/>
</RepeatableRow>
)
})}
<RepeatableRowsAddButton
onClick={() => setLinks((current) => [...current, { id: Date.now(), url: '' }])}
>
Add link
</RepeatableRowsAddButton>
</RepeatableRows>
</FieldSet>
)
}
- Each row is ordinary fields with a remove button at its end. One button under the list adds the next row.
- Name every field. With one field per row, the legend names the group and each input takes an
aria-labelsuch as "City 2". With two fields per row, each takesaria-labelledbypointing at its column label. - An error belongs to its row. Pass it as
erroron the row, and setaria-invalidon the field it is about. - When a list starts with one empty row, hide that row's remove button with
isRemovableuntil there is something to remove. Leave it on in two-field rows, so the fields stay under their column labels. - When adding hides the add button, or removing takes away the button that was pressed, move focus into a row's input so it is not lost.