Skip to content

Commit

Permalink
fix(vue2): shared controls not working, fix #266
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Dec 19, 2022
1 parent 3e85496 commit 2210c51
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
64 changes: 64 additions & 0 deletions examples/vue2/src/SharedControls.story.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<script lang="ts" setup>
import { reactive } from 'vue'
const options = [
{ label: 'Crash Bandicoot', value: 'crash-bandicoot' },
{ label: 'The Last of Us', value: 'the-last-of-us' },
{ label: 'Ghost of Tsushima', value: 'ghost-of-tsushima' },
]
const state = reactive({
text: 'Hello',
checkbox: false,
number: 20,
longText: 'Longer text...',
select: 'crash-bandicoot',
fn: () => { /* noop */ },
})
defineExpose({
state,
})
</script>

<template>
<Story title="Shared Controls">
<template #controls>
<HstText
v-model="state.text"
title="HstText"
/>
<HstCheckbox
v-model="state.checkbox"
title="HstCheckbox"
/>
<HstNumber
v-model="state.number"
title="HstNumber"
/>
<HstTextarea
v-model="state.longText"
title="HstTextarea"
/>
<HstSelect
v-model="state.select"
title="HstSelect"
:options="options"
/>
</template>

<Variant
title="variant 1"
>
<h1>Variant 1</h1>
<pre>{{ state }}</pre>
</Variant>

<Variant
title="variant 2"
>
<h1>Variant 2</h1>
<pre>{{ state }}</pre>
</Variant>
</Story>
</template>
14 changes: 12 additions & 2 deletions packages/histoire-plugin-vue2/src/client/app/RenderStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,19 @@ export default _defineComponent({
},

render: () => {
const vnodes = props.variant.slots()?.[props.slotName]?.({
const ensureFn = (fn: any) => {
if (typeof fn === 'function') {
return fn
} else if (fn != null) {
return () => fn
} else {
return null
}
}

const vnodes = ensureFn(props.variant.slots()?.[props.slotName])?.({
state: externalState,
}) ?? props.story.slots()?.[props.slotName]?.({
}) ?? ensureFn(props.story.slots()?.[props.slotName])?.({
state: externalState,
})

Expand Down

0 comments on commit 2210c51

Please sign in to comment.