-
Notifications
You must be signed in to change notification settings - Fork 39
Undo shortcut #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Undo shortcut #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import React, { useState } from "react"; | ||
| import React, { useState, useCallback, useEffect } from "react"; | ||
|
|
||
| import ExpandMoreIcon from "@material-ui/icons/ExpandMore"; | ||
| import { | ||
|
|
@@ -13,6 +13,7 @@ import { Reorder } from "framer-motion"; | |
| import { Item, TodoCompletedItem } from "./common"; | ||
| import { Form } from "./common/Todo/Form"; | ||
| import { TodoItem } from "./common/types"; | ||
| import { red } from "@material-ui/core/colors"; | ||
AnasMansy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const useStyles = makeStyles({ | ||
| accordion: { | ||
|
|
@@ -36,17 +37,24 @@ export interface TodoAppProps { | |
| defaultItems?: TodoItem[]; | ||
| onChange: (items: TodoItem[]) => void; | ||
| } | ||
|
|
||
| function TodoApp(props: TodoAppProps) { | ||
| const { defaultItems = [], onChange } = props; | ||
| const [items, setItems] = useState<TodoItem[]>(defaultItems); | ||
| const [undo, setUndo] = useState<TodoItem[]>(defaultItems); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be an array right? |
||
| const [redo, setRedo] = useState<TodoItem[]>(defaultItems); | ||
AnasMansy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const classes = useStyles(); | ||
| const setItemsCallback = (updatedItems: TodoItem[]) => { | ||
|
|
||
| setItems(updatedItems); | ||
| onChange(updatedItems); | ||
| setUndo(updatedItems) | ||
| }; | ||
|
|
||
| const addItem = (item: TodoItem | TodoItem[]) => { | ||
| const itemsCopy = [...items]; | ||
|
|
||
|
|
||
| if (Array.isArray(item)) { | ||
| item.forEach((it) => { | ||
| itemsCopy.unshift(it); | ||
|
|
@@ -67,14 +75,37 @@ function TodoApp(props: TodoAppProps) { | |
| const item = todoItems.find((item) => item.uuid === itemKey); | ||
| if (item) { | ||
| currItems.push(item); | ||
|
|
||
| } | ||
| return currItems; | ||
| }, | ||
| [] | ||
| ); | ||
| setItems([...updatedItems, ...completedItems]); | ||
|
|
||
|
|
||
| }; | ||
| const handleKeyPress = (event: any) => { | ||
| if (`${event.key}` == 'z' && `${event.key.control}`) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name of the function is misleading as keypress is another event. Also please use type for event instead of any. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i used " keyboardEvent" but throw error and shortcuts don't work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay. You can ignore the type issue. Please fix the undo behavier for a textfield and outside text field as mentioned in this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kamalkishor1991 while focus on item undo work on text otherwise on multiple items |
||
|
|
||
|
|
||
| undo.splice(0, 1); | ||
| setItems(undo); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| document.addEventListener('keydown', handleKeyPress); | ||
|
|
||
| return () => { | ||
| document.removeEventListener('keydown', handleKeyPress); | ||
| }; | ||
| }, [handleKeyPress]); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| return ( | ||
| <Container> | ||
| <Form addItem={addItem} /> | ||
|
|
@@ -105,6 +136,7 @@ function TodoApp(props: TodoAppProps) { | |
| > | ||
| <Typography> {completedItemsLength} Completed items </Typography> | ||
| </AccordionSummary> | ||
|
|
||
| <AccordionDetails className={classes.accordionDetails}> | ||
| {items.map((item, index) => { | ||
| return ( | ||
|
|
@@ -117,6 +149,7 @@ function TodoApp(props: TodoAppProps) { | |
| ); | ||
| })} | ||
| </AccordionDetails> | ||
|
|
||
| </Accordion> | ||
| )} | ||
| </Container> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.