DioxusLabs / DioxusLabs/dioxus

incorrect display in a `div` which has `contenteditable`

Open
#596 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**

I want to use a `div` to capture user input. I want to be able to clear the div when a button is pressed. I try to do this by using a UseState to capture and display the user input. I can use `oninput` to capture the input, but if I display that value inside of the div, extra keystrokes are recorded.

**Steps To Reproduce**

Steps to reproduce the behavior:
- minimal reproducible example
```
use dioxus::prelude::*;
use dioxus_html::KeyCode;

fn main() {
dioxus::desktop::launch(app);
}

fn app(cx: Scope) -> Element {
let text = use_state(&cx, || String::from(""));
cx.render(rsx! (
main {
p {
"some text"
}
div {
contenteditable: "true",
"text_align": "left",
onkeypress: move |evt| {
if evt.key_code == KeyCode::Enter {
println!("text submitted: {}", &text);
// fails to update the UI. enter "abcd" and it outputs "abcdabcaba"
text.set(String::from(""));
}
},
// maybe oninput is only supposed to work on input and textarea elements.
oninput: move |evt| {
// doesn't quite work until the content is deleted
text.set(evt.value.clone());
},
// if this next line is commented out, the input no longer contains extra characters. but there's still not a way to clear the text
"{text}"
}
p {
"more text"
}
}
))
}
```
- type "abc" and press Enter
- the console displays "text submitted: abcdabcaba"

**Expected behavior**

- I expect the console to display "text submitted: abc"

**Screenshots**
after inputting "a"
![input_a](https://user-images.githubusercontent.com/8636602/196525196-52d6cbd1-f630-4bad-802e-1c560cee007a.png)
after inputting "ab"
![input_ab](https://user-images.githubusercontent.com/8636602/196525211-a9e8515c-e69f-4848-a9d6-120b20c7d319.png)
after inputting "abc"
![input_abc](https://user-images.githubusercontent.com/8636602/196525229-8dab5450-54fb-4b61-ae1a-c58fe802a70b.png)

**Environment:**
- Dioxus version: v0.2.4, features = [ "desktop"]
- Rust version:1.64.0
- OS info: pop!_os 22.04
- App platform: desktop

**Questionnaire**

- [ ] I'm interested in fixing this myself but don't know where to start
- [ ] I would like to fix and I have a solution
- [ x ] I don't have time to fix this right now, but maybe later

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.