Skip to content

Idiomorph explicitly set swap strategy is overridden by inherited htmx swap, and disinheriting does not change the behavior. #133

@ranzlee

Description

@ranzlee

This is a change validation feature where a debounced input change issues a request. The intent is to morph the changed field with a validation error if invalid. I did work around this by changing the var source to be 'my-div' and moving the div inside the form. I think the issue is that htmx.ajax was using the form as the source and the swap strategy was locked-in. Explicitly setting idiomorph to swap outerHTML has no effect, it still applies the form swap strategy. I'm not sure if this is an idiomorph or htmx bug, or combination. As I mentioned, I was able to design my way around it, but wanted to report it just in case. P.S. I love htmx and idiomorph! Quality libraries and much appreciated! Thank you!

Here is the commit of the work-around, if it helps.

versions:
htmx: 2.0.4
idiomorph: 0.7.3

<div id="my-div" hx-ext="morph" >
  <form id="my-form" hx-post="/post-route" hx-swap="beforeend" hx-target="#some-list">
     <!--  content -->
      <script>
          (function() {
              var validate = function(evt) {
                  evt.stopPropagation();
                  if (!evt.srcElement.hasAttribute("data-validate-on-change")) {
                      return;
                  }
                  var dataId = evt.srcElement.getAttribute("data-id");
                  var source = document.getElementById('my-form');
                  htmx.ajax('POST', '/validation-route', {
                      select: `#${dataId}`,
                      target: `#${dataId}`,
                      swap: "morph:{morphStyle:'outerHTML', ignoreActiveValue:true}",
                      source: source
                  }).then(() => {
                      var evt = new Event('validationChange');
                      var ele = document.getElementById(dataId);
                      ele?.dispatchEvent(evt);
                  });
              }
              var debounce = function() {
                  var timer;
                  return function(evt) {
                      clearTimeout(timer);
                      timer = setTimeout(() => {
                          validate(evt);
                      }, 600);
                  }
              }
              document.getElementById('my-div').oninput = debounce(); 
          })()
      </script>
    </form>
  </div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions