Skip to content

Commit

Permalink
Merge pull request #11703 from jonspalmer/server_object_controls
Browse files Browse the repository at this point in the history
Server: Serialize Object controls as JSON over the wire
  • Loading branch information
shilman committed Jul 29, 2020
2 parents c3ccb70 + 07a23fe commit 7877bf3
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/server/src/client/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ClientStoryApi, Loadable } from '@storybook/addons';

import './globals';
import { renderMain as render } from './render';
import { StoryFnServerReturnType, IStorybookSection, ConfigureOptionsArgs } from './types';
import { StoryFnServerReturnType, IStorybookSection } from './types';

const framework = 'server';

Expand Down
9 changes: 7 additions & 2 deletions app/server/src/client/preview/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ const buildStoryArgs = (args: Args, argTypes: ArgTypes) => {
const argType = argTypes[key];
const { control } = argType;
const controlType = control && control.type.toLowerCase();
const argValue = storyArgs[key];
switch (controlType) {
case 'date':
// For cross framework & language support we pick a consistent representation of Dates as strings
storyArgs[key] = new Date(storyArgs[key]).toISOString();
storyArgs[key] = new Date(argValue).toISOString();
break;
case 'array': {
// use the supplied separator when seriazlizing an array as a string
const separator = control.separator || ',';
storyArgs[key] = storyArgs[key].join(separator);
storyArgs[key] = argValue.join(separator);
break;
}
case 'object':
// send objects as JSON strings
storyArgs[key] = JSON.stringify(argValue);
break;
default:
}
});
Expand Down
4 changes: 0 additions & 4 deletions app/server/src/client/preview/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ export interface ShowErrorArgs {
title: string;
description: string;
}

export interface ConfigureOptionsArgs {
fetchStoryHtml: FetchStoryHtmlType;
}
2 changes: 0 additions & 2 deletions app/server/src/lib/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@ export interface StorybookSection {
stories: StorybookStory[];
[x: string]: any;
}

export type Decorator = (section: StorybookSection) => StorybookSection;
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"colour": "deeppink",
"today": "Jan 20 2017 GMT+0",
"items": ["Laptop", "Book", "Whiskey"],
"nice": true
"nice": true,
"other": {"hair": "brown", "eyes": "blue"}
},
"argTypes": {
"stock": { "control": { "type": "range", "min": 0, "max": 30, "step": 5} },
Expand Down
5 changes: 5 additions & 0 deletions examples/server-kitchen-sink/views/addons/controls/all.pug
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- style = `border: 2px dotted ${colour}; padding: 8px 22px; border-radius: 8px`;
- today = new Date(today);
- items = items.split(',');
- other = JSON.parse(other)

div(style=`${style}`)
h1 My name is #{name},
Expand All @@ -15,4 +16,8 @@ div(style=`${style}`)
ul
each item in items
li= item
p Other things about me:
ul
each key in Object.keys(other)
li= `${key}: ${other[key]}`
p #{salutation}

0 comments on commit 7877bf3

Please sign in to comment.