Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
babel-plugin-jsx-event-modifiers
Advanced tools
This babel plugin adds some syntactic sugar to JSX.
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"]
}
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);
}} />
);
}
}
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 |
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>
);
}
}
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>
);
}
}
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);
}} />
);
}
}
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);
}} />
);
}
}
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);
}} />
);
}
}
FAQs
JSX Syntactic Sugar Plugin for Event Modifiers
The npm package babel-plugin-jsx-event-modifiers receives a total of 62,051 weekly downloads. As such, babel-plugin-jsx-event-modifiers popularity was classified as popular.
We found that babel-plugin-jsx-event-modifiers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.