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

Pass explicit false value for falsey checkboxes #413

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

davisshaver
Copy link
Member

Checkbox values will be passed to the shortcode for both true and false states.

See #359

@danielbachhuber
Copy link
Contributor

Hmm. This seems like one of those things it would be really helpful to have a test case for.

How should I review? Do we need to accommodate type=radio as well?

@davisshaver
Copy link
Member Author

@danielbachhuber No, I don't think we need to accommodate the radio button.

What we've changed here is how a truthy default works with the checkbox.

Before this fix, assuming prop is a checkbox attribute with a truthy default, if the user unchecked prop then there'd be a loss of information as [shortcode] would be passed back to TinyMCE. Then when the checkbox field was rendered again, the default would again override the false value for the field.

If the default of a radio is some 'value', then we expect the shortcode to look like [shortcode prop="value"] when it's passed back to the editor. This value would be reflected in the input and all would be good. If no default value is provided then the functional default is no selection, which would just mean that the attribute doesn't get passed to the shortcode, but this is fine as the input will render with no selection anyway.

I've added a test based on @niallkennedy's use case.

@davisshaver
Copy link
Member Author

These gifs aren't good quality but they demonstrate how this PR improves the use case.

Before
ugliergif

After
uglygif


// Test with falsey checkbox
_shortcode.get( 'attrs' ).last().set( 'value', 'false' );
expect( _shortcode.formatShortcode() ).toEqual( '[test_shortcode checkbox="false"]' );
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd expect this to be checkbox=""...

Copy link
Member Author

Choose a reason for hiding this comment

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

@danielbachhuber That would evaluate as null, which would trigger the default to override (Which is the behavior we are trying to prevent).

@danielbachhuber
Copy link
Contributor

@davisshaver I think we should chat about this some during the Shortcake chat today. I'm not 100% sure this is the behavior we want.

@davisshaver
Copy link
Member Author

@danielbachhuber Sure, fine with me.

Maybe we could ask @niallkennedy what he expects, too.

@niallkennedy
Copy link

I look for falsey values (0, 'false', etc) for a shortcode override. I ignore empty string values for a boolean attribute.

The checked behavior passes a 'true' string to be interpreted by the shortcode handler. If you support Shortcake you also need to accept a value of 'true' for a boolean shortcode attribute. A string of 'false' represents the opposite state in my mind, and provides consistency between the required shortcode attribute values a shortcode handler should accept to be compatible with Shortcake.

@davisshaver
Copy link
Member Author

Thanks @niallkennedy!

@davisshaver
Copy link
Member Author

@danielbachhuber based on #413 (comment), what do you think about merging? Should we get a second opinion from @mattheu or @goldenapples?

@davisshaver
Copy link
Member Author

@danielbachhuber Assigning this to you for a second opinion – still hesitant about behavior change?

@danielbachhuber danielbachhuber removed this from the v0.5.0 milestone Aug 13, 2015
@danielbachhuber danielbachhuber removed their assignment Aug 13, 2015
@danielbachhuber
Copy link
Contributor

still hesitant about behavior change?

Yes. For now, let's just bump it from the v0.5.0 milestone.

@montchr
Copy link

montchr commented Dec 16, 2016

It would be great to see this merged at some point. And if not, at least an explanation of why this might be unwanted behavior and possible workarounds? Currently I'm just avoiding checkbox fields because of this and using select fields instead.

@goldenapples
Copy link
Contributor

I like this concept, but it seems that we would need some way of enabling this as an opt-in feature on an attribute. I know that a number of shortcodes I've built would break if this were merged as-is, as there are plenty of places where I just check for a boolean attribute to be present in the callback, and attribute="false" would evaluate to true in the callback.

It might be good to follow the pattern we established with the "encoded" parameter, where a flag set on an attribute could be used in a filter on "shortcode_atts_{$shortcode_tag}" like this one is to parse boolean attributes and either unset the attribute or set it to a boolean false rather than a string.

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

Successfully merging this pull request may close these issues.

5 participants