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

Core: Add 'mapping' to support complex arg values #14100

Merged
merged 8 commits into from
Mar 3, 2021

Conversation

ghengeveld
Copy link
Member

@ghengeveld ghengeveld commented Mar 2, 2021

Issue: #13888
Related: #12613

What I did

This introduces a mapping property for fields in argTypes, to allow the use of complex arg values. It looks like this:

argTypes: {
  label: {
    control: {
      type: 'select',
      options: ['Normal', 'Bold', 'Italic'],
    },
    mapping: {
      Bold: <b>Bold</b>,
      Italic: <i>Italic</i>,
    },
  },
},

This works on any control type, but makes most sense when used with a radio/select type. Control options must be simple strings so they can be serialized and synced to the URL. Control options can still be defined as an object, but the only use case is if you want a custom select label that may not be serializable (e.g. outside the [a-z0-9 _-] range).

How to test

  • Is this testable with Jest or Chromatic screenshots? Beyond mapping of the initial value, not really
  • Does this need a new example in the kitchen sink apps? Updated official-storybook.
  • Does this need an update to the documentation? Yes, done.

If your answer is yes to any of these, please make sure to include it in your PR.

@ghengeveld ghengeveld changed the title Introduce 'mapping' for complex arg values [core] Introduce 'mapping' for complex arg values Mar 2, 2021
@ghengeveld ghengeveld changed the title [core] Introduce 'mapping' for complex arg values Core: Introduce 'mapping' for complex arg values Mar 2, 2021
Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

does this actually fix the bug in #12613? or just in the case where the user provides a mapping?

Copy link
Member

@tmeasday tmeasday left a comment

Choose a reason for hiding this comment

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

Good start!

Definitely need a test in story_store.test.js

docs/writing-stories/args.md Show resolved Hide resolved
lib/client-api/src/story_store.ts Outdated Show resolved Hide resolved
lib/client-api/src/story_store.ts Outdated Show resolved Hide resolved
lib/client-api/src/story_store.ts Outdated Show resolved Hide resolved
@ghengeveld
Copy link
Member Author

does this actually fix the bug in #12613? or just in the case where the user provides a mapping?

No it doesn't. It logs a warning when you try to use an object as value, and suggest to use a mapping, but the actual bug remains (which I think we can leave in, since we're saying it's not supported).

@ghengeveld
Copy link
Member Author

Definitely need a test in story_store.test.js

Done.

Copy link
Member

@shilman shilman left a comment

Choose a reason for hiding this comment

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

Looking great @ghengeveld 💯

@shilman shilman changed the title Core: Introduce 'mapping' for complex arg values Core: Add 'mapping' to support complex arg values Mar 3, 2021
@shilman shilman merged commit be1dc2e into next Mar 3, 2021
@shilman shilman deleted the select-args-serialization branch March 3, 2021 23:39
@ghengeveld
Copy link
Member Author

ghengeveld commented Mar 4, 2021

Note I reopened the original issue #13888 because we changed our minds on the approach. In addition to mapping, we're going to hoist control.options to options, and introduce control.labels.

The mapping feature introduced in this PR is here to stay and will work independently from options.

useEffect(() => {
if (
Object.values(rows).some(({ control: { options = {} } = {} }) =>
Object.values(options).some((v) => !['boolean', 'number', 'string'].includes(typeof v))
Copy link
Member

Choose a reason for hiding this comment

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

@ghengeveld after upgrading to latest beta I'm getting this error: Uncaught TypeError: _ref$control is null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants