Skip to content

Instantly share code, notes, and snippets.

@dandelany
Created April 17, 2015 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dandelany/846adbb3ea4ef260628d to your computer and use it in GitHub Desktop.
Save dandelany/846adbb3ea4ef260628d to your computer and use it in GitHub Desktop.
// 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