mirror of
https://github.com/He4eT/elseifplayer.git
synced 2026-05-04 17:07:22 +00:00
Add autofocus and autoclean for InputBox
This commit is contained in:
parent
76ffe39348
commit
f7b8691a50
1 changed files with 23 additions and 6 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import { h } from 'preact'
|
import { h } from 'preact'
|
||||||
// import { useState, useEffect } from 'preact/hooks'
|
import { useEffect, useRef, useState } from 'preact/hooks'
|
||||||
|
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
const keyCodes = {
|
const keyCodes = {
|
||||||
|
|
@ -32,6 +32,18 @@ const keyNames = {
|
||||||
/* eslint-enable */
|
/* eslint-enable */
|
||||||
|
|
||||||
export default function ({ currentWindow, inputType, sendMessage }) {
|
export default function ({ currentWindow, inputType, sendMessage }) {
|
||||||
|
const [inputText, setInputText] = useState('')
|
||||||
|
const inputEl = useRef(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
inputEl.current && inputEl.current.focus()
|
||||||
|
}, [inputType])
|
||||||
|
|
||||||
|
const send = x => {
|
||||||
|
sendMessage(x, currentWindow)
|
||||||
|
setInputText('')
|
||||||
|
}
|
||||||
|
|
||||||
const charHandler = event => {
|
const charHandler = event => {
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
|
|
||||||
|
|
@ -39,25 +51,30 @@ export default function ({ currentWindow, inputType, sendMessage }) {
|
||||||
keyNames[event.keyCode] ||
|
keyNames[event.keyCode] ||
|
||||||
event.key
|
event.key
|
||||||
|
|
||||||
sendMessage(key, currentWindow)
|
send(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
const lineHandler = ({ keyCode, target: { value } }) => {
|
const lineHandler = ({ keyCode, target: { value } }) => {
|
||||||
if (keyCode === keyCodes.KEY_RETURN) {
|
if (keyCode === keyCodes.KEY_RETURN) {
|
||||||
sendMessage(value, currentWindow)
|
send(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputHandlers = {
|
const inputHandlers = {
|
||||||
'char': {
|
char: {
|
||||||
onKeyDown: charHandler
|
onKeyDown: charHandler
|
||||||
},
|
},
|
||||||
'line': {
|
line: {
|
||||||
onKeyPress: lineHandler
|
onKeyPress: lineHandler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<input {...inputHandlers[inputType]}
|
<input {...inputHandlers[inputType]}
|
||||||
type="text"/>
|
ref={inputEl}
|
||||||
|
value={inputText}
|
||||||
|
autofocus={true}
|
||||||
|
onInput={({ target: {value}}) => setInputText(value)}
|
||||||
|
type='text' />
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue