Created
March 27, 2019 09:21
-
-
Save SherryQueen/f5250b84486aca3440142639515ad12b to your computer and use it in GitHub Desktop.
react 防误触实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| export type AntiMissingProps = { | |
| once?: boolean; | |
| timeout?: number; | |
| eventName?: string; | |
| }; | |
| const defaultOption = { eventName: 'onClick', timeout: 1000, once: false }; | |
| const AntiMissing = (option: AntiMissingProps) => { | |
| const { eventName, timeout, once } = { ...defaultOption, ...option }; | |
| return (WrappedComponent: any) => { | |
| class AntiMissingComponent extends React.PureComponent<any, any> { | |
| _disabled: boolean = false; | |
| onAntiMissing = (...args: any[]) => { | |
| if (this._disabled) return; | |
| this._disabled = true; | |
| setTimeout(() => (this._disabled = once || false), timeout); | |
| this.props[eventName!] && this.props[eventName!].apply(this, args); | |
| }; | |
| render() { | |
| const props = (this as AntiMissingComponent).props; | |
| return <WrappedComponent {...props} {...{ [eventName!]: this.onAntiMissing }} />; | |
| } | |
| } | |
| return AntiMissingComponent; | |
| }; | |
| }; | |
| export default AntiMissing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment