Coder Social home page Coder Social logo

egoist-bot / babel-plugin-jsx-event-modifiers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nickmessing/babel-plugin-jsx-event-modifiers

0.0 2.0 0.0 35 KB

Event modifiers syntactic sugar for JSX

License: MIT License

JavaScript 100.00%

babel-plugin-jsx-event-modifiers's Introduction

Event Modifiers for JSX

This babel plugin adds some syntactic sugar to JSX.

Usage:

npm i babel-plugin-jsx-event-modifiers -D

or

yarn add babel-plugin-jsx-event-modifiers -D

Then add jsx-event-modifiers to your .babelrc file under plugins

example .babelrc (for vue):

{
  "presets": ["es2015"],
  "plugins": ["jsx-event-modifiers", "transform-vue-jsx"]
}

example .babelrc (for react):

{
  "presets": [
    "es2015",
    "react"
  ],
  "plugins": ["jsx-event-modifiers"]
}

Event Modifiers

Example:

export default {
  render () {
    return (
      <input
        onKeyUp:up={this.methodForPressingUp}
        onKeyUp:down={this.methodForPressingDown}
      />
    )
  }
}

will be transpiled into:

export default {
  render () {
    return (
      <input
        onKeyUp={event => {
          if (event.charCode === 38)
            this.methodForPressingUp(event);

          if (event.charCode === 40)
            this.methodForPressingDown(event);
        }} />
    );
  }
}

API:

Modifier Description
:stop executes event.stopPropagation()
:prevent executes event.preventDefault()
:k{keyCode} checks for the keyCode
:{keyAlias} checks for the keyCode that is assigned to this keyAlias
Stop

event.stopPropagation() is called before the expression

Example:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick:stop />
        <a href="/" onClick:stop={this.method} />
      </div>
    )
  }
}

is transpiled to:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick={event => {
          event.stopPropagation();
        }} />
        <a href="/" onClick={event => {
          event.stopPropagation();
          this.method(event);
        }} />
      </div>
    );
  }
}
Prevent

event.preventDefault() is called before the expression

Example:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick:prevent />
        <a href="/" onClick:prevent={this.method} />
      </div>
    )
  }
}

is transpiled to:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick={event => {
          event.preventDefault();
        }} />
        <a href="/" onClick={event => {
          event.preventDefault();
          this.method(event);
        }} />
      </div>
    );
  }
}
KeyCode

event.charCode is compared to the keyCode

Example:

export default {
  render () {
    return <input onKeyUp:k13={this.method} />
  }
}

is transpiled to:

export default {
  render () {
    return (
      <input onKeyUp={event => {
        if (event.charCode === 13)
          this.method(event);
      }} />
    );
  }
}
KeyAlias

There is a predefined list of aliases for keycodes:

const aliases = {
  esc: 27,
  tab: 9,
  enter: 13,
  space: 32,
  up: 38,
  left: 37,
  right: 39,
  down: 40,
  'delete': [8, 46]
}

Example:

export default {
  render () {
    return <input onKeyUp:enter={this.method} />
  }
}

is transpiled to:

export default {
  render () {
    return (
      <input onKeyUp={event => {
        if (event.charCode === 13)
          this.method(event);
      }} />
    );
  }
}

You can combine them:

Example:

export default {
  render () {
    return <input
      onKeyUp:enter={this.method}
      onKeyUp:k60={this.otherMethod} />
  }
}

is transpiled to:

export default {
  render () {
    return (
      <input
        onKeyUp={event => {
          if (event.charCode === 13)
            this.method(event);

          if (event.charCode === 60)
            this.otherMethod(event);
        }} />
    );
  }
}

babel-plugin-jsx-event-modifiers's People

Contributors

nickmessing avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.