Created
April 17, 2015 03:32
-
-
Save dandelany/846adbb3ea4ef260628d to your computer and use it in GitHub Desktop.
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
// InterfaceMixin takes a list of string "interfaces" | |
// and adds a static called implementsInterface to the component that simply checks if an interface is in the list | |
// This way, a parent component can pass particular props only to children which implement the relevant interface | |
// by checking child.type.implementsInterface('SomeInterface') | |
// usage: | |
// mixins: [InterfaceMixin('SomeInterface')] // or... | |
// mixins: [InterfaceMixin(['SomeInterface', 'AnotherInterface'])] | |
var InterfaceMixin = function(interfaces) { | |
interfaces = isStr(interfaces) ? [interfaces] : interfaces; | |
return { | |
statics: { | |
implementsInterface(name) { return interfaces.indexOf(name) >= 0; } | |
} | |
} | |
}; | |
function isStr(s){ return typeof s === "string" || s instanceof String; } | |
module.exports = InterfaceMixin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment