Global Navigation

  • Products
  • Overview
  • Getting Started
  • Guides
  • Resources
  • Console

Table of Contents

  • Introduction
    • App Builder Overview
    • What is App Builder
    • Business Case
    • FAQ
    • Community
  • Quick Start
    • App Builder Getting Started
      • Setting Up
      • Creating your First App
      • Publishing Your App
      • Troubleshooting
    • Runtime Getting Started
      • Overview
      • Activations
      • Deploy
      • Entities
      • How Runtime Works
      • Resources
      • Setup
      • Understanding Runtime
  • Develop
    • References
    • App Builder Guides
      • Architecture Overview
        • App Hooks
        • Introduction to React Spectrum
        • Using SDKs
      • Application State
      • Application Logging
        • Azure Log Analytics
        • New Relic
        • Splunk Cloud
        • Splunk Enterprise
      • Configuration
        • Webpack Configuration
      • Deployment
        • CI/CD for App Builder Apps
        • Credential Rotation
        • Setting Response Headers
      • Development
      • Distribution
      • Events
        • Webhooks
      • Exc App
        • Interfaces
          • Modules
          • Page ObjectWithHref
          • Page ObjectWithPath
          • Page PageAPI
          • Page PageAPIProperties
          • Runtime
          • TopBar Callback
          • TopBar CustomFeedbackConfig
          • TopBar CustomSearchConfig
          • TopBar ExternalFeedbackConfig
          • TopBar HelpCenterFeedbackConfig
          • TopBar Solution
          • TopBar TopBarAPI
          • TopBar TopBarAPIProperties
          • User UserAPI
          • User UserInfo
        • Migrate App to Exp Cloud SPA
        • Modules
          • Page
          • TopBar
          • User
      • Extensions
        • Extension Migration Guide
      • Optimization
      • Security
        • Understanding Authentication
      • Telemetry
    • Runtime Guides
      • Contribution Guide
      • Asynchronous Calls
      • Creating Actions
      • Creating REST APIs
      • CI/CD Pipeline
      • Debugging
      • Logging & Monitoring
      • Reference Docs
        • API Reference
        • CLI Usage
        • Configuring Proxy
        • Environment Variables
        • Feeds
        • Multiple Regions
        • Packages
        • Prepackages
        • Runtimes
        • Sequences & Compositions
        • Triggers & Rules
        • WSK Usage
      • Security General
      • Securing Web Actions
      • System Settings
      • Throughput Tuning
      • Tools
        • CLI Install
      • Troubleshooting
      • Using Packages
      • Using Runtime
    • Contribution Guide
  • Learning
    • Asset Compute Worker PS API
      • Requirements
      • Lesson 1: Create an app from Asset Compute template
      • Lesson 2: Configure the app
      • Lesson 3: Develop worker calling Photoshop API
      • Lesson 4: Integrate worker in AEMaaCS
      • Well done
    • Barcode Reader
      • Requirements
      • Lesson 1: Bootstrap a Headless App
      • Lesson 2: Writing a Serverless Action
      • Lesson 3: Unit and E2E Tests
      • Well done
    • Blog Articles
      • Blog Articles
    • CI/CD
      • Requirements
      • Lesson 1: Setup CI/CD
      • Lesson 2: Monitoring CI/CD
      • Lesson 3: Custom CI/CD workflow
      • Well done
    • Cron Jobs
      • Requirements
      • Lesson 1: Bootstrap a Headless App
      • Lesson 2: Set up Alarm Feed with Trigger and Rule
      • Lesson 3: Types of Alarm Feed
      • Well done
    • Custom Asset Compute Worker
      • Requirements
      • How AEM as Cloud assets works
      • Architecture of our worker
      • Configure services
      • Local environment setup
      • Implement the worker
      • Test the worker
      • Setup AEM to use the worker
      • Well Done
    • Customer Dashboard
      • Requirements
      • Lesson 1: Create a New App Builder App from Campaign Standard Template
      • Lesson 2: Explore the App Builder App
      • Lesson 3: Run the App Builder App Locally
      • Lesson 4: List All Customer Profiles on the UI
      • Lesson 5: Add Personalized Promotion Emails Triggering
      • Well Done
    • Debugging
      • Requirements
      • Lesson 1: Getting familiar with Debugger
      • Lesson 2: Debugging Application Code
      • Lesson 3: Managing Application Logs
      • Well Done
    • Event Driven
      • Requirements
      • Lesson 1: Create a New App Builder App from Template
      • Lesson 2: Register the App as Event Provider
      • Lesson 3: Fire an Event
      • Lesson 4: Consume Events
      • Well Done
    • Events Runtime
      • Requirements
      • Lesson 1: Step by Step Guide
      • Lesson 2: Verify the result
      • Well done
    • Journaling Events
      • Requirements
      • Lesson 1: Create an Event Provider using App Builder
      • Lesson 2: Create the Event Consumer using Journaling API
      • Lesson 3: End to end test
      • Well done
    • Sample Apps
      • Code Snippets
        • Caching HTTP responses
        • App Builder Files SDK
        • App Builder State SDK
        • I/O Events handler
        • Real-time data from Adobe Analytics API 1.4
    • Spectrum Introduction
      • Lesson 1: What is Spectrum ?
      • Lesson 2: Using Spectrum CSS
      • Lesson 3: Using React Spectrum
      • Lesson 4: Using React Spectrum in App Builder
      • Well done
    • Todo App
      • Requirements
      • Lesson 1: Create a New App Builder App with the React Spectrum template
      • Lesson 2: Setup Runtime actions
      • Lesson 3: Setup the CreateTodoList component
      • Lesson 4: Setup the Todo component
      • Lesson 5: Setup the TodoList component
      • Lesson 6: Bringing the pieces together to build the App
      • Well done
    • Videos
      • Overview
        • Introducing App Builder
        • Getting Started
        • Architecture
        • A Full Security Overview
        • User Journey
      • Exploring
        • Projects and Workspaces
        • React Spectrum
        • Custom Events
        • CI/CD
        • Debugging
        • Learning Resources
        • Dashboard Case Study
        • ODE Case Study
        • Deep Dive Use Cases
        • Live Wired Sneak
        • Softcrylic Partner Showcase
      • Developers Live
        • App Builder Deep Dive
        • Asset Compute Service Extensibility
        • Extend Adobe Experience Cloud
  1. Products
  2. Overview
  3. Resources
  4. Learning
  5. Todo App
  6. Lesson 5: Setup the TodoList component

Lesson 5: Set up the TodoList component

In this lesson, we'll create a TodoList React component that will be able to:

  • Create a new to-do item.
  • Delete the whole to-do list.
  • Display to-do items we have built in the previous lesson.

We'll create the React component file under web-src/src/components/ and name it TodoList.js.

Import React Spectrum components

This component will make use of several React Spectrum components:

  • View a generic container.
  • Well a generic pre-styled container which will contain the todo list.
  • Flex for the layout.
  • Form to submit the todo.
  • TextField the input field for the todo value.
  • ActionButton to trigger form submission.
  • AlertDialog to warn the user before deleting a todo list.
  • DialogTrigger to open the AlertDialog.
import { View, Flex, Form, TextField, ActionButton, AlertDialog, DialogTrigger, Well } from '@adobe/react-spectrum';

Component properties

The component will accept three properties:

  • todoList which corresponds to the to-do list object which holds the todo list name and to-do items.
  • onDelete the callback function to delete a to-do list.
  • onUpdate the callback function to update a to-do list.
function TodoList({ todoList, onDelete, onUpdate }) { const { name, todos } = todoList; // ... }

Spectrum Icons

We'll add React Spectrum Workflow Icons to the TodoList component. The icons are simply SVGs packed as React components.

To import icons, you need the @spectrum-icons/workflow dependency. It's pre-installed if you initialize the App Builder app with the React Spectrum template.

Each icon must be imported individually with a single statement:

import TaskList from '@spectrum-icons/workflow/TaskList'; import Close from '@spectrum-icons/workflow/Close'; import Add from '@spectrum-icons/workflow/Add';

A searchable list of workflow icons is available on the Spectrum website. The name of the icon without any whitespace matches the import in React Spectrum.

Displaying the to-do list name

React Spectrum has made available a set of components . Unfortunately, some components are not available in React Spectrum but available in Spectrum CSS which is the CSS framework used by React Spectrum to style components. Fortunately, we can use React Spectrum together with Spectrum CSS to fill in missing components.

In this case, we're going to install the Spectrum CSS Typography dependency to render Spectrum headings. Since React Spectrum already defines the Spectrum CSS variables, you'll need to install only the Spectrum CSS Typography package:

npm i --save @spectrum-css/typography

Then you can import the CSS:

import '@spectrum-css/typography';

Now you can use Spectrum CSS Heading classes to render the to-do list name.

<h2 className="spectrum-Heading spectrum-Heading--sizeM spectrum-Heading--serif">{name}</h2>

Rendering to-do items

Once again, we're going to use the State hook useState to declare a state variable which will hold a list of to-do items. This list will be updated whenever a new to-do item is created. By default, it's initialized with the todos from the todoList prop:

const [todoItems, setTodoItems] = useState(todos);

Next we'll iterate over the todoItems array using the map() function to render each item as Todo component.

<View marginTop="size-100"> {todoItems.map((todo) => ( <Todo key={todo.id} name={name} todo={todo} onUpdate={onUpdate} /> ))} </View>

The key property is necessary in React to uniquely identify the to-do item. In this case, we use the todo id. You can read more about React lists and keys here.

We're also passing the name, todo and onUpdate props down to the Todo component.

Creating a to-do item

In lesson 2, we defined a MAX_TODO_ITEMS value within a global configuration file defaults.json at the root of the App Builder App. Now we'll use it to block the user from creating too many to-do items inside a to-do list. We can import the value in a way similar to what we did in the Runtime action:

import { MAX_TODO_ITEMS } from '../../defaults.json';

Now we'll use the React hook useState again to bind it to the TextField to create a new to-do item the way we did in the previous lesson:

const [newTodo, setNewTodo] = useState('');

But this time we will disable the input once we reach the count of MAX_TODO_ITEMS by setting the isDisabled prop:

<TextField autoComplete="off" isDisabled={todoItems.length >= MAX_TODO_ITEMS} aria-label="New todo" width="100%" value={newTodo} onChange={(value) => { setNewTodo(value); }} placeholder="Todo" minLength={1} maxLength={140} /> <ActionButton type="submit" isDisabled={todoItems.length >= MAX_TODO_ITEMS}> <Add /> </ActionButton>

Finally, we'll wrap the TextField and the submit ActionButton with a Form component so we can invoke the onUpdate callback function on form submission. We'll prevent default form submission behavior with event.preventDefault() and create a new to-do item object that we pass to the callback function. Additionally, we're clearing the value of TextField:

<Form onSubmit={async (event) => { event.preventDefault(); const index = todoItems.length; const newTodoItem = { name, id: index, value: newTodo, done: false }; setTodoItems([newTodoItem, ...todoItems]); setNewTodo(''); onUpdate && (await onUpdate(name, newTodoItem)); }}>

Deleting a to-do list

Next to the to-do list name, we'll add a trigger to delete the list. The trigger will open an AlertDialog to warn the user about the action. Confirming the operation calls the onDelete callback function passing it the name of the to-do list. The dialog will close itself automatically.

<DialogTrigger> <ActionButton isQuiet> <Close /> </ActionButton> <AlertDialog title="Clear todo list" variant="destructive" primaryActionLabel="Delete" secondaryActionLabel="Cancel" onPrimaryAction={async () => { onDelete && (await onDelete(name)); }}> This action will clear the todo list <strong>{name}</strong>. Are you sure you want to continue ? </AlertDialog> </DialogTrigger>

Full component

Finally, we'll wrap the whole component inside a Well and use the Flex layout to align the Todo items vertically.

See the full component code here.

App