Skip to content

Keep field values the user changed while a morph request was out - #4222

Open
SahinurDEV wants to merge 1 commit into
bigskysoftware:four-devfrom
SahinurDEV:fix/morph-keep-values-changed-during-request
Open

SahinurDEV wants to merge 1 commit into
bigskysoftware:four-devfrom
SahinurDEV:fix/morph-keep-values-changed-during-request

Conversation

@SahinurDEV

Copy link
Copy Markdown

Description

Morph keeps the value of the focused field while a response lands (#3985). If the user types while the request is out and then moves focus (Tab, or a click into the next field) before the response arrives, the response's value is written into .value and the newer text is lost. With hx-trigger="input changed delay:…" that text is never sent either: changed sees the reverted value and drops the debounced request.

This takes the rule suggested in the issue. A field holds the user's value when it is focused (as before) or when its current value isn't one the request sent for its name. In that case morph still updates the value attribute but leaves .value alone. The textarea path uses the same check.

  • The request body is kept as ctx.sentValues right after htmx:config:request, before GET/DELETE move it into the query string, and passed to the morph through the swap tasks.
  • Fields the request didn't carry, and swaps that have no request (htmx.swap(), SSE), behave as before.
  • A field the user didn't touch after sending still takes the response's value, so a server that normalizes the input (ab → Ab) still wins.
  • Added sentValues to HtmxRequestCtx in htmx.d.ts, and a paragraph to the morphing guide.

Corresponding issue: #4184

Testing

Added 3 tests to test/tests/unit/morph.js (input value preservation):

  • keeps text typed while the request was out after the input loses focus: the hx-post form from the issue. In htmx:before:swap the input gets abcd and focus moves to the next field. .value stays abcd and the value attribute becomes ab.
  • keeps a textarea edited while the request was out after it loses focus: the same for a textarea sent with hx-get + hx-include, so the values go through the query string.
  • applies the response value to an input the user did not change since the request: a normalized value replaces the sent one, and an input the request didn't carry takes the response value.

The first two fail on four-dev without the change (expected 'ab' to equal 'abcd', expected 'first' to equal 'first draft') and pass with it. npm test (Chromium): 88 files, 1848 passed, 0 failed, 4 skipped, 100% coverage. npm run check:content passes.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

Morph only kept the value of the focused field. A user who typed while
the request was out and then moved focus lost that text to the response.
Treat a field's value as the user's when it differs from the value the
request sent for it.

Fixes bigskysoftware#4184
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