Repository navigation
Show each field's label position in the form builder #3724
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -456,6 +456,7 @@ private static function add_builder_placeholder_to_manifest( $field, array $disp | |||||
| */ | ||||||
| private static function get_classes_for_builder_field( $field, $display, $field_info ) { | ||||||
| $li_classes = $field_info->form_builder_classes( $display['type'] ); | ||||||
| $li_classes .= self::get_builder_label_position_classes( $field, $display ); | ||||||
| $li_classes .= ' frm_form_field frmstart '; | ||||||
|
|
||||||
| $style_align_class = self::get_builder_field_style_align_class( $field, $field_info ); | ||||||
|
|
@@ -477,6 +478,36 @@ private static function get_classes_for_builder_field( $field, $display, $field_ | |||||
| return $li_classes; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Show the field's label position in its form builder preview on load, so the layout | ||||||
| * does not shift when the builder scripts run. | ||||||
| * | ||||||
| * The classes sit before frmstart so changing the field's CSS classes in the builder | ||||||
| * leaves them alone. | ||||||
| * | ||||||
| * @since x.x | ||||||
| * | ||||||
| * @param array $field The field settings, as prepared by FrmFieldsHelper::setup_edit_vars(). | ||||||
| * @param array $display The field display settings. | ||||||
| * | ||||||
| * @return string | ||||||
| */ | ||||||
| private static function get_builder_label_position_classes( $field, $display ) { | ||||||
| if ( ! $field || ! is_array( $field ) || empty( $display['label_position'] ) || in_array( $field['type'], array( 'divider', 'end_divider' ), true ) ) { | ||||||
| return ''; | ||||||
| } | ||||||
|
|
||||||
| $position = FrmFieldsHelper::get_builder_label_position( $field, $field['label'] ); | ||||||
| $classes = ' frm-label-position-' . sanitize_html_class( $position ); | ||||||
|
|
||||||
| // A field with a value shows its inside label above the value, like on the form. | ||||||
| if ( 'inside' === $position && ! empty( $field['default_value'] ) ) { | ||||||
| $classes .= ' frm-label-float-top'; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fix: test for a non-empty string instead of
Suggested change
The condition on the line above is |
||||||
| } | ||||||
|
|
||||||
| return $classes; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Apply the active style alignment to a radio or checkbox builder preview on load. | ||||||
| * | ||||||
|
|
||||||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4591,6 +4591,9 @@ window.frmAdminBuildJS = function() { | |
| updateSliderFieldPreview( changes, att, newValue ); | ||
| } else if ( att === 'value' || att === 'textContent' ) { | ||
| changes[ att ] = newValue; | ||
| if ( att === 'value' ) { | ||
| maybeFloatInsideLabel( changes.closest( '.frm_field_box' ) ); | ||
|
Comment on lines
+4594
to
+4595
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recalculate the inside-label float after a textarea default changes. The default-value textarea has no Suggested fix
|
||
| } | ||
| } else if ( att === 'rows' && newValue === '' ) { | ||
| changes.removeAttribute( att ); | ||
| } else { | ||
|
|
@@ -10502,6 +10505,57 @@ window.frmAdminBuildJS = function() { | |
| const { target } = event; | ||
| maybeShowSaveAndReloadModal( target ); | ||
| validateRangeSettings( target ); | ||
| maybeChangeLabelPosition( target ); | ||
| } | ||
|
|
||
| /** | ||
| * Shows a field's new Label Position in its builder preview. | ||
| * | ||
| * The preview starts with the position from PHP, so this only runs when the setting changes. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement} target The changed setting. | ||
| * @return {void} | ||
| */ | ||
| function maybeChangeLabelPosition( target ) { | ||
| const match = 'SELECT' === target.tagName && target.name.match( /^field_options\[label_(\d+)\]$/ ); | ||
| if ( ! match ) { | ||
| return; | ||
| } | ||
|
|
||
| const field = document.getElementById( `frm_field_id_${ match[ 1 ] }` ); | ||
| if ( ! field ) { | ||
| return; | ||
| } | ||
|
|
||
| // Default follows the form's style, which PHP saves on the Default option. | ||
| const position = target.value || target.querySelector( 'option[value=""]' )?.dataset.labelPosition || 'top'; | ||
|
|
||
| field.classList.forEach( className => { | ||
| if ( className.startsWith( 'frm-label-position-' ) ) { | ||
| field.classList.remove( className ); | ||
| } | ||
| } ); | ||
| field.classList.add( `frm-label-position-${ position }` ); | ||
| maybeFloatInsideLabel( field ); | ||
| } | ||
|
|
||
| /** | ||
| * Moves an inside label above the field when the field has a value, like on the form. | ||
| * | ||
| * @since x.x | ||
| * | ||
| * @param {HTMLElement|null} field The field's builder list item. | ||
| * @return {void} | ||
| */ | ||
| function maybeFloatInsideLabel( field ) { | ||
| if ( ! field ) { | ||
| return; | ||
| } | ||
|
|
||
| const input = field.querySelector( '.frm_opt_container input:not([type="hidden"]), .frm_opt_container textarea' ); | ||
| field.classList.toggle( 'frm-label-float-top', field.classList.contains( 'frm-label-position-inside' ) && !! input?.value ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * Builder - Label Positions | ||
| * | ||
| * Lays out each field preview the way its Label Position setting shows it on the form. | ||
| * PHP prints the frm-label-position-* class, so the layout is in place on first paint, | ||
| * and admin.js swaps it when the setting changes. | ||
| * | ||
| * Left, right, inline and inside labels leave no label row for the action icons to share, | ||
| * so a band the height of that row (21px plus its 6px gap) is kept above the field for them. | ||
| * The card stays the same height as a top label card, and nothing covers the label on hover. | ||
| */ | ||
|
|
||
| #frm-show-fields li.ui-state-default.frm_not_divider:is(.frm-label-position-left, .frm-label-position-right, .frm-label-position-inline, .frm-label-position-inside) > .frm_inner_field_container { | ||
| display: grid; | ||
| grid-auto-rows: min-content; | ||
| padding-top: 27px; | ||
|
|
||
| > .frm-field-action-icons { | ||
| position: absolute; | ||
| top: 0; | ||
| right: 0; | ||
| z-index: 2; | ||
| } | ||
|
|
||
| > .frm_primary_label { | ||
| max-width: none; | ||
| margin: 0; | ||
| } | ||
|
|
||
| > .frm_form_fields { | ||
| margin-top: 0; | ||
| } | ||
| } | ||
|
|
||
| /* Left, right and inline */ | ||
| #frm-show-fields li.ui-state-default.frm_not_divider:is(.frm-label-position-left, .frm-label-position-right, .frm-label-position-inline) > .frm_inner_field_container { | ||
| grid-template-columns: var(--frm-builder-label-width, 150px) minmax(0, 1fr); | ||
| column-gap: 10px; | ||
|
|
||
| > .frm_primary_label { | ||
| grid-row: 1 / span 2; | ||
| grid-column: 1; | ||
| align-self: start; | ||
| padding-top: 4px; | ||
| white-space: normal; | ||
| overflow-wrap: anywhere; | ||
| } | ||
|
|
||
| > .frm_form_fields, | ||
| > .frm_description { | ||
| grid-column: 2; | ||
| } | ||
|
|
||
| > .frm_form_fields { | ||
| grid-row: 1; | ||
| } | ||
| } | ||
|
|
||
| #frm-show-fields li.ui-state-default.frm_not_divider.frm-label-position-inline > .frm_inner_field_container { | ||
| grid-template-columns: repeat(2, minmax(auto, max-content)); | ||
| } | ||
|
|
||
| #frm-show-fields li.ui-state-default.frm_not_divider.frm-label-position-right > .frm_inner_field_container { | ||
| grid-template-columns: minmax(0, 1fr) var(--frm-builder-label-width, 150px); | ||
|
|
||
| > .frm_primary_label { | ||
| grid-column: 2; | ||
| text-align: right; | ||
| } | ||
|
|
||
| > .frm_form_fields, | ||
| > .frm_description { | ||
| grid-column: 1; | ||
| } | ||
| } | ||
|
|
||
| /* Inside: the label sits in the field like a placeholder until the field has a value. */ | ||
| #frm-show-fields li.ui-state-default.frm_not_divider.frm-label-position-inside > .frm_inner_field_container { | ||
| grid-template-columns: minmax(0, 1fr); | ||
| } | ||
|
|
||
| #frm-show-fields li.ui-state-default.frm_not_divider.frm-label-position-inside:not(.frm-label-float-top) > .frm_inner_field_container { | ||
|
|
||
| > .frm_primary_label, | ||
| > .frm_form_fields { | ||
| grid-area: 1 / 1; | ||
| } | ||
|
|
||
| > .frm_primary_label { | ||
| z-index: 1; | ||
| align-self: start; | ||
| padding: 10px 13px; | ||
| color: var(--grey-500); | ||
| pointer-events: none; | ||
| } | ||
|
|
||
| > .frm_form_fields :is(input, textarea)::placeholder { | ||
| opacity: 0; | ||
| } | ||
| } | ||
|
|
||
| #frm-show-fields li.ui-state-default.frm_not_divider.frm-label-position-inside.frm-label-float-top > .frm_inner_field_container > .frm_primary_label { | ||
| font-size: calc(0.85 * var(--text-sm)); | ||
| } | ||
|
|
||
| /* Hidden and None labels stay faintly visible, so the field can still be found in the builder. */ | ||
| #frm-show-fields li.ui-state-default.frm_not_divider:is(.frm-label-position-none, .frm-label-position-hidden) > .frm_inner_field_container > .frm_primary_label { | ||
| opacity: 0.45; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Treat zero as a populated default value.
If an inside-label field has
default_valueequal to"0"or0,empty()returns true. The builder omitsfrm-label-float-top, so the inside-label rules place the label over the value on initial render.Use a presence check that accepts zero and still rejects an empty default.