Add items to the shopping list
React · React · beginner · modification
Households asked for a way to add things to the shared shopping list without leaving the page. `src/shopping/ShoppingList.jsx` gets a small form under the list (an "Item" field and an Add button). `addItem` reads the field through `FormData`, trims it, skips blank names, appends an unticked item with a fresh id to the list state, and resets the form. `src/shopping/ShoppingList.test.jsx` (new) has the component's first tests.
Household app SPA on React 18.3 (Vite, function components, served over HTTPS, so `crypto.randomUUID()` is available). Tests run with `npm test`: Vitest 2 with @testing-library/react 16 in jsdom. Existing and unchanged: `HouseholdPage.jsx` renders one shopping list per page, `<ShoppingList key={list.id} initialItems={list.items} />`, so a component instance always belongs to one list and `initialItems` is read once, on mount. `initialItems` is an array of at most 200 `{ id, name, done }` objects: `id` a unique non-empty string, `name` a non-empty string of at most 80 characters, `done` a boolean. The page's stylesheet already styles `.shopping-list` and its form controls.
Requirements
- The shopping list (`ShoppingList`) gets an add form under the list: a text field labelled "Item" and an Add button. Submitting it (the button or Enter) with a name that is not blank adds an unticked item with that name, trimmed, at the end of the list. The new item shows in the list straight away, and the field is cleared.
- A blank or whitespace-only name adds nothing. Names are at most 80 characters (the field's `maxLength`). The same name can be added twice; each add is its own item.
- A new item has the same shape as the existing ones, `{ id, name, done }`, with `done: false` and an `id` from `crypto.randomUUID()`.
- The `initialItems` array passed in by the parent is not modified.
- Unchanged: ticking an item's checkbox toggles its `done`; an empty list shows "Your list is empty."; the list lives in this component's state for the visit, and saving it to the server is a separate PR.
Files touched
- src/shopping/ShoppingList.jsx
- src/shopping/ShoppingList.test.jsx