/// import { type Cell, Default, handler, recipe, UI } from "commontools"; interface FriendListState { names: Default; selectedIndex: Default; } const removeItem = handler; index: number }>( (_, { names, index }) => { const currentNames = names.get(); names.set(currentNames.toSpliced(index, 1)); }, ); const editItem = handler; index: number }>( (event, { names, index }) => { if (event?.key === "Enter") { const newValue = event?.target?.value; if (newValue !== undefined) { const currentNames = names.get(); names.set(currentNames.toSpliced(index, 1, newValue)); } } }, ); const selectItem = handler< unknown, { selectedIndex: Cell; index: number } >( (_, { selectedIndex, index }) => { selectedIndex.set(index); }, ); const moveItem = handler< any, { names: Cell; selectedIndex: Cell; direction: "UP" | "DOWN"; } >((_, { names, selectedIndex, direction }) => { const index = selectedIndex.get(); const currentNames = names.get(); const offset = direction === "UP" ? -1 : 1; const newIndex = index + offset; if (newIndex >= 0 && newIndex < currentNames.length) { const newNames = [...currentNames]; [newNames[index], newNames[newIndex]] = [ newNames[newIndex], newNames[index], ]; names.set(newNames); selectedIndex.set(newIndex); } }); const addFriend = handler }>( (event, { names }) => { if (event?.key === "Enter") { const name = event?.target?.value?.trim(); if (name) { const currentNames = names.get(); names.set([...currentNames, name]); } } }, ); export default recipe("making lists - with add", (state) => { return { [UI]: (

My Friends

Click to select, Ctrl+Up/Down to reorder

    {/* Note: key is not needed for Common Tools but linters require it */} {state.names.map((name, index) => (
  • ))}
), names: state.names, selectedIndex: state.selectedIndex, }; });