# To-do list with drag to reorder

> A React Native to-do list for iOS, Android and the web: drag tasks by their handle to reorder them, tick them off with checkboxes, swipe to delete, and add new ones from a floating action button that opens a dialog. One file, built with Patine's SortableList, Checkbox, SwipeableRow, Fab and Dialog.

# To-do list

Today's tasks: drag one by its handle to change the order, tick it off, or swipe it left to delete it. The **+** button floats over the list and opens a dialog to add a task. On the web, the handles also move with the arrow keys.

```tsx live device
import { useState } from 'react';
import { StyleSheet, View } from 'react-native';
import {
  Button,
  Checkbox,
  Dialog,
  Fab,
  Header,
  SortableHandle,
  SortableList,
  SwipeableRow,
  Text,
  TextField,
  useTheme,
} from 'patine-ui';
import { Icon } from '@patine-ui/icons';

type Task = { id: string; title: string; note?: string; done: boolean };

const FIRST: Task[] = [
  { id: '1', title: 'Renew the car insurance', note: 'Before Friday', done: false },
  { id: '2', title: 'Book a vet visit for Pimenta', done: false },
  { id: '3', title: 'Return the drill to Rafael', done: true },
  { id: '4', title: 'Buy bread, figs and olive oil', note: 'Mercado de Campo de Ourique', done: false },
  { id: '5', title: 'Send the slides to Lea', done: false },
];

export default function Todo() {
  const { colors } = useTheme();
  const [tasks, setTasks] = useState(FIRST);
  const [adding, setAdding] = useState(false);
  const [title, setTitle] = useState('');
  const left = tasks.filter((t) => !t.done).length;

  const toggle = (id: string, done: boolean) => setTasks((all) => all.map((t) => (t.id === id ? { ...t, done } : t)));
  const remove = (id: string) => setTasks((all) => all.filter((t) => t.id !== id));
  const add = () => {
    if (!title.trim()) return;
    setTasks((all) => [{ id: String(Date.now()), title: title.trim(), done: false }, ...all]);
    setTitle('');
  };

  return (
    <View style={[styles.screen, { backgroundColor: colors.background }]}>
      <Header title="Today" subtitle={left ? `${left} to do` : 'All done'} size="large" />
      <SortableList
        data={tasks}
        keyExtractor={(task) => task.id}
        getItemLabel={(task) => task.title}
        onReorder={setTasks}
        activation="handle"
        style={styles.list}
        contentContainerStyle={styles.listContent}
        renderItem={({ item }) => (
          <SwipeableRow
            trailingActions={[
              { label: 'Delete', variant: 'destructive', icon: <Icon name="trash" size="sm" color="#fff" />, onPress: () => remove(item.id) },
            ]}
            fullSwipe
          >
            <View style={styles.row}>
              <Checkbox
                checked={item.done}
                onCheckedChange={(done) => toggle(item.id, done)}
                label={item.title}
                description={item.note}
                labelStyle={item.done ? [styles.done, { color: colors.textSecondary }] : undefined}
                style={styles.task}
              />
              <SortableHandle accessibilityLabel={`Reorder ${item.title}`} />
            </View>
          </SwipeableRow>
        )}
      />
      {tasks.length ? null : (
        <Text color="textSecondary" style={styles.empty}>
          Nothing on the list. Enjoy the afternoon.
        </Text>
      )}

      <View style={styles.fab}>
        <Fab icon={<Icon name="plus" size="lg" color="onPrimary" />} accessibilityLabel="Add a task" onPress={() => setAdding(true)} />
      </View>

      <Dialog
        open={adding}
        onOpenChange={setAdding}
        title="New task"
        description="It goes to the top of today’s list."
        actions={[
          { label: 'Cancel', variant: 'outline' },
          { label: 'Add task', onPress: add, disabled: !title.trim() },
        ]}
      >
        <TextField label="Task" placeholder="Water the balcony plants" value={title} onValueChange={setTitle} onSubmitEditing={add} autoFocus />
      </Dialog>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: { flex: 1 },
  list: { flex: 1 },
  listContent: { paddingBottom: 96 },
  row: { flexDirection: 'row', alignItems: 'center', paddingStart: 16, minHeight: 56 },
  task: { flex: 1, paddingVertical: 8 },
  done: { textDecorationLine: 'line-through' },
  empty: { position: 'absolute', top: 160, alignSelf: 'center' },
  fab: { position: 'absolute', right: 20, bottom: 24 },
});
```

## Components used

- [SortableList](/docs/components/sortable-list): `activation="handle"`, so a row lifts only by its handle and swipes stay swipes
- [Checkbox](/docs/components/checkbox): the task, its note as `description`, struck through when done
- [SwipeableRow](/docs/components/swipeable-row): swipe left to delete, with `fullSwipe`
- [Fab](/docs/components/fab): the floating **+**, with an `accessibilityLabel`
- [Dialog](/docs/components/dialog): controlled with `open`, with a text field in its body and `actions` in its footer
- [TextField](/docs/components/text-field), [Header](/docs/components/header), [Patine Icons](/docs/components/icons)

## How it works

- **Handles and swipes don't fight.** With `activation="handle"`, a drag that starts on the row is the row's swipe; only the handle lifts it to reorder.
- **`onReorder` gets the new array**, so `setTasks` is the whole handler. Save the order with the `{ from, to }` it also passes.
- **The dialog's Add button** is disabled until there's text, and each action closes the dialog after it runs.
