Skip to content

Instantly share code, notes, and snippets.

@brendtumi
Last active September 16, 2018 23:07
Show Gist options
  • Save brendtumi/b2c55e279401cfeaacab38657aa021a0 to your computer and use it in GitHub Desktop.
Save brendtumi/b2c55e279401cfeaacab38657aa021a0 to your computer and use it in GitHub Desktop.
Vue.js higher-order component (HOC)
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