Skip to content

ToggleGroupControlOption does not display correctly if value is empty string #69977

@coreyworrell

Description

@coreyworrell

Description

Often times we will use an empty string as default value for post meta, and previously the ToggleGroupControlOption worked correctly, after 6.8 update though it fails to show a selected option with value="", and renders no background color (so makes the control invisible as text is same color as background).

Step-by-step reproduction instructions

register_post_meta(
    '',
    'title-display',
    [
        'type' => 'string',
        'description' => 'Determines whether to show the page title or not',
        'single' => true,
        'default' => '',
        'sanitize_callback' => fn (mixed $value): string => $value === 'hide' ? $value : '',
        'auth_callback' => fn (): bool => current_user_can('edit_posts'),
        'show_in_rest' => true,
    ],
);

function show_title(?int $id = null): bool
{
    return get_post_meta($id ?: get_the_ID(), 'title-display', true) === '';
}
<ToggleGroupControl
  label="Title display"
  value={value}
  onChange={setValue}
  __nextHasNoMarginBottom
  __next40pxDefaultSize
>
  <ToggleGroupControlOption
    value=""
    label="Show"
  />
  <ToggleGroupControlOption
    value="hide"
    label="Hide"
  />
</ToggleGroupControl>

Screenshots, screen recording, code snippet

"Show" selected
Image

"Hide" selected
Image

Environment info

  • WordPress 6.8

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes

Please confirm which theme type you used for testing.

  • Block
  • Classic
  • Hybrid (e.g. classic with theme.json)
  • Not sure

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Package] Components/packages/components[Type] BugAn existing feature does not function as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions