Skip to content

Commit

Permalink
Add static methods to a component
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Jun 10, 2015
1 parent 6ece83f commit 537ec26
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/utils/statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function addStatics(obj) {
return (Component) => {
Object.keys(obj).forEach((key) => {
Component[key] = obj[key]
})
return Component
}
}

export default function statics(obj, Component) {
return Component
? addStatics(obj)(Component)
: addStatics(obj)
}
22 changes: 22 additions & 0 deletions test/statics-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Alt from '../'
import statics from '../utils/statics'
import { Component } from 'react'
import { assert } from 'chai'

const alt = new Alt()

@statics({
a() { }
})
class Foo extends Component { }

const Bar = statics({ b() { } }, Foo)

export default {
'statics': {
'static methods are added to a component'() {
assert.isFunction(Bar.a, 'works as decorator')
assert.isFunction(Bar.b, 'works as function')
}
},
}

0 comments on commit 537ec26

Please sign in to comment.