turbo_form 0.10.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 +22 -0
- data/app/assets/javascripts/turbo_form.js +28 -4
- data/lib/turbo_form/controller.rb +10 -5
- 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
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# turbo_form
|
|
2
2
|
|
|
3
|
+
<div>
|
|
4
|
+
<img src="https://github.com/OutlawAndy/turbo_form/actions/workflows/ci.yml/badge.svg" alt="CI">
|
|
5
|
+
<img src="https://img.shields.io/gem/v/turbo_form?label=rubygems" alt="RubyGems">
|
|
6
|
+
<img src="https://img.shields.io/npm/v/@rolemodel/turbo-form?label=npm" alt="npm">
|
|
7
|
+
</div>
|
|
8
|
+
<br/>
|
|
9
|
+
|
|
3
10
|
Dynamic forms for Rails, by convention.
|
|
4
11
|
|
|
5
12
|

|
|
@@ -97,6 +104,11 @@ otherwise; every other event sends at once unless it names a wait.
|
|
|
97
104
|
Text typed into a field while its request is out is kept when the response
|
|
98
105
|
arrives, and a response overtaken by a later one to the same URL isn't rendered.
|
|
99
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
|
+
|
|
100
112
|
### `dynamic_action:` on a field
|
|
101
113
|
|
|
102
114
|
```erb
|
|
@@ -157,6 +169,16 @@ For a `WidgetsController`:
|
|
|
157
169
|
themselves — `render layout: 'panel'` is fine. The values are assigned just
|
|
158
170
|
before it renders; an action that redirects still redirects.
|
|
159
171
|
|
|
172
|
+
When `new` and `edit` build the form's object under another name — a bulk form
|
|
173
|
+
object in `@form`, say, where `@widget` already means one of the records it
|
|
174
|
+
wraps — name it, and `widget_params` still permits it:
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
class WidgetsController < ApplicationController
|
|
178
|
+
self.turbo_form_resource_name = :form
|
|
179
|
+
end
|
|
180
|
+
```
|
|
181
|
+
|
|
160
182
|
### Switching an STI subclass
|
|
161
183
|
|
|
162
184
|
When the submitted values include the inheritance column, the object is switched
|
|
@@ -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)
|
|
@@ -5,12 +5,17 @@ module TurboForm
|
|
|
5
5
|
# implicitly, or with its own `render layout: 'panel'`.
|
|
6
6
|
#
|
|
7
7
|
# Leans on the scaffold's conventions: the action builds `@<resource>` and
|
|
8
|
-
# `<resource>_params` permits the form.
|
|
8
|
+
# `<resource>_params` permits the form. A controller whose form object goes by
|
|
9
|
+
# another name sets `self.turbo_form_resource_name = :form`.
|
|
9
10
|
module Controller
|
|
10
11
|
extend ActiveSupport::Concern
|
|
11
12
|
|
|
12
13
|
included do
|
|
13
14
|
helper_method :turbo_form_render?
|
|
15
|
+
|
|
16
|
+
# The instance variable `new` and `edit` build the form's object in, when
|
|
17
|
+
# it isn't the controller's namesake.
|
|
18
|
+
class_attribute :turbo_form_resource_name, instance_accessor: false
|
|
14
19
|
end
|
|
15
20
|
|
|
16
21
|
# Public, as Rails' own is. ActionController::Base's public methods are never
|
|
@@ -31,12 +36,12 @@ module TurboForm
|
|
|
31
36
|
|
|
32
37
|
def dynamic_form_assignment
|
|
33
38
|
resource = turbo_form_resource.extend(TurboForm::Resource).dynamic_assign(turbo_form_resource_params)
|
|
34
|
-
instance_variable_set(
|
|
39
|
+
instance_variable_set(turbo_form_ivar, resource)
|
|
35
40
|
end
|
|
36
41
|
|
|
37
|
-
def turbo_form_resource = instance_variable_get(
|
|
38
|
-
def turbo_form_resource_params = send(:"#{
|
|
39
|
-
def
|
|
42
|
+
def turbo_form_resource = instance_variable_get(turbo_form_ivar)
|
|
43
|
+
def turbo_form_resource_params = send(:"#{controller_name.singularize}_params")
|
|
44
|
+
def turbo_form_ivar = :"@#{self.class.turbo_form_resource_name || controller_name.singularize}"
|
|
40
45
|
|
|
41
46
|
# Active Record saves `has_many` writers and `*_ids=` on assignment, and
|
|
42
47
|
# this request exists only to render.
|
data/lib/turbo_form/version.rb
CHANGED