exp/textinput, examples/textinput: bug fix: handle input states correctly on Android Chrome

Updates #2898
This commit is contained in:
Hajime Hoshi 2024-02-03 22:16:16 +09:00
parent 0adc1ad681
commit 5c7bfd3ed7
2 changed files with 28 additions and 1 deletions

View File

@ -138,6 +138,21 @@ func (t *TextField) Blur() {
func (t *TextField) Update() {
if !t.focused {
// If the text field still has a session, read the last state and process it just in case.
if t.ch != nil {
select {
case state, ok := <-t.ch:
if ok && state.Committed {
t.text = t.text[:t.selectionStart] + state.Text + t.text[t.selectionEnd:]
t.selectionStart += len(state.Text)
t.selectionEnd = t.selectionStart
t.state = textinput.State{}
}
t.state = state
default:
break
}
}
if t.end != nil {
t.end()
t.ch = nil

View File

@ -91,7 +91,19 @@ func (t *textInput) init() {
}))
t.textareaElement.Call("addEventListener", "input", js.FuncOf(func(this js.Value, args []js.Value) any {
e := args[0]
t.trySend(!e.Get("isComposing").Bool())
if e.Get("isComposing").Bool() {
t.trySend(false)
return nil
}
if e.Get("inputType").String() == "insertLineBreak" {
t.trySend(true)
return nil
}
t.trySend(false)
return nil
}))
t.textareaElement.Call("addEventListener", "change", js.FuncOf(func(this js.Value, args []js.Value) any {
t.trySend(true)
return nil
}))
// TODO: What about other events like wheel?