浏览代码

don’t use onload, because fileCard is actually always in DOM

@goto-bus-stop could you help a bit here? I’m confused as to how to
remove an event listener when FileCard is closed, given that data
needed for that event listener lives in closure. Now it seems to me
that every time FileCard is open, a new listener is added
Artur Paikin 7 年之前
父节点
当前提交
2f0d1b8581
共有 1 个文件被更改,包括 9 次插入4 次删除
  1. 9 4
      src/plugins/Dashboard/FileCard.js

+ 9 - 4
src/plugins/Dashboard/FileCard.js

@@ -8,14 +8,19 @@ const meta = {}
 
 module.exports = function fileCard (props) {
   file = props.fileCardFor ? props.files[props.fileCardFor] : false
-  // const meta = {}
 
   const handleEnterKey = (ev) => {
-    if (event.keyCode === 13) {
+    if (file && event.keyCode === 13) {
       props.done(meta, file.id)
     }
   }
 
+  if (file) {
+    document.body.addEventListener('keyup', handleEnterKey)
+  } else {
+    document.body.removeEventListener('keyup', handleEnterKey)
+  }
+
   const tempStoreMeta = (ev) => {
     const value = ev.target.value
     const name = ev.target.dataset.name
@@ -73,8 +78,8 @@ module.exports = function fileCard (props) {
   </div>`
 
   return onload(html`<div>${fileCardEl()}</div>`,
-    () => document.body.addEventListener('keyup', handleEnterKey),
-    () => document.body.removeEventListener('keyup', handleEnterKey),
+    null,
+    null,
     `uppy${file.id}`
   )
 }