From ee8335856c7c1acecb6b448864772e95e7521255 Mon Sep 17 00:00:00 2001 From: afc163 Date: Tue, 18 Aug 2015 13:15:43 +0800 Subject: [PATCH] Only listen mousemove once MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 初始化时监听, 共享同一个 mousePosition --- components/modal/demo/basic.md | 1 - components/modal/eventListener.js | 14 ++++++++++++++ components/modal/index.jsx | 28 ++++++++-------------------- 3 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 components/modal/eventListener.js diff --git a/components/modal/demo/basic.md b/components/modal/demo/basic.md index c8cf14d7ab..6fc34df0bb 100644 --- a/components/modal/demo/basic.md +++ b/components/modal/demo/basic.md @@ -43,4 +43,3 @@ var Test = React.createClass({ React.render( , document.getElementById('components-modal-demo-basic')); ```` - diff --git a/components/modal/eventListener.js b/components/modal/eventListener.js new file mode 100644 index 0000000000..90f79085b8 --- /dev/null +++ b/components/modal/eventListener.js @@ -0,0 +1,14 @@ +function wrap(standard, fallback) { + return function (el, evtName, listener, useCapture) { + if (el[standard]) { + el[standard](evtName, listener, useCapture); + } else if (el[fallback]) { + el[fallback]('on' + evtName, listener); + } + }; +} + +export default { + add: wrap('addEventListener', 'attachEvent'), + remove: wrap('removeEventListener', 'detachEvent') +}; diff --git a/components/modal/index.jsx b/components/modal/index.jsx index d53dbfe695..5079a6cbab 100644 --- a/components/modal/index.jsx +++ b/components/modal/index.jsx @@ -1,22 +1,18 @@ import React from 'react'; import Dialog from 'rc-dialog'; +import eventListener from './eventListener'; function noop() { } -function wrap(standard, fallback) { - return function (el, evtName, listener, useCapture) { - if (el[standard]) { - el[standard](evtName, listener, useCapture); - } else if (el[fallback]) { - el[fallback]('on' + evtName, listener); - } +let mousePosition; +function onDocumentMousemove(e) { + mousePosition = { + x: e.pageX, + y: e.pageY }; } -let eventListener = { - add: wrap('addEventListener', 'attachEvent'), - remove: wrap('removeEventListener', 'detachEvent') -}; +eventListener.add(document, 'mousemove', onDocumentMousemove); export default React.createClass({ getDefaultProps() { @@ -41,14 +37,6 @@ export default React.createClass({ }; }, - componentWillMount() { - eventListener.add(document, 'mousemove', this.onDocumentMousemove); - }, - - componentWillUnmount() { - eventListener.remove(document, 'mousemove', this.onDocumentMousemove); - }, - handleCancel() { this.props.onCancel(); this.setState({ @@ -92,6 +80,6 @@ export default React.createClass({ let visible = this.state.visible; return ; + visible={visible} mousePosition={mousePosition} />; } });