Skip to content

Commit 78398df

Browse files
GaryJonesclaude
andcommitted
fix: convert input to hidden type for Select2 AJAX compatibility
Select2 with AJAX data requires hidden inputs, not text inputs. Also fix test pollution by dequeuing scripts between tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent e6a1ed4 commit 78398df

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

acm-autocomplete.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@
7272
// Destroy existing Select2 if present.
7373
if ( $input.hasClass( 'select2-hidden-accessible' ) ) {
7474
$input.select2( 'destroy' );
75+
// Remove the Select2 container that gets left behind.
76+
$argumentsContainer.find( '.select2-container' ).remove();
7577
}
7678

77-
// Reset the input.
79+
// Reset the input to a visible text input.
7880
$input.val( '' ).attr( 'type', 'text' ).show();
7981

8082
// If this conditional supports autocomplete, initialize it.
@@ -121,6 +123,10 @@
121123
var $input = $argumentsContainer.find( 'input[name="acm-arguments[]"]' );
122124
var currentValue = $input.val();
123125

126+
// Select2 with AJAX requires a hidden input, not text input.
127+
// Convert the text input to hidden type for Select2.
128+
$input.attr( 'type', 'hidden' );
129+
124130
// Initialize Select2 with AJAX.
125131
$input.select2({
126132
ajax: {

tests/Integration/UI/ConditionalAutocompleteTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ final class ConditionalAutocompleteTest extends TestCase {
3131
*/
3232
public function set_up(): void {
3333
parent::set_up();
34+
35+
// Ensure scripts are dequeued at the start of each test.
36+
wp_dequeue_script( 'acm-conditional-autocomplete' );
37+
wp_dequeue_script( 'selectWoo' );
38+
wp_dequeue_style( 'select2' );
39+
3440
$this->autocomplete = new Conditional_Autocomplete();
3541
}
3642

@@ -228,6 +234,12 @@ public function test_search_tags(): void {
228234
*/
229235
public function tear_down(): void {
230236
unset( $_GET['search'], $_GET['conditional'], $_GET['type'], $_GET['taxonomy'], $_GET['post_type'], $_GET['nonce'] );
237+
238+
// Dequeue scripts to prevent test pollution.
239+
wp_dequeue_script( 'acm-conditional-autocomplete' );
240+
wp_dequeue_script( 'selectWoo' );
241+
wp_dequeue_style( 'select2' );
242+
231243
parent::tear_down();
232244
}
233245
}

0 commit comments

Comments
 (0)