bigskysoftware / bigskysoftware/idiomorph

preserve input value if no attr change

Open
#27 6 comments 4 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
1.1k
Forks
57
Avg merge
9h 58m
Merged PRs (30d)
4

Description

Since it is possible(-ish) to work around this feel free to close this issue, but imho the current behaviour around input values is wrong/confusing.

Consider the following structure:

```html

```

If I type something into this input and then do

```js
Idiomorph.morph(document.form, document.form.outerHTML)
```

I'd expect the inputs value to be preserved since nothing changed and an inputs `value` is only loosely coupled to the attribute of the same name, similar to the play state of a video.

According to [this part](https://github.com/bigskysoftware/idiomorph/blob/61327049948378b4f7ba0d4c29419420986d8a79/src/idiomorph.js#L362) the value of an input is reset if
- the new content (`from`) does not have a `value` attribute
- the `beforeAttributeUpdate` callback does not return false

Imho it would be better to simplify this to a mismatch in the value attribute like this:

```js
if(from.getAttribute("value") !== to.getAttribute("value") && !beforeAttributeUpdate(attr, to, 'remove')) {
to.value = from.getAttribute("value") ?? ""
}
```

meaning that the behaviour can be depicted as the following table
| value attribute of `to` (existing) | value attribute of `from` (new content) | behaviour |
| --- | --- | --- |
| null (not present) | null (not present) | no desire to change -> preserve state |
| some value | the same value | no desire to change -> preserve state |
| some value | null (not present) | desire to reset -> set to '' |
| null (not present) | some value | desire to change -> set to value of `from` |
| some value | some other value | desire to change -> set to value of `from` |

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.