Skip to content

Commit 846661a

Browse files
committed
Always flag dirty inputs
This fixes an issue where defaultValues were not being morphed correctly.
1 parent 3584fa0 commit 846661a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/morphlex.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export function morphDocument(from: Document, to: Document | string, options?: O
160160
export function morph(from: ChildNode, to: ChildNode | NodeListOf<ChildNode> | string, options: Options = {}): void {
161161
if (typeof to === "string") to = parseFragment(to).childNodes
162162

163-
if (!options.preserveChanges && isParentNode(from)) flagDirtyInputs(from)
163+
if (isParentNode(from)) flagDirtyInputs(from)
164164
new Morph(options, getActiveElement(from, options)).morph(from, to)
165165
}
166166

test/new/inputs.browser.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,44 @@ describe("text input", () => {
3434
expect(a.outerHTML).toBe(`<input type="text" value="b">`)
3535
expect(a.value).toBe("b")
3636
})
37+
38+
test("morphing a modified value across multiple preserveChanges morphs updates defaultValue", () => {
39+
const input = dom(`<input type="text" value="a">`) as HTMLInputElement
40+
const firstTarget = dom(`<input type="text" value="b">`) as HTMLInputElement
41+
const secondTarget = dom(`<input type="text" value="c">`) as HTMLInputElement
42+
43+
input.value = "user one"
44+
morph(input, firstTarget, { preserveChanges: true })
45+
46+
expect(input.value).toBe("user one")
47+
expect(input.defaultValue).toBe("b")
48+
expect(input.getAttribute("value")).toBe("b")
49+
50+
input.value = "user two"
51+
morph(input, secondTarget, { preserveChanges: true })
52+
53+
expect(input.value).toBe("user two")
54+
expect(input.defaultValue).toBe("c")
55+
expect(input.getAttribute("value")).toBe("c")
56+
})
57+
58+
test("morphing sibling inputs keeps modified value but updates the correct defaultValue", () => {
59+
const from = dom(`<div><input type="text" name="n" value="a"><input type="text" name="n" value="a"></div>`) as HTMLElement
60+
const to = dom(`<div><input type="text" name="n" value="b"><input type="text" name="n" value="a"></div>`) as HTMLElement
61+
62+
const first = from.children[0] as HTMLInputElement
63+
const second = from.children[1] as HTMLInputElement
64+
65+
first.value = "user typed"
66+
morph(from, to, { preserveChanges: true })
67+
68+
expect(first.value).toBe("user typed")
69+
expect(first.defaultValue).toBe("b")
70+
expect(first.getAttribute("value")).toBe("b")
71+
expect(second.value).toBe("a")
72+
expect(second.defaultValue).toBe("a")
73+
expect(second.getAttribute("value")).toBe("a")
74+
})
3775
})
3876

3977
describe("checkbox", () => {

0 commit comments

Comments
 (0)