VNodeRenderer.vue 1011 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <script lang="ts">
  2. import { defineComponent, h, type PropType, type VNode } from 'vue'
  3. export default defineComponent({
  4. name: 'VNodeRenderer',
  5. props: {
  6. vnode: {
  7. type: Object as PropType<VNode>,
  8. default: null
  9. },
  10. render: {
  11. type: Function as PropType<(data: Record<string, unknown>|null) => VNode>,
  12. default: null
  13. },
  14. renderChild: {
  15. type: Boolean,
  16. default: null
  17. },
  18. data: {
  19. type: Object as PropType<Record<string, unknown>>,
  20. default: null
  21. },
  22. },
  23. render() {
  24. if(this.vnode) {
  25. if (typeof this.vnode === 'string')
  26. return h('div', this.vnode);
  27. const props = this.vnode.props;
  28. if(props)
  29. for(let key in this.data)
  30. props[key] = this.data[key];
  31. return this.renderChild ?
  32. h('div', { style: { height: '100%'} }, [
  33. this.vnode
  34. ]) :
  35. this.vnode;
  36. }
  37. if(this.render) {
  38. return this.render(this.data);
  39. }
  40. return h('div');
  41. }
  42. })
  43. </script>