Repository navigation
Target the right element when a set_focus/set_value id is built from a Var - #7904
FarhanAliRaza wants to merge 10 commits into
Conversation
…a Var
rx.set_focus, rx.blur_focus and rx.set_value named a ref by formatting the
id at compile time, with the Var marker of an f-string dropped. An id such
as f"row-{item}" inside rx.foreach therefore named one ref for every row
("ref_row_item_rx_state_"), which the last rendered row had registered:
each row's button focused or filled the last row's input.
An id built from Vars is only known when the event runs, so it is now sent
as it evaluates, and the frontend looks the element up by that id when no
ref is registered under the name. A literal id still names its registered
ref, as before. The three events also accept a plain Var id.
Fixes #7838
Claude-Session: https://claude.ai/code/session_01LwJUCTCgM7D4Vj6H4dRWss
Confidence Score: 4/5
[Medium impact] The change affects three browser events across Reflex apps that use them. Rename Findings
SummaryThe PR makes focus, blur, and value events find the right element when its id comes from a
No new findings. The previous helper-naming requirement remains outstanding. Reviews (7) · Last reviewed commit: "Treat a ref event id that evaluates to n..." · Reviewed by Greptile |
There was a problem hiding this comment.
1 issue found across 7 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/reflex-frontend/src/utils/state.js">
<violation number="1" location="packages/reflex-frontend/src/utils/state.js:97">
P2: `resolveRef` treats inherited `Object.prototype` names as registered refs, so dynamic ids such as `"toString"` never reach `getElementById` and their focus/value operations target the inherited function instead of the row. Check only own registry keys with `Object.hasOwn(refs, ref)`.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| * itself when it is not a string. | ||
| */ | ||
| const resolveRef = (ref) => { | ||
| if (ref in refs) { |
There was a problem hiding this comment.
P2: resolveRef treats inherited Object.prototype names as registered refs, so dynamic ids such as "toString" never reach getElementById and their focus/value operations target the inherited function instead of the row. Check only own registry keys with Object.hasOwn(refs, ref).
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/reflex-frontend/src/utils/state.js, line 97:
<comment>`resolveRef` treats inherited `Object.prototype` names as registered refs, so dynamic ids such as `"toString"` never reach `getElementById` and their focus/value operations target the inherited function instead of the row. Check only own registry keys with `Object.hasOwn(refs, ref)`.</comment>
<file context>
@@ -85,6 +85,23 @@ export const useRegisteredRef = (name) => {
+ * itself when it is not a string.
+ */
+const resolveRef = (ref) => {
+ if (ref in refs) {
+ return refs[ref];
+ }
</file context>
| if (ref in refs) { | |
| if (Object.hasOwn(refs, ref)) { |
There was a problem hiding this comment.
Fixed in 96c0897 by a different route than Object.hasOwn. An id built from a Var is now sent as id and looked up with document.getElementById only, so it never reaches the refs lookup; state.test.js covers an element whose id is toString.
The ref in refs check that remains is the expression from main. It only sees names produced by format_ref, which always start with ref_, so an inherited name cannot match it, and I left it as it was rather than widen the diff.
Generated by Claude Code
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
rx.set_focus, rx.blur_focus and rx.set_value sent an id built from a Var in the same `ref` field as the name a literal id registers its ref under, and the frontend tried the registry first. A row id that evaluated to "ref_alpha" therefore acted on the input with the literal id "alpha", and one spelled like a name every object inherits, such as "toString", found that instead of the element. The id is now sent as `id` and looked up in the document only; `ref` names a registered ref only, resolved as it was before. The frontend test imports `refs` rather than reading the global. Claude-Session: https://claude.ai/code/session_01LwJUCTCgM7D4Vj6H4dRWss
An id built from a Var that evaluates to undefined, such as a missing key, left resolveRef without a ref, and _set_value read .current from undefined. It now does nothing, as _set_focus and _blur_focus already log and return. The missing-element message no longer calls an id a ref, and the test restores its console spy in finally.
… a shared id targets the first element rx.blur_focus is not exported from reflex; the event is rx.event.blur_focus.
Resolve tests/units/reflex_base/event/test_init.py by keeping both the event chain tests from main and this branch's Var id tests.
|
|
||
|
|
||
| def set_focus(ref: str) -> EventSpec: | ||
| def _ref_args(ref: str | Var[str]) -> dict[str, Var]: |
There was a problem hiding this comment.
The new helper _ref_args abbreviates its purpose. The repository requires descriptive function names instead of abbreviations. Rename it and its callers to _element_event_arguments or another full name that explains what it builds. This naming requirement must be satisfied before merging.
Rule Used: Use descriptive function names instead of abbreviations. Prefer generic names that describe functionality rather than implementation-specific names when the function might be replaced with different providers. (source)
Learned From
reflex-dev/flexgen#2190
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
masenf
left a comment
There was a problem hiding this comment.
this is a cool change, and i like how it keeps compatibility with the old mechanism.
ultimately i think we should port over the ElementAPI from reflex-enterprise, roughly:
@dataclass
class ElementAPI(PassthroughAPI):
"""Wrapper for DOM element API methods."""
element_id: str
@property
def _api(self) -> JSAPIVar:
"""Get the JavaScript API object."""
return JSAPIVar(f"document.getElementById('{self.element_id}')")But this is heavily dependent on insecure call_script, which we can't rely on going forward. So we need some way to provide an API like this, but injecting the element into the eval environment if there's even a safe way to do so.
All Submissions:
Type of change
Changes To Core Features:
Problem
Clicking "focus" or "fill" on the first row acted on the last row's input.
rx.set_focus,rx.event.blur_focusandrx.set_valueformat the id into a ref name at compile time and drop the Var marker of the f-string, so every row sentref: "ref_rf_item_rx_state_", a name the last rendered row had registered. Reproduced onmain.Fix
An id built from Vars is only known when the event runs, so it cannot name a ref chosen at compile time.
reflex_base/event/__init__.py): for an id that is aVaror an f-string holding one, the three events send the id as it evaluates, in its own payload field (id: ("rf_"+item_rx_state_)). A literal id is unchanged and still sends its registered ref name (ref: "ref_input1").utils/state.js):_set_focus,_blur_focusand_set_valueresolverefthrough therefsregistry exactly as onmain, and resolveidwithdocument.getElementByIdonly. The two never cross, so a row id that happens to evaluate toref_alpha, or to a name every object inherits such astoString, cannot be taken for a registered ref. An id that evaluates toundefined(a missing key) finds nothing:_set_valuedoes nothing and the focus events log "No element found". When two elements share an id, the event acts on the first; the docs say so.This is a different route from the one #7838 sketches (registering each row's ref under a key derived from its evaluated id). That needs a hook whose argument reads the loop variable, which is only safe where the hook is guaranteed to compile inside the loop; it is not in an
@rx.memobody (#7836) and not for a component the per-item pass leaves unwrapped. Looking the element up by id needs no hook and works in every one of those places.rx.scroll_toalready resolves its target withdocument.getElementById.As a side effect the three events now accept a plain
Varid (rx.set_focus(State.target_id)), which raised aTypeErrorbefore.Not changed
Formcollects for such controls, so I left it alone.rx.set_focus(f"rf_{self.selected}")passes a plain string, which is formatted toref_rf_alphaas before and still finds nothing for an input whose id came from a Var. Supporting that needs the raw id in the payload for literal ids too, which changes the payload of every existing use, so I kept it out of this fix. Happy to follow up if you want it.Tests
tests/units/reflex_base/event/test_init.py: the exact event the three functions compile to for an f-string id and for a plainVarid.packages/reflex-frontend/tests/state.test.js:reffinds a registered ref by name,idfinds an element by its id (including ids spelledref_namedandtoString), a missing target still logs the "No element found" error, and an id that evaluates toundefinedno longer throws in_set_value. Fails without thestate.jschange.tests/integration/tests_playwright/test_server_side_event.py: the existing app gets anrx.foreachwhose rows fill and focus their own input. Without the fix the first row's input stays empty.Ran locally:
tests/units(minusreflex_bench, whose browser test cannot start Chromium in my sandbox),npm run checkinpackages/reflex-frontend(lint, type-check, vitest), the Playwright modulestest_server_side_eventandtest_input, plusruff check,ruff formatandpyrighton the changed files.closes #7838
https://claude.ai/code/session_01LwJUCTCgM7D4Vj6H4dRWss