Skip to content

Commit

Permalink
Add state auto controls
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoattal committed Feb 4, 2024
1 parent 52253d3 commit 483d962
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 1 deletion.
31 changes: 31 additions & 0 deletions examples/vue3/src/components/Autoprops.story.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script lang="ts" setup>

Check warning on line 1 in examples/vue3/src/components/Autoprops.story.vue

View workflow job for this annotation

GitHub Actions / Build and test

Component name "Autoprops.story" should always be multi-word

Check warning on line 1 in examples/vue3/src/components/Autoprops.story.vue

View workflow job for this annotation

GitHub Actions / Build and test

Component name "Autoprops.story" should always be multi-word
import Autoprops from './Autoprops.vue'
function initState () {
return {
name: 'Fry',
}
}
</script>

<template>
<Story
title="Autoprops"
:layout="{
type: 'grid',
width: 200,
}"
>
<Variant title="Naked">
<Autoprops />
</Variant>
<Variant
title="State"
:init-state="initState"
>
<template #default="{ state }">
<Autoprops :name="state.name" />
</template>
</Variant>
</Story>
</template>
12 changes: 12 additions & 0 deletions examples/vue3/src/components/Autoprops.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts" setup>

Check warning on line 1 in examples/vue3/src/components/Autoprops.vue

View workflow job for this annotation

GitHub Actions / Build and test

Component name "Autoprops" should always be multi-word

Check warning on line 1 in examples/vue3/src/components/Autoprops.vue

View workflow job for this annotation

GitHub Actions / Build and test

Component name "Autoprops" should always be multi-word
defineProps({
name: {
type: String,
default: 'world',
},
})
</script>

<template>
Hello {{ name }}!
</template>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { computed, ref } from 'vue'
import { computed } from 'vue'
import { Icon } from '@iconify/vue'
import type { AutoPropComponentDefinition, PropDefinition } from '@histoire/shared'
import { HstCheckbox, HstJson, HstNumber, HstText } from '@histoire/controls'
Expand Down Expand Up @@ -54,6 +54,7 @@ const canReset = computed(() => props.variant.state?._hPropState?.[props.compone
:is="comp"
v-if="comp"
v-model="model"
:placeholder="model === undefined ? definition?.default : null"
class="histoire-controls-component-prop-item"
:title="`${definition.name}${canReset ? ' *' : ''}`"
>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<script setup lang="ts">
import { computed } from 'vue'
import { Icon } from '@iconify/vue'
import type { Variant } from '@histoire/shared'
import ControlsComponentStateItem from './ControlsComponentStateItem.vue'
const props = defineProps<{
variant: Variant
}>()
const stateKeys = computed(() => Object.keys(props.variant.state || {})
.filter((key) => !key.startsWith('_h')))
</script>

<template>
<div class="histoire-controls-component-init-state">
<div class="htw-p-2 htw-flex htw-items-center htw-gap-1">
<Icon
v-tooltip="'Auto-detected state'"
icon="carbon:data-vis-1"
class="htw-w-4 htw-h-4 htw-text-primary-500 htw-flex-none"
/>
<div>
State
</div>
</div>
<ControlsComponentStateItem
v-for="key of stateKeys"
:key="key"
:item="key"
:variant="variant"
/>
</div>
</template>

<style scoped>
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script lang="ts" setup>
import { computed } from 'vue'
import { HstCheckbox, HstJson, HstNumber, HstText } from '@histoire/controls'
import type { Variant } from '../../types'
const props = defineProps<{
variant: Variant
item: string
}>()
const comp = computed(() => {
switch (typeof props.variant.state[props.item]) {
case 'string':
return HstText
case 'number':
return HstNumber
case 'boolean':
return HstCheckbox
case 'object':
default:
return HstJson
}
})
</script>

<template>
<component
:is="comp"
v-if="comp"
v-model="variant.state[props.item]"

Check failure on line 30 in packages/histoire-app/src/app/components/panel/ControlsComponentStateItem.vue

View workflow job for this annotation

GitHub Actions / Build and test

Unexpected mutation of "variant" prop

Check failure on line 30 in packages/histoire-app/src/app/components/panel/ControlsComponentStateItem.vue

View workflow job for this annotation

GitHub Actions / Build and test

Unexpected mutation of "variant" prop
class="histoire-controls-component-prop-item"
:title="props.item"
/>
</template>
17 changes: 17 additions & 0 deletions packages/histoire-app/src/app/components/panel/StoryControls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Story, Variant } from '../../types'
import BaseEmpty from '../base/BaseEmpty.vue'
import StatePresets from './StatePresets.vue'
import ControlsComponentProps from './ControlsComponentProps.vue'
import ControlsComponentState from './ControlsComponentState.vue'
const props = defineProps({
variant: {
Expand All @@ -27,6 +28,12 @@ watch(() => props.variant, () => {
})
const hasCustomControls = computed(() => props.variant.slots().controls || props.story.slots().controls)
const hasInitState = computed(() => Object
.entries(props.variant.state || {})
.filter(([key]) => !key.startsWith('_h'))
.length > 0)
</script>

<template>
Expand Down Expand Up @@ -56,6 +63,16 @@ const hasCustomControls = computed(() => props.variant.slots().controls || props
@ready="ready = true"
/>

<!-- Init state -->
<div
v-else-if="hasInitState"
>
<ControlsComponentState
class="htw-flex-none htw-my-2"
:variant="variant"
/>
</div>

<BaseEmpty v-else-if="!variant.state?._hPropDefs?.length">
<Icon
icon="carbon:audio-console"
Expand Down

0 comments on commit 483d962

Please sign in to comment.