Skip to content

Expand drag-and-drop array reordering to secondary form contexts#412

Open
JoyceJ53 wants to merge 3 commits into
ash-project:mainfrom
JoyceJ53:main
Open

Expand drag-and-drop array reordering to secondary form contexts#412
JoyceJ53 wants to merge 3 commits into
ash-project:mainfrom
JoyceJ53:main

Conversation

@JoyceJ53

Copy link
Copy Markdown
Contributor

Problem:
Primitive array drag-and-drop reordering only worked on create/update attribute forms. Argument arrays in secondary form contexts — data table query forms, generic action forms, and show-page calculation forms — still required deleting and re-typing items to change order. Separately, LiveView unused* tracking keys were leaking into those array params and exploding into fake tag rows on every keystroke, which made the feature hard to use and test.
Solution:

  • Shared cleanup helpers: Extended to_indexed_map/1 in helpers.ex to ignore non-digit keys and skip structures, and added sanitize_form_params/1 plus normalize_argument_params/2 so form params strip LiveView unused* keys, densify indexed array maps, and convert array arguments to real lists before FilterForm.Arguments casting.

  • Data table query forms: Wired update_array_sorting and sort_array_value/3 into data_table.ex, and sanitized query params on validate/save so Sortable.js reorders work on read-action argument arrays without regenerating unused tags.

  • Generic action forms: Mirrored the same sorting handler and sanitize-on-validate/save path in generic_action.ex so primitive array arguments on custom actions can be reordered.

  • Show calculation forms: Implemented a show-page-specific update_array_sorting handler in show.ex that reorders raw calculation param maps (no AshPhoenix.Form.update_form/3), and hooked sanitize/normalize into validate, calculate, append, and remove so calculation argument arrays stay clean and reorder correctly.

  • Shared form rendering: Updated list_value/2 in form.ex to densify via to_indexed_map/1 and avoid charlist-as-many-tags issues, and ran sanitize_form_params/1 on create/update validate/submit for consistency with the secondary contexts.

  • Demo resources: Added :filter_by_tags, :echo_tags, and :join_tags in the demo user/calculator resources (and matching test Post definitions) so each secondary context can be exercised manually and in LiveView tests.

  • Testing: Expanded helpers_test.exs for sanitize/normalize edge cases, and added page_live_test.exs coverage that simulates Sortable hook events on data table, generic action, and show calculation forms.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

Closes #408

JoyceJ53 added 3 commits June 16, 2026 13:31
…sProblem:Users could add or remove items from primitive array fields (such as lists of tags), but they could not change their sequence. Reordering required deleting elements and manually re-typing them, creating a frustrating user experience when index order mattered.Solution:- Integrated `Sortable.js` (vendored to keep dependencies lightweight) via a new Phoenix LiveView hook (`Hooks.Sortable`) to handle the front-end drag-and-drop interactions.- Added visual feedback in `app.css` (`.sortable-ghost` and `.sortable-drag`) for smoother visual transitions.- Updated `render_fallback_attribute/7` in `form.ex` to inject `data-sort-index` metadata and provide a dedicated grab-handle icon so text inputs remain fully editable.- Introduced server-side reindexing utility functions (`to_indexed_map/1` and `reorder_by_indices/2`) in `helpers.ex` to safely normalize inputs and shuffle form parameter values based on the browser's new sequence recipe.- Implemented the core `update_array_sorting` event handler and `sort_array_value/3` function in `form.ex` to persist the new order directly into `form.raw_params`.- Added isolated unit tests in `helpers_test.exs` and an end-to-end integration test in `page_live_test.exs` to ensure correct state transformation and prevent regressions.
@zachdaniel

Copy link
Copy Markdown
Contributor

@JoyceJ53 I see a bunch of merge conflicts here, mind resolving those before I review?

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.

Expand drag-and-drop array reordering to secondary form contexts (Data Tables, Generic Actions, Calculations)

2 participants