项目作者: hiroppy

项目描述 :
Decorator for keybinding
高级语言: TypeScript
项目地址: git://github.com/hiroppy/keybinding-decorator.git
创建时间: 2017-08-16T08:51:32Z
项目社区:https://github.com/hiroppy/keybinding-decorator

开源协议:MIT License

下载



keybinding-decorator



Decorator for Keybinding

Build Status
npm version

keybinding-decorator is using Mousetrap.

Decorators offer a convenient declarative syntax to modify the shape of class declarations.
see: https://tc39.github.io/proposal-decorators/

You must use babel-plugin-transform-decorators-legacy.

Install

  1. $ npm install keybinding-decorator --save

Usage

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import keybind from 'keybinding-decorator';
  4. class Main extends React.Component {
  5. constructor() {
  6. super();
  7. this.state = { current: '' };
  8. // init
  9. Reflect.apply(this.csk, this, []);
  10. Reflect.apply(this.esc, this, []);
  11. }
  12. @keybind('command+shift+k')
  13. csk() {
  14. this.setState({ current: 'command+shift+k' });
  15. }
  16. @keybind('esc')
  17. esc() {
  18. this.setState({ current: 'esc' });
  19. }
  20. componentWillUnmount() {
  21. this.esc.unbind();
  22. this.csk.unbind();
  23. }
  24. render() {
  25. return <div>current: {this.state.current}</div>;
  26. }
  27. }
  28. const root = () => <Main ></Main>;
  29. ReactDOM.render(root(), document.getElementById('root'));

Method

unbind()

A method binded by decorator has unbind method.
Release this function from Mousetrap.