Nuxt/examples/storybook/stories/collection.story.js

228 lines
6.7 KiB
JavaScript
Raw Normal View History

2018-02-06 03:06:45 +00:00
import Vuex from 'vuex'
import { storiesOf } from '@storybook/vue'
import { action } from '@storybook/addon-actions'
import { linkTo } from '@storybook/addon-links'
import { withNotes } from '@storybook/addon-notes'
2018-02-06 02:36:22 +00:00
import {
withKnobs,
text,
number,
boolean,
array,
select,
color,
date,
2018-02-06 03:06:45 +00:00
button
} from '@storybook/addon-knobs/vue'
import MyButton from '~/components/Button.vue'
2018-02-06 02:36:22 +00:00
storiesOf('Features/Method for rendering Vue', module)
.add('render', () => ({
2018-02-06 03:06:45 +00:00
render: h => h('div', ['renders a div with some text in it..'])
2018-02-06 02:36:22 +00:00
}))
.add('render + component', () => ({
render(h) {
2018-02-06 03:06:45 +00:00
return h(MyButton, { props: { color: 'pink' } }, ['renders component: MyButton'])
}
2018-02-06 02:36:22 +00:00
}))
.add('template', () => ({
template: `
<div>
<h1>A template</h1>
<p>rendered in vue in storybook</p>
2018-02-06 03:06:45 +00:00
</div>`
2018-02-06 02:36:22 +00:00
}))
.add('template + component', () => ({
components: { MyButton },
2018-02-06 03:06:45 +00:00
template: '<my-button>MyButton rendered in a template</my-button>'
2018-02-06 02:36:22 +00:00
}))
.add('template + methods', () => ({
components: { MyButton },
template: `
<p>
<em>Clicking the button will navigate to another story using the 'addon-links'</em><br/>
<my-button :rounded="true" :handle-click="action">MyButton rendered in a template + props & methods</my-button>
</p>`,
methods: {
2018-02-06 03:06:45 +00:00
action: linkTo('Button')
}
2018-02-06 02:36:22 +00:00
}))
.add('JSX', () => ({
components: { MyButton },
render() {
2018-02-06 03:06:45 +00:00
return <my-button>MyButton rendered with JSX</my-button>
}
2018-02-06 02:36:22 +00:00
}))
.add('vuex + actions', () => ({
components: { MyButton },
template: '<my-button :handle-click="log">with vuex: {{ $store.state.count }}</my-button>',
store: new Vuex.Store({
state: { count: 0 },
mutations: {
increment(state) {
state.count += 1; // eslint-disable-line
2018-02-06 03:06:45 +00:00
action('vuex state')(state)
}
}
2018-02-06 02:36:22 +00:00
}),
methods: {
log() {
2018-02-06 03:06:45 +00:00
this.$store.commit('increment')
}
}
2018-02-06 02:36:22 +00:00
}))
.add('whatever you want', () => ({
components: { MyButton },
template:
'<my-button :handle-click="log">with awesomeness: {{ $store.state.count }}</my-button>',
store: new Vuex.Store({
state: { count: 0 },
mutations: {
increment(state) {
state.count += 1; // eslint-disable-line
2018-02-06 03:06:45 +00:00
action('vuex state')(state)
}
}
2018-02-06 02:36:22 +00:00
}),
methods: {
log() {
2018-02-06 03:06:45 +00:00
this.$store.commit('increment')
}
}
2018-02-06 02:36:22 +00:00
}))
.add('pre-registered component', () => ({
/* By pre-registering component in config.js,
* the need to register all components with each story is removed.
* You'll only need the template */
template: `
<p>
<em>This component was pre-registered in .storybook/config.js</em><br/>
<my-button>MyButton rendered in a template</my-button>
2018-02-06 03:06:45 +00:00
</p>`
}))
2018-02-06 02:36:22 +00:00
2018-02-06 03:06:45 +00:00
storiesOf('Features/Decorator for Vue', module)
.addDecorator((story) => {
2018-02-06 02:36:22 +00:00
// Decorated with story function
2018-02-06 03:06:45 +00:00
const WrapButton = story()
2018-02-06 02:36:22 +00:00
return {
components: { WrapButton },
template: '<div :style="{ border: borderStyle }"><wrap-button/></div>',
data() {
2018-02-06 03:06:45 +00:00
return { borderStyle: 'medium solid red' }
}
}
2018-02-06 02:36:22 +00:00
})
.addDecorator(() => ({
// Decorated with `story` component
template: '<div :style="{ border: borderStyle }"><story/></div>',
data() {
return {
2018-02-06 03:06:45 +00:00
borderStyle: 'medium solid blue'
}
}
2018-02-06 02:36:22 +00:00
}))
.add('template', () => ({
2018-02-06 03:06:45 +00:00
template: '<my-button>MyButton with template</my-button>'
2018-02-06 02:36:22 +00:00
}))
.add('render', () => ({
render(h) {
2018-02-06 03:06:45 +00:00
return h(MyButton, { props: { color: 'pink' } }, ['renders component: MyButton'])
}
}))
2018-02-06 02:36:22 +00:00
storiesOf('Features/Addon Actions', module)
.add('Action only', () => ({
template: '<my-button :handle-click="log">Click me to log the action</my-button>',
methods: {
2018-02-06 03:06:45 +00:00
log: action('log1')
}
2018-02-06 02:36:22 +00:00
}))
.add('Action and method', () => ({
template: '<my-button :handle-click="log">Click me to log the action</my-button>',
methods: {
log: (e) => {
2018-02-06 03:06:45 +00:00
e.preventDefault()
action('log2')(e.target)
}
}
}))
2018-02-06 02:36:22 +00:00
storiesOf('Features/Addon Notes', module)
.add(
'Simple note',
withNotes({ text: 'My notes on some bold text' })(() => ({
template:
2018-02-06 03:06:45 +00:00
'<p><strong>Etiam vulputate elit eu venenatis eleifend. Duis nec lectus augue. Morbi egestas diam sed vulputate mollis. Fusce egestas pretium vehicula. Integer sed neque diam. Donec consectetur velit vitae enim varius, ut placerat arcu imperdiet. Praesent sed faucibus arcu. Nullam sit amet nibh a enim eleifend rhoncus. Donec pretium elementum leo at fermentum. Nulla sollicitudin, mauris quis semper tempus, sem metus tristique diam, efficitur pulvinar mi urna id urna.</strong></p>'
2018-02-06 02:36:22 +00:00
}))
)
.add(
'Note with HTML',
withNotes({
text: `
<h2>My notes on emojies</h2>
<em>It's not all that important to be honest, but..</em>
Emojis are great, I love emojis, in fact I like using them in my Component notes too! 😇
2018-02-06 03:06:45 +00:00
`
2018-02-06 02:36:22 +00:00
})(() => ({
2018-02-06 03:06:45 +00:00
template: '<p>🤔😳😯😮<br/>😄😩😓😱<br/>🤓😑😶😊</p>'
2018-02-06 02:36:22 +00:00
}))
2018-02-06 03:06:45 +00:00
)
2018-02-06 02:36:22 +00:00
storiesOf('Features/ Addon Knobs', module)
.addDecorator(withKnobs)
.add('Simple', () => {
2018-02-06 03:06:45 +00:00
const name = text('Name', 'John Doe')
const age = number('Age', 44)
const content = `I am ${name} and I'm ${age} years old.`
2018-02-06 02:36:22 +00:00
return {
2018-02-06 03:06:45 +00:00
template: `<div>${content}</div>`
}
2018-02-06 02:36:22 +00:00
})
.add('All knobs', () => {
2018-02-06 03:06:45 +00:00
const name = text('Name', 'Jane')
2018-02-06 02:36:22 +00:00
const stock = number('Stock', 20, {
range: true,
min: 0,
max: 30,
2018-02-06 03:06:45 +00:00
step: 5
})
2018-02-06 02:36:22 +00:00
const fruits = {
apples: 'Apple',
bananas: 'Banana',
2018-02-06 03:06:45 +00:00
cherries: 'Cherry'
}
const fruit = select('Fruit', fruits, 'apple')
const price = number('Price', 2.25)
2018-02-06 02:36:22 +00:00
2018-02-06 03:06:45 +00:00
const colour = color('Border', 'deeppink')
const today = date('Today', new Date('Jan 20 2017'))
const items = array('Items', ['Laptop', 'Book', 'Whiskey'])
const nice = boolean('Nice', true)
2018-02-06 02:36:22 +00:00
const stockMessage = stock
? `I have a stock of ${stock} ${fruit}, costing &dollar;${price} each.`
2018-02-06 03:06:45 +00:00
: `I'm out of ${fruit}${nice ? ', Sorry!' : '.'}`
const salutation = nice ? 'Nice to meet you!' : 'Leave me alone!'
2018-02-06 02:36:22 +00:00
2018-02-06 03:06:45 +00:00
button('Arbitrary action', action('You clicked it!'))
2018-02-06 02:36:22 +00:00
return {
template: `
<div style="border:2px dotted ${colour}; padding: 8px 22px; border-radius: 8px">
<h1>My name is ${name},</h1>
<h3>today is ${new Date(today).toLocaleDateString()}</h3>
<p>${stockMessage}</p>
<p>Also, I have:</p>
<ul>
${items.map(item => `<li key=${item}>${item}</li>`).join('')}
</ul>
<p>${salutation}</p>
</div>
2018-02-06 03:06:45 +00:00
`
}
})