Repository navigation
fix: keep a control's typed or picked value a String in event data - #103
Merged
Merged
Conversation
Event#parse_data ran JSON.parse on every String payload. The client sends
a TextField's text, a Dropdown's option key and a RadioGroup's value as
plain Strings, right after syncing the same String into the control
(update_control). When the text looked like JSON, handlers got another
value of another type ("123" became 123, "2.50" became 2.5, "null" became
nil, "[1]" became [1]), and change events wrote it over props["value"].
A String payload that equals the control's own synced value or text now
stays a String. The decoded value is kept only when it matches a
non-String option key or radio value the app declared: the renderer
sends Integer key 2 back as "2", and handlers keep getting 2. Payloads
that are not the control's value decode as before. A Form relays its
field's decoded event instead of decoding raw_data again.
Fixes #96
The compiled framework does not load JSON by itself, so Ruflet::Event only decodes String payloads on the VM once something has required "json". Require it so the check exercises the decoding path.
…t a String Review follow-ups for the event value fix: - Only a Dropdown or a RadioGroup reports one of its choices as its value. A TextField holding a Radio or a Dropdown in a prop now reports the typed text like any other TextField. - When choices declare both 2 and "2", the client sends "2" for either, so the handler gets 2, as before this branch. - An option with neither key nor text is read by its wire id, as material_dropdown_m2.dart does, so that payload decodes as before. - SearchBar change and submit, and Dropdown text_change, always carry the typed text: the search bar syncs the capitalized text but sends it as typed, and text typed into a dropdown can equal the picked key. - The props["text"] match is gone; the Dropdown text_change entry covers the only control that synced its text before an event. Tests pin Option keys and text, Integer text-only options, the wire id fallback, the 2/"2" case, nested radios, CodeEditor, Symbol keys, the capitalized SearchBar, and ERB select and radio-group params.
radio_group.dart wraps its content in a RadioGroup<String> that every Radio below it joins, also one inside a Form, a field's suffix or a Checkbox's or Dropdown's label. choice_values skipped those, so their Integer values reached the handler as Strings. Only a nested RadioGroup owns its radios and a nested Dropdown its options. Tests also pin that a Form relays its field's event with itself as control and target.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #96
What changed
Ruflet::Event#parse_dataranJSON.parseon every String payload. The client sends what the user typed or picked as a plain String, right after it syncs the same String into the control withupdate_control(textfield.dart,code_editor.dart,material_dropdown.dart,radio_group.dart). So when the user typed123,2.50,nullor[1], the handler got123,2.5,nilor[1], and the change event wrote that value overprops["value"].Now, in
packages/ruflet_core/lib/ruflet_ui/ruflet/event.rb:props["value"]stays a String.get<String>, so an Integer key2comes back as"2"and the handler still gets2.key, else itstext, else its wire id, and a radio'svalue.radio_group.dartwraps its content in aRadioGroup<String>, and each of those radios joins it. Only the radios of a nested RadioGroup and the options of a nested Dropdown are left out.change/submitand Dropdowntext_changealways carry the typed text.material_search_bar.dartsyncs the text with its capitalization applied but sends it as typed.In
form_control.rb, a Form now relays its field's already-decoded event through the new publicEvent#relayed, instead of decodingraw_dataa second time.The new code uses only String, Hash and Array methods, so it runs the same on the embedded VM.
Changes since the last review:
radio_group(content: form(controls: [radio(value: 1), radio(value: 2)]))gave"2", where main gives2.material_dropdown.dartbuilds itslabelandleading_iconinside the group, so a radio there joins the group too. Only itsoptionsare skipped.name,controlandtarget.27e2ffa8.How verified
The final head is
7fd8d07c, with mainf034e99bmerged in. The PR's diff against main is unchanged. The suites, the H1 goldens and the embedded VM below ran on7fd8d07c. The "Against main's code", seed and mutation results come from7b2f07c1, whoseevent.rb,form_control.rband four test files are identical to the final head's.Regression tests. Every file passes on the head and fails against origin/main's
event.rbandform_control.rb:packages/ruflet_core/test/event_client_value_test.rbpackages/ruflet_server/test/event_client_value_wire_test.rb(real MessagePack frames)packages/ruflet_rails/test/erb_form_typed_text_test.rb{"zip"=>12345, "price"=>19.9, "note"=>"", "tags"=>[1], "qty"=>2, "plan"=>20}tools/embedded_vm_harness/tests/event_client_value_test.rb(CRuby)With the last head's
event.rb(0917444f), the core file has 1 failure, the server file 2 and the harness 1, all on radios inside a Form.The core file passes on seeds 1 to 5. The core and server files and the harness also pass on Ruby 3.4.5.
Mutations of
event.rbandform_control.rb. Each of these fails at least one of the new files:controlortarget; reverting the Form relay;Optionentry,Option'stextor_ifallback, or the owner check;"2"-over-2guard.Two mutations survive, and both change nothing. Not renaming the relayed event: a Form relays
changeaschangeandsubmitassubmit. DroppingtextforDropdownOption:DropdownOptionControlalready copiestextintokey.Existing suites, one process per file, Ruby 4.0.5:
packages/ruflet_coredsl_presented_controls_test.rb, which this PR does not touch)packages/ruflet_serverpackages/ruflet_rails(bundle exec)packages/rufletH1 goldens:
cd packages/ruflet && ruby test/compat/compat.rb --jobs 4checked 314 goldens and exited 0.ruby -Ilib -Itest test/compat_goldens_test.rbgave 322 runs with 0 failures.Embedded VM. I built an arm64
embedded_mrubyfrom the final head7fd8d07c(mruby 449627d,ruby_runtime/vm/build_config/desktop_macos.rb). Its framework contains this PR'schoice_valuesand main's newestpage.rbcode. The harness testsclient_state_readers,icon_deferral,scoped_helper,ruflet_gemandruflet_record_gemalso pass on it.event.rb, 1 check fails (the radio inside a Form). With origin/main'sevent.rb, 11 fail. One example: the VM's JSON turns"007"into7.Behavior changes
This is a bug fix (D6). No app code needs to change.
"123","2.50"," 42","1e3","true","null","[1]","{...}","\"q\"".changeon TextField, CupertinoTextField, AutoComplete and CodeEditor, TextFieldsubmit, SearchBarchangeandsubmit(with or withoutcapitalization), and Dropdowntext_change.nil, Array, Hash or unquoted String, while"007"or"12a"stayed Strings. Code that did arithmetic on them already raised on the first letter and on an emptied field ("")."2"used to arrive as2, and"null"asnil.:"2"now gives"2"instead of2, the same way:smallalready gave"small".e.data,e.value, Formon_change/on_submitdata, andprops["value"]after a change or select. Soform.values, validators and a later render see the String too.ruflet_rails) post typed text and the value picked in<select>and<radio-group>as Strings, the way a browser posts them. They now send{"qty"=>"2", "plan"=>"20"}, where before they sent{"qty"=>2, "plan"=>20}.Ruflet.optionkeys, Integer text-only options, and options that declare both2and"2": the client sends"2"for either, and the handler gets2.material_dropdown_m2.dartreports by its wire id. (Ruflet.dropdown_m2goes on the wire as a Dropdown, whose renderer leaves such an option out.)Docs follow-up (ruflet_site):
app/content/docs/events_and_state.md:34says "JSON strings become Ruby arrays or hashes". It needs this exception added.script/sync_ruflet_apirun will addEvent#relayedtoconfig/runtime_catalog.json.51bea9b1(.github/workflows/ci.yml:93), so merging this PR does not break the docs build.Found, not fixed here: the block forms
Ruflet.dropdown { dropdown_option(key: 1) }andRuflet.radio_group { column { radio(value: 1) } }put no options or radios on the wire, so there is nothing to pick. That is a separate bug.