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

Fix the output of the boolean knob #3612

Merged
merged 4 commits into from
May 24, 2018

Conversation

Keraito
Copy link
Contributor

@Keraito Keraito commented May 20, 2018

Issue: While doing some work on fixing an issue with the button knob discussed on Slack, I found out that the boolean knob's behaviour was also kind of broken: after the initial toggle the value of the knob would always return undefined. This was due to the incoming change object at https://github.com/storybooks/storybook/blob/master/addons/knobs/src/registerKnobs.js#L18 always being of the form: {name: "name-of-knob", type: "boolean"}, without a value. Easiest reproducible by adding a story with a boolean knob and just printing out its direct value.

What I did

Change the behaviour when the incoming change object is of type boolean, to flip the current value.

@Keraito Keraito added bug addon: knobs patch:yes Bugfix & documentation PR that need to be picked to main branch labels May 20, 2018
@Hypnosphi Hypnosphi self-assigned this May 21, 2018

// Update the related knob and it's value.
const knobOptions = knobStore.get(name);

knobOptions.value = value;
if (type === 'boolean') {
knobOptions.value = !knobOptions.value;
Copy link
Member

@wuweiweiwu wuweiweiwu May 23, 2018

Choose a reason for hiding this comment

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

what if the default value is true and initial toggled to false but since value is undefined, this will set knobOptions.value as true which is the wrong value.

I would suggest a undefined check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

initial toggled to false

What do you mean exactly? That the default value is true and the user toggles it to false? That the default is true and the user toggled it to false, and that toggle persisted after refresh? I'm kinda confused so could you elaborate more?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wuweiweiwu in my change, I'm using the knopOptions.value stored in the current knob to calculate the new knobOptions.value. So i'm not using the value destructured from the incoming change object. That value is always undefined, so it's impossible to defer the correct boolean from that value. The knob itself initializes its knobOptions.value with the setted default value, so my change is basically proposing to flip that value every time, instead of relying on the incoming event value. Does this solve your issue? Sorry for the confusion.

Copy link
Member

Choose a reason for hiding this comment

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

That value is always undefined

I would prefer to fix this part

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will see what I can do

Copy link
Member

Choose a reason for hiding this comment

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

@Keraito ah my bad. I thought you were doing !value instead.

@codecov
Copy link

codecov bot commented May 23, 2018

Codecov Report

Merging #3612 into master will decrease coverage by 0.36%.
The diff coverage is 20%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3612      +/-   ##
==========================================
- Coverage   40.05%   39.69%   -0.37%     
==========================================
  Files         481      475       -6     
  Lines        5255     5212      -43     
  Branches      864      862       -2     
==========================================
- Hits         2105     2069      -36     
+ Misses       2632     2626       -6     
+ Partials      518      517       -1
Impacted Files Coverage Δ
addons/knobs/src/components/PropField.js 66.66% <0%> (+16.66%) ⬆️
addons/knobs/src/components/types/Boolean.js 66.66% <33.33%> (+16.66%) ⬆️
...es/react-native-vanilla/storybook/stories/index.js
...tive-vanilla/storybook/stories/Button/index.ios.js
...ct-native-vanilla/storybook/stories/Knobs/index.js
...amples/react-native-vanilla/storybook/storybook.js
...tive-vanilla/storybook/stories/CenterView/index.js
...-native-vanilla/storybook/stories/Welcome/index.js

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8004678...e26bed8. Read the comment docs.

@Keraito
Copy link
Contributor Author

Keraito commented May 23, 2018

I would prefer to fix this part

The previous implementation used a ref in the input element and tried to access the value that way. That didn't work, so I removed the ref and used the event.target.checked value instead. Reverted the previous changes and seemed to work.
Linter warned that the components should be written as a pure function, so I assumed this was the way. If not, please let me know.

@@ -23,23 +23,14 @@ const Label = styled('label')({
fontWeight: 600,
});

export default class PropField extends React.Component {
onChange = e => {
this.props.onChange(e.target.value);
Copy link
Member

Choose a reason for hiding this comment

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

You probably need to change other knob types to pick e.changes.value now

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, this one was actually unused

@Hypnosphi
Copy link
Member

Hypnosphi commented May 23, 2018

@wuweiweiwu please review again and merge if it looks OK

Copy link
Member

@wuweiweiwu wuweiweiwu left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@wuweiweiwu wuweiweiwu merged commit 8f4c781 into storybookjs:master May 24, 2018
@Hypnosphi Hypnosphi removed the patch:yes Bugfix & documentation PR that need to be picked to main branch label May 26, 2018
@Hypnosphi
Copy link
Member

Released as 4.0.0-alpha.8

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.

3 participants