From 67cfcddb14fb5d57fdb10c546d54aee315c7fce9 Mon Sep 17 00:00:00 2001 From: Julien Barbay Date: Wed, 14 Nov 2018 19:33:38 +0900 Subject: [PATCH] modified render function to focus on Vue props --- app/vue/src/client/preview/render.js | 59 +++++++--------------------- 1 file changed, 15 insertions(+), 44 deletions(-) diff --git a/app/vue/src/client/preview/render.js b/app/vue/src/client/preview/render.js index 2ac18e1936d..d73d451a065 100644 --- a/app/vue/src/client/preview/render.js +++ b/app/vue/src/client/preview/render.js @@ -3,49 +3,6 @@ import Vue from 'vue'; let root = null; -function updateComponent(component) { - const [lastStory] = root.$children; - - if (!lastStory) { - return false; - } - - if (typeof component.data !== 'function') { - return false; - } - - const data = component.data(); - - if (!data) { - return false; - } - - Object.entries(data).forEach(([key, value]) => { - lastStory[key] = value; - }); - - lastStory.$forceUpdate(); - - return true; -} - -function renderRoot({ forceRender, component }) { - if (root) { - if (forceRender === true && updateComponent(component)) { - return; - } - - root.$destroy(); - } - - root = new Vue({ - el: '#root', - render(h) { - return h('div', { attrs: { id: 'root' } }, [h(component)]); - }, - }); -} - export default function render({ story, selectedKind, @@ -72,5 +29,19 @@ export default function render({ showMain(); - renderRoot({ forceRender, component }); + const props = Object.entries(component.props) + .map(([name, def]) => ({ [name]: def.default })) + .reduce((wrapper, prop) => ({ ...wrapper, ...prop }), {}); + + if (!root) { + root = new Vue({ + el: '#root', + render(h) { + return h('div', { attrs: { id: 'root' } }, [h(component, { props: this.proxy || null })]); + }, + }); + } else if (forceRender) { + root.proxy = props; + root.$forceUpdate(); + } }