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

display_if incorrectly hide siblings when matching their display-src values #873

Open
renatonascalves opened this issue Jul 24, 2024 · 0 comments
Labels
bug Something isn't working good first issue Good for newcomers javascript Requires understanding JavaScript scope:display-if ux Designing and implementing user experience improvements

Comments

@renatonascalves
Copy link

renatonascalves commented Jul 24, 2024

Description of the bug

When trying to trigger the show and/or hide fields, display_if matches the partial field name of a sibling field instead of performing an exact match.

If you happen to have a display_if logic that share a field name, fields with a partial match ends up getting hidden.

The source of the issue seems to be the use of .match here, since it is not an exact name match but a partial one (it matches a string based of the field name instead of matching the string with the field name):

if ( name && name.match( $( this ).data( 'display-src' ) ) != null ) {

Steps To Reproduce

Here is an example you can use to replicate this issue:

function add_custom_meta_boxes() {
	$custom_meta_group = new Fieldmanager_Group(
		array(
			'name'           => 'custom_meta_group', // <--- display_if will match 'meta' in this field name too, even though it should not.
			'serialize_data' => false,
			'children'       => array(
				'meta'   => new Fieldmanager_Checkbox(					[
						'save_empty'    => false,
						'checked_value' => 'meta',
						'label'         => 'Checkbox',
					]
				),
				'text_field' => new Fieldmanager_Textfield(
					array(
						'label'      => 'Text Field',
						'display_if'  => [
							'src'   => 'meta',
							'value' => 'meta',
						],
					)
				),
				'textarea'   => new Fieldmanager_TextArea(
					array(
						'label' => 'Text Area',
						'display_if'  => [
							'src'   => 'meta',
							'value' => 'meta',
						],
						'attributes' => array(
							'rows' => 5,
							'cols' => 50,
						),
					)
				),
				'subject_prefix'        => new Fieldmanager_Select(
					[
						'name'          => 'subject_prefix',
						'label'         => 'Subject prefix',
						'options'       => [
							'none'   => 'None',
							'custom' => 'Custom',
						],
						'default_value' => 'none',
						'display_if'  => [
							'src'   => 'meta',
							'value' => 'meta',
						],
					]
				),
				'subject_prefix_custom' => new Fieldmanager_Textfield(
					[
						'label'       => 'Custom subject prefix',
						'display_if'  => [
							'src'   => 'subject_prefix',
							'value' => 'custom',
						],
					]
				),
			),
		)
	);

	$custom_meta_group->add_meta_box( 'Custom Meta', 'your-post-type', 'normal', 'high' );
}
add_action( 'admin_init', 'add_custom_meta_boxes' );
CleanShot.2024-07-23.at.23.51.29.mp4

Additional Information

I'm working on the assumption that .match should match the field name provided in display-src, but it is possible there is feature that relies on a loose match.

So this change might require a bit more testing to avoid regressions.

@renatonascalves renatonascalves added bug Something isn't working scope:display-if labels Jul 24, 2024
@kevinfodness kevinfodness added javascript Requires understanding JavaScript good first issue Good for newcomers ux Designing and implementing user experience improvements labels Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers javascript Requires understanding JavaScript scope:display-if ux Designing and implementing user experience improvements
Projects
None yet
Development

No branches or pull requests

2 participants