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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbeda5baf3151daeee78f081393fcb7ad320a927c7c928b1a1af4940afe02580
4
- data.tar.gz: 62f16acb9a560d7b7abb946c02d72c6a021a7e790ae1d8abf984a5627e44a0f0
3
+ metadata.gz: 36fd9568e6deb7eb620bc5e2dddb79eb0d5a451a6ca353ab6023856030a2e60c
4
+ data.tar.gz: 1167593c17e58b8f655eaaf51bad116b662ebdddf85de761fb1ccaa5e7b2b929
5
5
  SHA512:
6
- metadata.gz: fa3df3560e87aa90ad79e86664c14cfc1a4d44cd28474e43b3bfbc66032fe676531c444d3d3716421628a3590f7b7c32e6eb3f04d77ca324f4bd223630a1b225
7
- data.tar.gz: 4de2fedf514e70e5f37f5fa49e74bec3389dfed6626eb848180d90b2fa216963db372ab55bad53f0b6cedddaac9b93d3c29ed80045960ac356226311ae121996
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
  ![title image](title_image.png)
@@ -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
- document.addEventListener("turbo:load", countVisit, { once: true })
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(:"@#{turbo_form_resource_name}", resource)
39
+ instance_variable_set(turbo_form_ivar, resource)
35
40
  end
36
41
 
37
- def turbo_form_resource = instance_variable_get(:"@#{turbo_form_resource_name}")
38
- def turbo_form_resource_params = send(:"#{turbo_form_resource_name}_params")
39
- def turbo_form_resource_name = controller_name.singularize
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.
@@ -1,3 +1,3 @@
1
1
  module TurboForm
2
- VERSION = '0.10.0'
2
+ VERSION = '0.12.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Cohen