-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Hi Edy,
this is also more a question than a bug (bug is lack of docs - but maybe I can help here...)
Could you explain a pattern with djhtmx how it is possible to preserve the content/value of an input field when it is htmx-swapped?
{% load tz i18n htmx %}
<div {% hx-tag %}>
<form>
<input
name="q"
type="text"
autofocus
required
{% on "keyup changed delay:300ms" "validate" %}
{# value="{{ q | default:'' }}" #}
>
<button {% on 'search' %}>search</button>
</div>
</form>
</div>
<div id="search-results" hx-swap-oob="true">
foo
</div>And the component
class SearchInput(Component):
template_name = search_input.html"
def __init__(self, **kwargs):
super().__init__(**kwargs)
# self.q = ""
[...]
def validate(self, **kwargs):
search_text = kwargs["q"]
# [...
# self.q = search_textEach time validate() is called, the input field is reloaded and the input is cleared. So with every swap, a new Component seems to be created, and __init__() is called. Which seems to be a bit of wasting ressources? But anyway, this makes it clear that a self.q = "" in the init method clears the content each time. But if I remove this line, the search input value gets preserved - but the cursor is put at the beginning, so it's not really possible to write anything in the input field.
You say that you use morphdom and I saw code to preserve focus. HTMX has support for even keeping the selection in an inpu field AFAIK. Do I do something wrong here - or how would you create a component with an on change event triggering a validation or similar without crashing the value of the input field?
Thanks in advance