phlex-reactive 0.9.1 → 0.9.3
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/CHANGELOG.md +39 -0
- data/README.md +103 -1
- data/app/javascript/phlex/reactive/reactive_controller.js +201 -4
- data/app/javascript/phlex/reactive/reactive_controller.min.js +2 -2
- data/app/javascript/phlex/reactive/reactive_controller.min.js.map +3 -3
- data/lib/phlex/reactive/component/dsl.rb +62 -3
- data/lib/phlex/reactive/component/helpers.rb +71 -6
- data/lib/phlex/reactive/component.rb +6 -1
- data/lib/phlex/reactive/js.rb +14 -0
- data/lib/phlex/reactive/version.rb +1 -1
- metadata +1 -1
|
@@ -20,6 +20,14 @@ module Phlex
|
|
|
20
20
|
module DSL
|
|
21
21
|
extend ActiveSupport::Concern
|
|
22
22
|
|
|
23
|
+
# A cross-root mirror target must be a single ID selector (issue #159)
|
|
24
|
+
# — "#" + a CSS identifier, nothing else. Not a class, not an attribute
|
|
25
|
+
# selector, not `*`, not a compound/descendant/list selector: a
|
|
26
|
+
# compromised reducer must not be able to scribble text across the
|
|
27
|
+
# page. The client interpreter enforces the SAME shape (two-sided
|
|
28
|
+
# default-deny, mirroring the attr allowlist posture).
|
|
29
|
+
MIRROR_ID_SELECTOR = /\A#[A-Za-z_][\w-]*\z/
|
|
30
|
+
|
|
23
31
|
class_methods do
|
|
24
32
|
# Declare the ActiveRecord (GlobalID-able) record this component is
|
|
25
33
|
# rebuilt from. The signed token carries its GlobalID; the server
|
|
@@ -165,15 +173,45 @@ module Phlex
|
|
|
165
173
|
#
|
|
166
174
|
# import { setComputeReducer } from "phlex/reactive/compute"
|
|
167
175
|
# setComputeReducer("payment_split", ({ allowance, cash, leasing, total }, { changed }) => ({ … }))
|
|
168
|
-
|
|
169
|
-
|
|
176
|
+
#
|
|
177
|
+
# `mirror:` (issue #159) declares CROSS-ROOT text mirrors — the opt-in
|
|
178
|
+
# escape from root isolation (issue #15) for a derived value shown in a
|
|
179
|
+
# recap OUTSIDE the computing root (another tab pane, a sticky footer):
|
|
180
|
+
#
|
|
181
|
+
# reactive_compute :split,
|
|
182
|
+
# inputs: %i[a b total],
|
|
183
|
+
# outputs: %i[a b],
|
|
184
|
+
# mirror: { sum_a: "#sum_a", sum_total: ["#sum_total", "#footer-total"] }
|
|
185
|
+
#
|
|
186
|
+
# Each key is a compute name — a declared input (its identity mirror
|
|
187
|
+
# also paints cross-root) or a reducer-result key (an extra text-only
|
|
188
|
+
# output). Each value is one or more DOCUMENT-WIDE id selectors the
|
|
189
|
+
# value is painted into via textContent (XSS-safe, change-guarded,
|
|
190
|
+
# never innerHTML, never a field write). Id selectors ONLY — a class/
|
|
191
|
+
# attribute/compound selector raises here (declared, not arbitrary;
|
|
192
|
+
# the client interpreter re-checks the same shape).
|
|
193
|
+
def reactive_compute(name, inputs: nil, outputs: nil, reducer: nil, mirror: nil)
|
|
194
|
+
if inputs.nil? && outputs.nil?
|
|
195
|
+
# The bare form is the GETTER — a mirror: passed here would be
|
|
196
|
+
# silently dropped, so refuse it LOUDLY (declare-time validation,
|
|
197
|
+
# same posture as normalize_compute_mirror's selector check).
|
|
198
|
+
unless mirror.nil?
|
|
199
|
+
raise ArgumentError,
|
|
200
|
+
"#{self}: reactive_compute(#{name.inspect}, mirror: ...) without inputs:/outputs: " \
|
|
201
|
+
"is the getter form — the mirror would be silently dropped. Declare the mirror " \
|
|
202
|
+
"alongside inputs:/outputs:."
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
return reactive_compute_def(name)
|
|
206
|
+
end
|
|
170
207
|
|
|
171
208
|
input_names, input_types = normalize_compute_inputs(inputs)
|
|
172
209
|
Registry.write_entry(
|
|
173
210
|
self, :computes, name.to_sym,
|
|
174
211
|
ComputeDefinition.new(
|
|
175
212
|
name: name.to_sym, inputs: input_names, input_types:,
|
|
176
|
-
outputs: Array(outputs).map(&:to_sym), reducer: (reducer || name).to_s
|
|
213
|
+
outputs: Array(outputs).map(&:to_sym), reducer: (reducer || name).to_s,
|
|
214
|
+
mirror: normalize_compute_mirror(mirror)
|
|
177
215
|
)
|
|
178
216
|
)
|
|
179
217
|
end
|
|
@@ -210,6 +248,27 @@ module Phlex
|
|
|
210
248
|
end
|
|
211
249
|
end
|
|
212
250
|
|
|
251
|
+
# Normalize `mirror:` to { name => [id selectors] } (nil passes
|
|
252
|
+
# through — no mirror declared). Each value may be one selector or a
|
|
253
|
+
# list; every selector is validated LOUDLY at declare time against
|
|
254
|
+
# MIRROR_ID_SELECTOR (id selectors only).
|
|
255
|
+
def normalize_compute_mirror(mirror)
|
|
256
|
+
return nil if mirror.nil?
|
|
257
|
+
|
|
258
|
+
mirror.to_h do |name, selectors|
|
|
259
|
+
list = Array(selectors).map(&:to_s)
|
|
260
|
+
list.each do
|
|
261
|
+
next if it.match?(MIRROR_ID_SELECTOR)
|
|
262
|
+
|
|
263
|
+
raise ArgumentError,
|
|
264
|
+
"#{self}: mirror target #{it.inspect} for #{name.inspect} must be a single " \
|
|
265
|
+
"id selector (\"#some-id\") — cross-root text mirrors are declared, allowlisted " \
|
|
266
|
+
"targets, never arbitrary selectors"
|
|
267
|
+
end
|
|
268
|
+
[name.to_sym, list.freeze]
|
|
269
|
+
end.freeze
|
|
270
|
+
end
|
|
271
|
+
|
|
213
272
|
# Rebuild a component instance from a verified identity payload. Called
|
|
214
273
|
# by the action endpoint after the token signature is verified.
|
|
215
274
|
#
|
|
@@ -399,6 +399,54 @@ module Phlex
|
|
|
399
399
|
span(**mix({ data: { reactive_text: name.to_s } }, attrs)) { initial }
|
|
400
400
|
end
|
|
401
401
|
|
|
402
|
+
# Value-conditional visibility (issue #161) — the x-show / data-show /
|
|
403
|
+
# wire:show equivalent, entirely client-side. Spread onto the element to
|
|
404
|
+
# show/hide; it declares which OWNED field controls it plus ONE literal
|
|
405
|
+
# predicate, and the generic controller toggles the `hidden` attribute
|
|
406
|
+
# from the field's CURRENT value on every input/change — no round trip,
|
|
407
|
+
# no token, no bespoke Stimulus controller:
|
|
408
|
+
#
|
|
409
|
+
# div(**reactive_show(:mode, not: "off")) { "details" }
|
|
410
|
+
# div(**reactive_show(:kind, equals: "premium")) { "premium panel" }
|
|
411
|
+
# div(**reactive_show(:size, in: %w[l xl])) { "surcharge note" }
|
|
412
|
+
# div(**reactive_show(:gift, equals: true)) { "gift message" }
|
|
413
|
+
#
|
|
414
|
+
# Exactly one predicate — equals:, not:, or in: (a list) — and every
|
|
415
|
+
# value is STRINGIFIED for a literal match against the field's value
|
|
416
|
+
# (a checkbox compares its checked state as "true"/"false", so
|
|
417
|
+
# `equals: true` is the checkbox form; a radio group reads the checked
|
|
418
|
+
# radio's value). This is a DECLARED LITERAL MATCH, never an expression
|
|
419
|
+
# — there is no eval surface (default-deny, like the op vocabulary).
|
|
420
|
+
#
|
|
421
|
+
# Scope: presentational only, strictly less powerful than the js ops —
|
|
422
|
+
# it reads an owned field (#15 ownership) and toggles `hidden` on an
|
|
423
|
+
# owned element. The client seeds visibility at connect and reconciles
|
|
424
|
+
# after a morph; render the initial `hidden:` yourself (from the same
|
|
425
|
+
# server state that renders the field) to avoid a first-paint flash.
|
|
426
|
+
# Extra attrs deep-merge over the binding (mix), like reactive_field.
|
|
427
|
+
def reactive_show(field, **options)
|
|
428
|
+
predicates = options.slice(*SHOW_PREDICATE_KEYS)
|
|
429
|
+
attrs = options.except(*SHOW_PREDICATE_KEYS)
|
|
430
|
+
unless predicates.size == 1
|
|
431
|
+
raise ArgumentError,
|
|
432
|
+
"reactive_show(#{field.inspect}) needs exactly one predicate — equals:, not:, or " \
|
|
433
|
+
"in: — got #{predicates.keys.inspect}"
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
key, value = predicates.first
|
|
437
|
+
data = { reactive_show_field: field.to_s }
|
|
438
|
+
if key == :in
|
|
439
|
+
list = Array(value).map(&:to_s)
|
|
440
|
+
raise ArgumentError, "reactive_show(#{field.inspect}) in: needs at least one value" if list.empty?
|
|
441
|
+
|
|
442
|
+
data[:reactive_show_in] = list.to_json
|
|
443
|
+
else
|
|
444
|
+
data[:"reactive_show_#{key}"] = value.to_s
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
mix({ data: }, attrs)
|
|
448
|
+
end
|
|
449
|
+
|
|
402
450
|
# Scoped busy indicator (issue #99). Marks an element so the generic
|
|
403
451
|
# controller toggles `data-reactive-busy` on it ONLY while `action` is in
|
|
404
452
|
# flight — the scoped sibling of the always-on `data-reactive-busy` the
|
|
@@ -422,13 +470,23 @@ module Phlex
|
|
|
422
470
|
definition = self.class.reactive_compute_def(name)
|
|
423
471
|
raise Error, "#{self.class} has no reactive_compute #{name.inspect}" unless definition
|
|
424
472
|
|
|
425
|
-
{
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
reactive_compute_outputs_param: definition.outputs.map(&:to_s).to_json
|
|
430
|
-
}
|
|
473
|
+
data = {
|
|
474
|
+
reactive_compute_reducer_param: definition.reducer,
|
|
475
|
+
reactive_compute_inputs_param: compute_inputs_param(definition),
|
|
476
|
+
reactive_compute_outputs_param: definition.outputs.map(&:to_s).to_json
|
|
431
477
|
}
|
|
478
|
+
# Declared cross-root text mirrors (issue #159) ride as a JSON object of
|
|
479
|
+
# name → [id selectors]; omitted entirely when undeclared so the shipped
|
|
480
|
+
# wire stays byte-identical.
|
|
481
|
+
data[:reactive_compute_mirror_param] = compute_mirror_param(definition) if definition.mirror
|
|
482
|
+
|
|
483
|
+
{ data: }
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
# The mirror param wire (issue #159): { "sum_a" => ["#sum_a"], … } — the
|
|
487
|
+
# values are ALWAYS arrays so the client parses one uniform shape.
|
|
488
|
+
def compute_mirror_param(definition)
|
|
489
|
+
definition.mirror.transform_keys(&:to_s).to_json
|
|
432
490
|
end
|
|
433
491
|
|
|
434
492
|
# The inputs param wire (issue #104). Untyped (array form) → a JSON ARRAY of
|
|
@@ -475,6 +533,13 @@ module Phlex
|
|
|
475
533
|
reactive_record_for_nested.update!(**nested_attributes(association, attrs), **extra)
|
|
476
534
|
end
|
|
477
535
|
|
|
536
|
+
# The declared reactive_show predicates (issue #161): a literal match on
|
|
537
|
+
# the controlling field's current value. Exactly one per binding —
|
|
538
|
+
# enforced loudly at render (a dead binding must not no-op in the
|
|
539
|
+
# browser). `not`/`in` are Ruby keywords, so they arrive via **options
|
|
540
|
+
# rather than named kwargs.
|
|
541
|
+
SHOW_PREDICATE_KEYS = %i[equals not in].freeze
|
|
542
|
+
|
|
478
543
|
# The declared optimistic-hint class ops (issue #98): the cosmetic class
|
|
479
544
|
# vocabulary the client applies instantly and reverts on failure. Enforced
|
|
480
545
|
# at build time in optimistic_hint_json (default-deny — a dead hint fails
|
|
@@ -86,7 +86,12 @@ module Phlex
|
|
|
86
86
|
# { name => type } hash for the typed HASH form (:string reads the field
|
|
87
87
|
# value raw, :number coerces). `inputs` stays the ordered name list either
|
|
88
88
|
# way, so iteration order is preserved and the array-form wire is unchanged.
|
|
89
|
-
|
|
89
|
+
#
|
|
90
|
+
# `mirror` (issue #159) is nil when undeclared, else a { name => [id
|
|
91
|
+
# selectors] } map — DECLARED cross-root text mirrors, validated to id
|
|
92
|
+
# selectors only at declare time (the server half of the two-sided
|
|
93
|
+
# default-deny; the client interpreter re-checks the shape).
|
|
94
|
+
ComputeDefinition = Data.define(:name, :inputs, :outputs, :reducer, :input_types, :mirror)
|
|
90
95
|
|
|
91
96
|
# A declared add/remove-row collection (issue #35): the list contract tied
|
|
92
97
|
# into one unit — the per-row item component, the container DOM id rows
|
data/lib/phlex/reactive/js.rb
CHANGED
|
@@ -157,6 +157,20 @@ module Phlex
|
|
|
157
157
|
append("focus_first", target_args(to, global:))
|
|
158
158
|
end
|
|
159
159
|
|
|
160
|
+
# --- Text content (issue #159) ---
|
|
161
|
+
#
|
|
162
|
+
# text(to, value) — set the target's textContent (stringified; nil clears).
|
|
163
|
+
# XSS-safe by construction: textContent only, NEVER innerHTML — strictly
|
|
164
|
+
# less powerful than set_attr. Pair with `global: true` to paint a value
|
|
165
|
+
# into a node OUTSIDE the component's root (the cross-root text escape,
|
|
166
|
+
# e.g. a read-only recap in another tab pane).
|
|
167
|
+
|
|
168
|
+
def text(to, value, global: false)
|
|
169
|
+
args = { "to" => normalize_target(to), "value" => value.to_s }
|
|
170
|
+
args["global"] = true if global
|
|
171
|
+
append("text", args.freeze)
|
|
172
|
+
end
|
|
173
|
+
|
|
160
174
|
# --- Dispatch a bubbling CustomEvent (issue #96) ---
|
|
161
175
|
#
|
|
162
176
|
# dispatch(name, to: nil, detail: {}) — emit a bubbling CustomEvent so other
|