Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server: Serialize Object controls as JSON over the wire #11703

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the fix

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"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't testing object controls in the example app

},
"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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proves we have it as json that we can parse


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}