turbo_form 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/app/assets/javascripts/turbo_form.js +28 -4
- data/lib/turbo_form/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 36fd9568e6deb7eb620bc5e2dddb79eb0d5a451a6ca353ab6023856030a2e60c
|
|
4
|
+
data.tar.gz: 1167593c17e58b8f655eaaf51bad116b662ebdddf85de761fb1ccaa5e7b2b929
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7b9b335f084defd276d85323a16ca2ed2eb9cbff17588a57d9e5d142ca13d2458feed1e3b36f2df9215d1d7efc155f8f140786a751944da74e70baf247d40df0
|
|
7
|
+
data.tar.gz: db6f3ffa2a8558e4bf8761dea2c40c9a3d007be0815b05e820c4b69a19a8669b7f283fd363ab0aadffd9f614d8600de65303fb1977ed22e9067fdb0dcec125f2
|
data/README.md
CHANGED
|
@@ -104,6 +104,11 @@ otherwise; every other event sends at once unless it names a wait.
|
|
|
104
104
|
Text typed into a field while its request is out is kept when the response
|
|
105
105
|
arrives, and a response overtaken by a later one to the same URL isn't rendered.
|
|
106
106
|
|
|
107
|
+
A widget built around a trigger, like a custom select that keeps its focus in an
|
|
108
|
+
input of its own, is left as it is while the response renders, so it stays
|
|
109
|
+
focused and open. That's the smallest element holding both the trigger and the
|
|
110
|
+
focus, never the form itself; the rest of the page is morphed as usual.
|
|
111
|
+
|
|
107
112
|
### `dynamic_action:` on a field
|
|
108
113
|
|
|
109
114
|
```erb
|
|
@@ -24,6 +24,7 @@ class FormRefresh {
|
|
|
24
24
|
const action = trigger?.dataset.turboFormAction
|
|
25
25
|
|
|
26
26
|
this.formElement = formElement
|
|
27
|
+
this.trigger = trigger
|
|
27
28
|
this.location = new URL(action || formElement.dataset.turboFormUrl, location.href)
|
|
28
29
|
this.target = action ? new StreamTarget() : targetFor(formElement)
|
|
29
30
|
}
|
|
@@ -33,7 +34,7 @@ class FormRefresh {
|
|
|
33
34
|
try {
|
|
34
35
|
const response = await Turbo.fetch(this.location, { method: "PATCH", body: new FormData(this.formElement), headers: { ...this.target.headers, "X-CSRF-Token": csrfToken() } })
|
|
35
36
|
|
|
36
|
-
if (!this.superseded) await this.target.render(response)
|
|
37
|
+
if (!this.superseded) await this.target.render(response, this.trigger)
|
|
37
38
|
} finally {
|
|
38
39
|
this.requestFinished()
|
|
39
40
|
}
|
|
@@ -67,10 +68,11 @@ class PageTarget {
|
|
|
67
68
|
elements = []
|
|
68
69
|
headers = { Accept: "text/html" }
|
|
69
70
|
|
|
70
|
-
async render(response) {
|
|
71
|
+
async render(response, trigger) {
|
|
71
72
|
const responseHTML = await response.text()
|
|
72
73
|
|
|
73
|
-
|
|
74
|
+
keepFocusedWidget(trigger)
|
|
75
|
+
document.addEventListener("turbo:load", () => (keptWidget = null, countVisit()), { once: true })
|
|
74
76
|
Turbo.visit(location.href, {
|
|
75
77
|
action: "replace",
|
|
76
78
|
shouldCacheSnapshot: false,
|
|
@@ -90,10 +92,12 @@ class FrameTarget {
|
|
|
90
92
|
return { Accept: "text/html", "Turbo-Frame": this.element.id }
|
|
91
93
|
}
|
|
92
94
|
|
|
93
|
-
async render(response) {
|
|
95
|
+
async render(response, trigger) {
|
|
94
96
|
const frame = new DOMParser().parseFromString(await response.text(), "text/html").getElementById(this.element.id)
|
|
95
97
|
|
|
98
|
+
keepFocusedWidget(trigger)
|
|
96
99
|
Turbo.morphTurboFrameElements(this.element, frame)
|
|
100
|
+
keptWidget = null
|
|
97
101
|
countVisit()
|
|
98
102
|
}
|
|
99
103
|
}
|
|
@@ -218,5 +222,25 @@ function keepFocusedValue(event) {
|
|
|
218
222
|
if (target == document.activeElement && target.form?.querySelector(triggerSelector) && detail.attributeName == "value") event.preventDefault()
|
|
219
223
|
}
|
|
220
224
|
|
|
225
|
+
// A widget built around a trigger, like a custom select, can hold the focus in an
|
|
226
|
+
// element of its own beside the trigger. The server never rendered that element,
|
|
227
|
+
// so the morph would remove it mid-use. While a response renders, the smallest
|
|
228
|
+
// element holding both the trigger and the focus is left as it is, short of the
|
|
229
|
+
// form itself.
|
|
230
|
+
let keptWidget = null
|
|
231
|
+
|
|
232
|
+
function keepFocusedWidget(trigger) {
|
|
233
|
+
const focused = document.activeElement
|
|
234
|
+
let element = trigger?.isConnected && focused != trigger ? trigger.parentElement : null
|
|
235
|
+
|
|
236
|
+
while (element && !element.contains(focused)) element = element.parentElement
|
|
237
|
+
keptWidget = element?.contains(trigger.form) ? null : element
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function keepWidget(event) {
|
|
241
|
+
if (event.target == keptWidget) event.preventDefault()
|
|
242
|
+
}
|
|
243
|
+
|
|
221
244
|
new TriggerObserver({ triggerFired: refresh }, document).start()
|
|
222
245
|
addEventListener("turbo:before-morph-attribute", keepFocusedValue)
|
|
246
|
+
addEventListener("turbo:before-morph-element", keepWidget)
|
data/lib/turbo_form/version.rb
CHANGED