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

date knob incorrectly deserializes value #4

Open
cellog opened this issue Apr 13, 2021 · 2 comments
Open

date knob incorrectly deserializes value #4

cellog opened this issue Apr 13, 2021 · 2 comments

Comments

@cellog
Copy link

cellog commented Apr 13, 2021

Describe the bug
https://github.com/storybookjs/storybook/blob/next/addons/knobs/src/converters.ts#L18 has this deserialization code:

 (value: any): number => new Date(value).getTime() || new Date().getTime(),

However, since value is a Date when used in the iframe, but is a number when used in a new tab (by clicking on the open Canvas in new tab button), it always initializes to the current date.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date

When deserializing, the value is a string instead of a number, and so will be the string representation of the timestamp. The Date object then tries to parse this string as a date string, fails, and falls back to today's date.

fix is straightforward:

 (value: string | number): number => new Date(+value).getTime() || new Date().getTime(),

To Reproduce
Steps to reproduce the behavior:

  1. add a date knob
  2. set a value
  3. open the canvas in a new tab

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Code snippets
If applicable, add code samples to help explain your problem.

System
Please paste the results of npx sb@next info here.

Additional context
Add any other context about the problem here.

@shilman
Copy link
Member

shilman commented Apr 14, 2021

FYI, we’ve released addon-controls in Storybook 6.0. Controls are portable, auto-generated knobs that are intended to replace addon-knobs, which are slated for deprecation.

Please upgrade and try out controls today!

@cellog
Copy link
Author

cellog commented Apr 14, 2021

thanks, we are aware of controls. We also have a gigantic codebase, so the migration has not happened yet.

@shilman shilman transferred this issue from storybookjs/storybook May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants