Skip to content

Comments

Combobox form integration improvements#6

Merged
ukutaht merged 14 commits intomainfrom
combobox-phx-change
Oct 29, 2025
Merged

Combobox form integration improvements#6
ukutaht merged 14 commits intomainfrom
combobox-phx-change

Conversation

@ukutaht
Copy link
Contributor

@ukutaht ukutaht commented Oct 29, 2025

No description provided.

ukutaht and others added 13 commits October 29, 2025 12:48
…arent form phx-change

This commit fixes an issue where combobox search inputs would incorrectly
trigger parent form phx-change events during typing, when only selection
changes should trigger form updates.

## Changes

### JavaScript (assets/js/hooks/combobox.js)
- Add `notifyFormChange()` method to dispatch DOM input events on hidden inputs
- Make `stopPropagation()` conditional: only stop in frontend mode, not async mode
- Call `notifyFormChange()` in `addSelection()` and `removeSelection()` to notify parent forms

### Elixir Component (lib/prima/combobox.ex)
- Add `phx-update="ignore"` to search input to preserve value across LiveView updates
- Prevents search input from being reset when parent form phx-change triggers updates

### LiveView (lib/prima_web/live/fixtures_live.ex)
- Update `form_changed` handler to ignore events from `_search` fields
- Only count actual selection changes (from hidden value inputs)

### Tests (test/wallaby/prima_web/combobox_form_integration_test.exs)
- Add `assert_form_change_count/3` helper for cleaner assertions
- Add test for async combobox form integration behavior
- Add test for search input value persistence after form updates
- Create new async combobox form change fixture

### Documentation Updates
- Replace `prima-search` with `phx-change` in async combobox examples
- Update fixtures to use standard Phoenix LiveView patterns

## Why This Works

1. **Search input filtering**: LiveView handler checks if `_target` ends with `_search`
   and ignores those events (typing doesn't trigger form change)

2. **Selection notification**: When selection changes, `notifyFormChange()` dispatches
   a DOM input event on the hidden value input, which triggers form phx-change

3. **Async mode support**: `stopPropagation()` only applies in frontend mode, allowing
   async comboboxes to still send their search events to the server

4. **Value persistence**: `phx-update="ignore"` on search input prevents LiveView from
   resetting the displayed value during re-renders

## Test Coverage

- ✅ Frontend combobox: search doesn't trigger form change, selection does
- ✅ Async combobox: search doesn't trigger form change, selection does
- ✅ Search input value persists after selection when form updates
- ✅ Multiple selection changes properly increment form change counter

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Merged `combobox_selection_change_fixture` and `combobox_form_change_fixture`
into a single `combobox_change_fixture` that tests both combobox-level and
form-level phx-change events together.

## Changes

- Created unified fixture with one combobox inside a form
- Combobox has `phx-change="selection_changed"` (component-level)
- Form has `phx-change="form_changed"` (form-level)
- Both selection display and form change counter visible in same fixture
- Updated all test references to use new IDs: `#change-combobox`, `#change-options`
- Replaced two routes with single `/fixtures/combobox-change` route

## Benefits

- Simpler test infrastructure (1 fixture instead of 2)
- Tests both behaviors together in realistic scenario
- Demonstrates that both event types work correctly when combined
- Easier to maintain with fewer files

All 7 tests passing with unified fixture.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Corrected the documentation to reflect the actual implementation:
- Async mode is triggered by adding `phx-change` to the search input
- The `getMode()` function checks for `phx-change` attribute on search input
- All examples and attribute documentation now correctly use `phx-change`

The previous documentation incorrectly referenced `prima-search` which was
from an earlier iteration that was never implemented.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ukutaht ukutaht changed the title Combobox phx-change Combobox form integration improvements Oct 29, 2025
Adds documentation explaining that combobox triggers parent form phx-change only on selection changes, not during search input typing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@ukutaht ukutaht merged commit 52f7522 into main Oct 29, 2025
1 check passed
@ukutaht ukutaht deleted the combobox-phx-change branch October 29, 2025 16:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant