Skip to content

Commit 456bcc2

Browse files
authored
Merge pull request #7 from upnotes-io/feature/multiline-paste
create multiple todos on multiline string paste
2 parents a896f96 + 918ca7f commit 456bcc2

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

src/components/Todo/Todo.tsx

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export interface TodoItem {
5656
}
5757

5858
export interface AddProps {
59-
addItem: (item: TodoItem) => void;
59+
addItem: (item: TodoItem | TodoItem[]) => void;
6060
}
6161
const Add = (props: AddProps) => {
6262
const classes = useStyles();
@@ -68,6 +68,25 @@ const Add = (props: AddProps) => {
6868
<AddIcon className={classes.plusIcon} />{' '}
6969
<FormControl fullWidth >
7070
<TextField
71+
onPaste={
72+
(e) =>{
73+
let clipboardData, pastedData;
74+
75+
// Stop data actually being pasted into div
76+
e.stopPropagation();
77+
e.preventDefault();
78+
79+
// Get pasted data via clipboard API
80+
clipboardData = e.clipboardData;
81+
pastedData = clipboardData.getData('Text').split('\n').reverse().filter((name)=> name.trim() !== "");
82+
83+
// Do whatever with pasteddata
84+
const items = pastedData.map((name)=> {
85+
return { name, uuid: uuid(), isComplete: false }
86+
})
87+
addItem(items);
88+
}
89+
}
7190
onChange={(e) => {
7291
addItem({ name: e.target.value, uuid: uuid(), isComplete: false });
7392
setItemName('');
@@ -85,7 +104,7 @@ const Add = (props: AddProps) => {
85104
interface ItemProps {
86105
items: TodoItem[];
87106
setItemsCallback: (updatedItems: TodoItem[]) => void;
88-
addItem: (item: TodoItem) => void;
107+
addItem: (item: TodoItem | TodoItem[]) => void;
89108
itemIndex: number;
90109
}
91110

@@ -113,11 +132,29 @@ const Item = (props: ItemProps) => {
113132
InputProps={{ classes: { underline: classes.underline } }}
114133
inputRef={inputRef}
115134
value={itemText} // innerHTML of the editable div
135+
onPaste={
136+
(e) =>{
137+
let clipboardData, pastedData;
138+
139+
// Stop data actually being pasted into div
140+
e.stopPropagation();
141+
e.preventDefault();
142+
143+
// Get pasted data via clipboard API
144+
clipboardData = e.clipboardData;
145+
pastedData = clipboardData.getData('Text').split('\n').reverse().filter((name)=> name.trim() !== "");
146+
147+
// Do whatever with pasteddata
148+
const items = pastedData.map((name)=> {
149+
return { name, uuid: uuid(), isComplete: false }
150+
})
151+
addItem(items);
152+
}
153+
}
116154
onChange={(e) => {
117155
items[itemIndex].name = e.target.value;
118156
setItemText(e.target.value);
119-
}} // handle innerHTML change
120-
// tagName='article' // Use a custom HTML tag (uses a div by default)
157+
}}
121158
onBlur={(e) => {
122159
setItemsCallback([...items])
123160
}}
@@ -169,9 +206,19 @@ export default (props: TodoAppProps) => {
169206
setItems(updatedItems);
170207
onChange(updatedItems);
171208
}
172-
const addItem = (item: TodoItem) => {
173-
items.unshift(item);
174-
setItemsCallback([...items])
209+
const addItem = (item: TodoItem | TodoItem[]) => {
210+
const itemsCopy = [...items];
211+
if(Array.isArray(item)){
212+
item.forEach(
213+
(it)=>{
214+
itemsCopy.unshift(it);
215+
}
216+
)
217+
setItemsCallback([...itemsCopy])
218+
}else {
219+
itemsCopy.unshift(item);
220+
setItemsCallback([...itemsCopy])
221+
}
175222
};
176223
const completedItemsLength = items.filter((item: TodoItem) => item.isComplete).length;
177224
return (

0 commit comments

Comments
 (0)