UIPackage React Native
Search
Menu

Task Detail

A controlled task editor section with a title, composable due date, checklist, and one status action with error recovery.

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 task-detail

Source files

Expand a file to read the source the installer copies into your app.

components/blocks/task-detail/TaskDetail.tsx
import * as React from 'react'
import { View, type ViewProps } from 'react-native'
import { Alert, AlertDescription } from '@/components/ui/alert'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
import { FormField } from '@/components/ui/form-field'
import { Input } from '@/components/ui/input'
import { List, ListItem, ListItemContent, ListItemDescription, ListItemTitle } from '@/components/ui/list'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'

const defaultLabels = {
  title: 'Title',
  dueDate: 'Due date',
  checklist: 'Checklist',
  open: 'Open',
  completed: 'Completed',
  completeTask: 'Mark complete',
  reopenTask: 'Reopen task',
  retry: 'Try again',
  updating: 'Updating task…',
}

export interface TaskDetailProps extends ViewProps {
  className?: string
  title?: string
  onTitleChange?: (title: string) => void
  titleError?: string
  /** Compose a native date picker or read-only date. The caller owns its value. */
  dueDate?: React.ReactNode
  completed?: boolean
  onCompletedChange?: (completed: boolean) => void
  /** Applies only to the status action. Title, due date, and checklist stay available. */
  updating?: boolean
  /** Error from the status action. A retry replaces the regular status button. */
  error?: string
  onRetry?: () => void
  labels?: Partial<typeof defaultLabels>
}

function hasChecklistItems(children: React.ReactNode): boolean {
  return React.Children.toArray(children).some((child) =>
    React.isValidElement<{ children?: React.ReactNode }>(child) && child.type === React.Fragment
      ? hasChecklistItems(child.props.children)
      : true,
  )
}

export function TaskDetail({
  className,
  title = 'Prepare the studio handover',
  onTitleChange,
  titleError,
  dueDate,
  completed = false,
  onCompletedChange,
  updating = false,
  error,
  onRetry,
  labels,
  children,
  ...props
}: TaskDetailProps) {
  const copy = { ...defaultLabels, ...labels }
  const hasChecklist = hasChecklistItems(children)
  const retry = error && onRetry ? onRetry : undefined
  const action = retry ?? (onCompletedChange ? () => onCompletedChange(!completed) : undefined)
  const actionLabel = updating ? copy.updating : retry ? copy.retry : completed ? copy.reopenTask : copy.completeTask
  useAnnouncement(error && !updating ? error : undefined)

  return (
    <View {...props} className={cn('gap-5', className)}>
      <View className="gap-4 rounded-2xl border border-border bg-card p-4">
        <View className="self-start">
          <Badge variant={completed ? 'success' : 'outline'} text={completed ? copy.completed : copy.open} />
        </View>
        {onTitleChange ? (
          <FormField label={copy.title} error={titleError}>
            {(field) => <Input {...field} value={title} onChangeText={onTitleChange} />}
          </FormField>
        ) : (
          <Text size="title" accessibilityRole="header">
            {title}
          </Text>
        )}
        {dueDate ? (
          <View className="gap-2">
            <Text size="sm" className="font-medium">
              {copy.dueDate}
            </Text>
            {dueDate}
          </View>
        ) : null}
      </View>
      {hasChecklist ? (
        <View className="gap-2">
          <Text size="lg" accessibilityRole="header" className="font-semibold">
            {copy.checklist}
          </Text>
          <List>{children}</List>
        </View>
      ) : null}
      {error && !updating ? (
        <Alert variant="destructive">
          <AlertDescription>{error}</AlertDescription>
        </Alert>
      ) : null}
      {action ? (
        <Button
          accessibilityLabel={actionLabel}
          variant={completed && !retry ? 'outline' : 'default'}
          loading={updating}
          onPress={() => {
            if (!updating) action()
          }}
        >
          {actionLabel}
        </Button>
      ) : null}
    </View>
  )
}

export interface TaskDetailChecklistItemProps extends ViewProps {
  className?: string
  label: string
  detail?: string
  checked?: boolean
  disabled?: boolean
  onCheckedChange?: (checked: boolean) => void
}

export function TaskDetailChecklistItem({
  className,
  label,
  detail,
  checked = false,
  disabled = false,
  onCheckedChange,
  ...props
}: TaskDetailChecklistItemProps) {
  const interactive = !!onCheckedChange && !disabled
  return (
    <ListItem
      {...props}
      accessible
      accessibilityRole="checkbox"
      accessibilityLabel={props.accessibilityLabel ?? [label, detail].filter(Boolean).join(', ')}
      accessibilityState={{ ...props.accessibilityState, checked, disabled: !interactive }}
      disabled={disabled}
      onPress={interactive ? () => onCheckedChange(!checked) : undefined}
      className={cn('gap-1 py-2 pe-4 ps-1', className)}
    >
      <Checkbox aria-hidden pointerEvents="none" focusable={false} tabIndex={-1} checked={checked} />
      <ListItemContent>
        <ListItemTitle>{label}</ListItemTitle>
        {detail ? <ListItemDescription>{detail}</ListItemDescription> : null}
      </ListItemContent>
    </ListItem>
  )
}
components/blocks/task-detail/index.ts
export {
  TaskDetail,
  TaskDetailChecklistItem,
  type TaskDetailProps,
  type TaskDetailChecklistItemProps,
} from './TaskDetail'

Dependencies

Browse work and productivity