Skip to content

Commit

Permalink
Add accessible name to Contact Form dropdown (#34139)
Browse files Browse the repository at this point in the history
  • Loading branch information
monsieur-z authored and matticbot committed Nov 20, 2023
1 parent 4e26b66 commit e92d769
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This is an alpha version! The changes listed here are not final.
- The package now requires PHP >= 7.0.

### Fixed
- Add an accessible name to the Contact Form dropdown rendered in the front-end
- Avoid errors when a saved feedback form does not have the expected WP_Post format.

## [0.23.1] - 2023-11-14
Expand Down
8 changes: 6 additions & 2 deletions src/contact-form/class-contact-form-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,12 @@ public function render_checkbox_multiple_field( $id, $label, $value, $class, $re
* @return string HTML
*/
public function render_select_field( $id, $label, $value, $class, $required, $required_field_text ) {
$field = $this->render_label( 'select', $id, $label, $required, $required_field_text );
$field .= "\t<select name='" . esc_attr( $id ) . "' id='" . esc_attr( $id ) . "' " . $class . ( $required ? "required aria-required='true'" : '' ) . ">\n";
$label_ns = 'contact-form-label';
$label_id_key = "$label_ns-id";
$label_id_val = "$label_ns-$id";

$field = $this->render_label( 'select', $id, $label, $required, $required_field_text, array( 'id' => $label_id_val ) );
$field .= "\t<select name='" . esc_attr( $id ) . "' id='" . esc_attr( $id ) . "' " . $class . ( $required ? "required aria-required='true'" : '' ) . "data-$label_id_key='" . esc_attr( $label_id_val ) . "'>\n";

if ( $this->get_attribute( 'togglelabel' ) ) {
$field .= "\t\t<option value=''>" . $this->get_attribute( 'togglelabel' ) . "</option>\n";
Expand Down
25 changes: 16 additions & 9 deletions src/contact-form/js/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ jQuery( function ( $ ) {
} );

function initializeSelectMenu() {
$( '.contact-form .contact-form-dropdown' )
.selectmenu( {
classes: {
'ui-selectmenu-button': 'contact-form-dropdown__button',
'ui-selectmenu-menu': 'contact-form-dropdown__menu',
},
} )
.attr( 'aria-hidden', true )
.prop( 'tabindex', -1 );
$( '.contact-form .contact-form-dropdown' ).each( function () {
const $select = $( this );
const labelId = $select.data( 'contact-form-label-id' );

$select
.selectmenu( {
classes: {
'ui-selectmenu-button': 'contact-form-dropdown__button',
'ui-selectmenu-menu': 'contact-form-dropdown__menu',
},
} )
.attr( 'aria-hidden', true )
.prop( 'tabindex', -1 )
.selectmenu( 'widget' )
.attr( 'aria-labelledby', labelId );
} );
}
} );

0 comments on commit e92d769

Please sign in to comment.