Route Stop Editor
Fixed route endpoints and caller-owned intermediate stops with accessible move, remove, and add controls.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add route-stop-editorSource files
Expand a file to read the source the installer copies into your app.
components/blocks/route-stop-editor/RouteStopEditor.tsx
import { Fragment, useState } from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { Separator } from '@/components/ui/separator'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'
export interface RouteStop {
id: string
label: string
detail?: string
}
interface RouteStopEditorLabels {
start: string
finish: string
stop: (position: number) => string
moveUp: string
moveDown: string
remove: string
add: string
limit: (maximum: number) => string
moved: (name: string, position: number) => string
removed: (name: string) => string
}
export interface RouteStopEditorProps extends Omit<ViewProps, 'children'> {
className?: string
title?: string
start: string
finish: string
/** Only intermediate stops. IDs must be unique and nonempty. */
stops?: readonly RouteStop[]
/** Maximum intermediate stops that can be added. Existing stops can still be removed. */
maxStops?: number
onValueChange: (stops: RouteStop[]) => void
onAdd?: () => void
labels?: Partial<RouteStopEditorLabels>
}
interface RequestedChange {
id: string
index: number
announcement: string
}
export function RouteStopEditor({
className,
title = 'Route stops',
start,
finish,
stops = [],
maxStops = 4,
onValueChange,
onAdd,
labels,
...props
}: RouteStopEditorProps) {
const [requested, setRequested] = useState<RequestedChange>()
const limit = Number.isSafeInteger(maxStops) ? Math.max(0, maxStops) : 4
const canAdd = stops.length < limit
const moveUp = labels?.moveUp ?? 'Move up'
const moveDown = labels?.moveDown ?? 'Move down'
const removeLabel = labels?.remove ?? 'Remove'
const add = labels?.add ?? 'Add stop'
useAnnouncement(
requested && stops.findIndex(({ id }) => id === requested.id) === requested.index
? requested.announcement
: undefined,
)
function move(index: number, offset: -1 | 1) {
const target = index + offset
const stop = stops[index]
if (!stop || target < 0 || target >= stops.length) return
const next = [...stops]
next[index] = stops[target]
next[target] = stop
onValueChange(next)
setRequested({
id: stop.id,
index: target,
announcement: labels?.moved?.(stop.label, target + 1) ?? `${stop.label} moved to stop ${target + 1}.`,
})
}
function remove(stop: RouteStop) {
onValueChange(stops.filter(({ id }) => id !== stop.id))
setRequested({
id: stop.id,
index: -1,
announcement: labels?.removed?.(stop.label) ?? `${stop.label} removed.`,
})
}
return (
<View {...props} className={cn('gap-[16px]', className)}>
<Text size="title" accessibilityRole="header">
{title}
</Text>
<View className="overflow-hidden rounded-lg border border-border bg-card">
<View className="gap-[4px] px-[16px] py-[12px]">
<Text size="sm" variant="muted">
{labels?.start ?? 'Start'}
</Text>
<Text className="font-semibold text-card-foreground">{start}</Text>
</View>
{stops.map((stop, index) => (
<Fragment key={stop.id}>
<Separator />
<View className="gap-[8px] px-[16px] py-[12px]">
<View className="gap-[4px]">
<Text size="sm" variant="muted">
{labels?.stop?.(index + 1) ?? `Stop ${index + 1}`}
</Text>
<Text className="font-semibold text-card-foreground">{stop.label}</Text>
{stop.detail ? (
<Text size="sm" variant="muted">
{stop.detail}
</Text>
) : null}
</View>
<View className="flex-row flex-wrap gap-[4px]">
<Button
size="sm"
variant="ghost"
disabled={index === 0}
accessibilityLabel={`${moveUp}, ${stop.label}`}
onPress={() => move(index, -1)}
>
{moveUp}
</Button>
<Button
size="sm"
variant="ghost"
disabled={index === stops.length - 1}
accessibilityLabel={`${moveDown}, ${stop.label}`}
onPress={() => move(index, 1)}
>
{moveDown}
</Button>
<Button
size="sm"
variant="ghost"
textClassName="text-destructive"
accessibilityLabel={`${removeLabel}, ${stop.label}`}
onPress={() => remove(stop)}
>
{removeLabel}
</Button>
</View>
</View>
</Fragment>
))}
{onAdd ? (
<>
<Separator />
<View className="gap-[8px] px-[16px] py-[12px]">
<Button
variant="outline"
accessibilityLabel={add}
disabled={!canAdd}
onPress={() => {
if (canAdd) onAdd()
}}
>
{add}
</Button>
{!canAdd ? (
<Text size="sm" variant="muted">
{labels?.limit?.(limit) ?? `Maximum intermediate stops: ${limit}`}
</Text>
) : null}
</View>
</>
) : null}
<Separator />
<View className="gap-[4px] px-[16px] py-[12px]">
<Text size="sm" variant="muted">
{labels?.finish ?? 'Final stop'}
</Text>
<Text className="font-semibold text-card-foreground">{finish}</Text>
</View>
</View>
</View>
)
}
components/blocks/route-stop-editor/index.ts
export { RouteStopEditor, type RouteStop, type RouteStopEditorProps } from './RouteStopEditor'