Implement support for beforeinput event#5581
Merged
Merged
Conversation
Adds onbeforeinput exposing inputType, data, isComposing and the pre-change value. Cancellable via event.prevent_default(). The JS serializer gates the new fields on event.type === "beforeinput" so payloads for oninput and the other callers of serializeInputEvent are unchanged. SerializedBeforeInputData::input_type is required (no serde default) to serve as the discriminator in the untagged EventData enum. Drive-by: serializeInputEvent now falls back to target.textContent for HTMLElement targets when no form-control branch matches, mirroring WebBeforeInputData::value / WebFormData::value. This closes a gap where event.value() returned "" on desktop/liveview but textContent on dioxus-web for contenteditable elements.
df9d953 to
4e36fb6
Compare
ealmloff
requested changes
Jun 2, 2026
ealmloff
left a comment
Member
There was a problem hiding this comment.
Thanks for picking this up! Overall looks good, but there is a bit of duplication around the input data and we could represent the types better for the input_type method
Member
|
It would also be great to get this added to the |
Member
|
min dep failure in ci is unrelated. see #5609 |
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.
Adds an
onbeforeinputevent handler that fires before an editable element is about to be modified. Unlikeoninput, the event is cancellable: handlers can callevent.prevent_default()to block the user agent from applying the change. Exposesinput_type,data,is_composing, and the pre-changevalue.What changed
BeforeInputData/HasBeforeInputData/SerializedBeforeInputDataindioxus-html.convert_before_input_dataplumbing indioxus-desktop,dioxus-liveview,dioxus-native-dom(unimplemented stub), anddioxus-web(WebBeforeInputDatareadsvaluefrom<input>,<textarea>,<select>, ortextContent).input_type,is_composing,dataonly whenevent.type === "beforeinput", so existingoninputpayloads are unchanged.<input>, IME composition (isComposing: true, non-ASCIIdata), andcontenteditable<div>.event_bubbles("beforeinput") => trueto match the DOM spec.Design decisions
input_typeis required (not#[serde(default)]).EventDatais an#[serde(untagged)]enum, so its derivedDeserializefalls through variant-by-variant.Reason: every existing variant has all-default fields and no
deny_unknown_fields. DirectEventData::deserializecall matches the first variant by shape and silently drops the rest of the payload. This is existing behaviour. I filed an issue separately (#5582) and happy to move onto it next. Requiringinput_typeties theBeforeInputvariant to a field the browser always emits forInputEvent, so it acts as a discriminator. The happy path throughHtmlEvent::deserializeroutes by event name and never depends on this, but the public derivedimplis now harder to misuse.Drive-by
textContentfallback inserializeInputEvent. Previously, callingevent.value()from any input-shape handler (oninput,onchange, nowonbeforeinput)Previously returned
""on desktop/liveview forcontenteditableelements, even thoughdioxus-webreturnedtextContentviaWebFormData::value/WebBeforeInputData::value. The JS serializer now mirrors the wasm renderer: if the target isn't an<input>/<textarea>/<select>but is anHTMLElement,contents.valuefalls back totarget.textContent ?? "", yielding the same result regardless of renderer. This affectsinput,change,submit,reset,click, andbeforeinputpayloads oncontenteditabletargets. Happy to split this into a follow-up if you prefer.Docs
The
onbeforeinputrustdoc has a runnable example showing how to block digit insertion. Usesevent.data.input_type()/event.data.data().References