Last active
September 16, 2018 23:07
-
-
Save brendtumi/b2c55e279401cfeaacab38657aa021a0 to your computer and use it in GitHub Desktop.
Vue.js higher-order component (HOC)
This file contains 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
import Vue from 'vue'; | |
// Test : https://jsfiddle.net/brend/xe75nmdz/ | |
// Note: https://github.com/vuejs/vue/issues/6201 | |
const HOC = component => { | |
const inheritedProps = (typeof component === 'function' ? component.options.props : component.props) || []; | |
return Vue.component('HOC', { | |
mounted() {}, | |
data() { | |
return {}; | |
}, | |
props: { ...inheritedProps, value: null }, | |
render(createElement) { | |
// const parentCreateElement = this.$parent.$createElement; | |
const slots = this.$slots; | |
const scopedSlots = {}; | |
Object.keys(slots).map(key => (scopedSlots[key] = () => slots[key])); | |
return createElement( | |
component, | |
{ | |
attrs: { ...this.$attrs }, | |
props: { ...this.$props }, | |
on: { | |
...this.$listeners, | |
input: value => { | |
this.value = value; | |
this.$emit('input', value); | |
}, | |
}, | |
scopedSlots, | |
domProps: { | |
value: this.value, | |
}, | |
}, | |
slots, | |
); | |
}, | |
}); | |
}; | |
export default HOC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment