Explorar el Código

Update: Enter触发submit事件,不是订阅Enter事件

Evo hace 4 años
padre
commit
556bf1c6ca
Se han modificado 2 ficheros con 8 adiciones y 15 borrados
  1. 7
    13
      src/components/Editor/index.js
  2. 1
    2
      src/index.js

+ 7
- 13
src/components/Editor/index.js Ver fichero

@@ -71,7 +71,7 @@ class Editor extends React.Component {
71 71
   }
72 72
 
73 73
   componentDidMount() {
74
-    const { app, onRef, allowEnter, onPressEnter } = this.props;
74
+    const { app, onRef } = this.props;
75 75
     if (
76 76
       app.currentUser &&
77 77
       (app.currentUser.user_id > 0 || app.currentUser.id > 0)
@@ -81,9 +81,6 @@ class Editor extends React.Component {
81 81
     if (isFunction(onRef)) {
82 82
       onRef(this);
83 83
     }
84
-
85
-    if ((allowEnter && !onPressEnter) || (!allowEnter && onPressEnter))
86
-      console.error("`onPressEnter` must be undefined when `allowEnter`!");
87 84
   }
88 85
 
89 86
   handleEmojiScroll(e) {
@@ -298,17 +295,16 @@ class Editor extends React.Component {
298 295
 
299 296
   /**
300 297
    *  **处理Enter事件**
301
-   *  1. `allowEnter` & `onPressEnter`同时有效时才能触发
298
+   *  1. `allowEnterSubmit`为true时enter触发submit事件
302 299
    *  2. `e.preventDefault`为了防止enter事件后仍触发换行
303
-   *  3. enter事件开启,仍可以用`shift + enter`触发换行
300
+   *  3. enter事件开启,仍可以用`shift + enter`触发换行
304 301
    *  -- evo 20200222
305 302
    */
306 303
   handlePressEnter = e => {
307
-    const { allowEnter, onPressEnter } = this.props;
308
-    if (allowEnter && onPressEnter) {
304
+    if (this.props.allowEnterSubmit) {
309 305
       if (!e.shiftKey) {
310 306
         e.preventDefault();
311
-        onPressEnter();
307
+        this.handleSubmit();
312 308
       }
313 309
     }
314 310
   };
@@ -530,8 +526,7 @@ Editor.propTypes = {
530 526
   onError: PropTypes.func,
531 527
   maxLength: PropTypes.number,
532 528
   // Enter事件相关
533
-  allowEnter: PropTypes.bool,
534
-  onPressEnter: PropTypes.func
529
+  allowEnterSubmit: PropTypes.bool
535 530
 };
536 531
 
537 532
 Editor.defaultProps = {
@@ -553,8 +548,7 @@ Editor.defaultProps = {
553 548
   app: {},
554 549
   handleChangeFileList: () => {},
555 550
   // Enter事件相关
556
-  allowEnter: false,
557
-  onPressEnter: undefined
551
+  allowEnterSubmit: false
558 552
 };
559 553
 
560 554
 export default Comment(Editor);

+ 1
- 2
src/index.js Ver fichero

@@ -43,8 +43,7 @@ class Index extends React.Component {
43 43
               fileList
44 44
             });
45 45
           }}
46
-          allowEnter
47
-          onPressEnter={e => alert("enter pressed")}
46
+          allowEnterSubmit
48 47
         />
49 48
       </App>
50 49
     );