add input click away blur

This commit is contained in:
Qing 2022-09-21 21:56:51 +08:00
parent b1cebb614a
commit d8ed4d2a58
3 changed files with 27 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import React, { FormEvent, useState } from 'react' import React, { FormEvent, useRef, useState } from 'react'
import { useClickAway } from 'react-use'
import { useRecoilState } from 'recoil' import { useRecoilState } from 'recoil'
import emitter, { EVENT_PROMPT } from '../../event' import emitter, { EVENT_PROMPT } from '../../event'
import { appState, propmtState } from '../../store/Atoms' import { appState, propmtState } from '../../store/Atoms'
@ -9,6 +10,7 @@ import TextInput from '../shared/Input'
const PromptInput = () => { const PromptInput = () => {
const [app, setAppState] = useRecoilState(appState) const [app, setAppState] = useRecoilState(appState)
const [prompt, setPrompt] = useRecoilState(propmtState) const [prompt, setPrompt] = useRecoilState(propmtState)
const ref = useRef(null)
const handleOnInput = (evt: FormEvent<HTMLInputElement>) => { const handleOnInput = (evt: FormEvent<HTMLInputElement>) => {
evt.preventDefault() evt.preventDefault()
@ -23,9 +25,17 @@ const PromptInput = () => {
} }
} }
useClickAway<MouseEvent>(ref, () => {
if (ref?.current) {
const input = ref.current as HTMLInputElement
input.blur()
}
})
return ( return (
<div className="prompt-wrapper"> <div className="prompt-wrapper">
<TextInput <TextInput
ref={ref}
value={prompt} value={prompt}
onInput={handleOnInput} onInput={handleOnInput}
placeholder="I want to repaint of..." placeholder="I want to repaint of..."

View File

@ -1,4 +1,5 @@
import React from 'react' import React, { useRef } from 'react'
import { useClickAway } from 'react-use'
import NumberInput from '../shared/NumberInput' import NumberInput from '../shared/NumberInput'
import SettingBlock from './SettingBlock' import SettingBlock from './SettingBlock'
@ -27,6 +28,15 @@ function NumberInputSetting(props: NumberInputSettingProps) {
disable, disable,
} = props } = props
const ref = useRef(null)
useClickAway<MouseEvent>(ref, () => {
if (ref?.current) {
const input = ref.current as HTMLInputElement
input.blur()
}
})
return ( return (
<SettingBlock <SettingBlock
className="sub-setting-block" className="sub-setting-block"
@ -47,6 +57,7 @@ function NumberInputSetting(props: NumberInputSettingProps) {
value={value} value={value}
disabled={disable} disabled={disable}
onValue={onValue} onValue={onValue}
ref={ref}
/> />
{suffix && <span>{suffix}</span>} {suffix && <span>{suffix}</span>}
</div> </div>

View File

@ -1,11 +1,12 @@
import React, { FocusEvent, InputHTMLAttributes } from 'react' import React, { FocusEvent, InputHTMLAttributes, RefObject } from 'react'
import { useClickAway } from 'react-use'
import { useRecoilState } from 'recoil' import { useRecoilState } from 'recoil'
import { appState } from '../../store/Atoms' import { appState } from '../../store/Atoms'
const TextInput = React.forwardRef< const TextInput = React.forwardRef<
HTMLInputElement, HTMLInputElement,
InputHTMLAttributes<HTMLInputElement> InputHTMLAttributes<HTMLInputElement>
>((props: InputHTMLAttributes<HTMLInputElement>, forwardedRef) => { >((props, ref) => {
const { onFocus, onBlur, ...itemProps } = props const { onFocus, onBlur, ...itemProps } = props
const [_, setAppState] = useRecoilState(appState) const [_, setAppState] = useRecoilState(appState)
@ -26,7 +27,7 @@ const TextInput = React.forwardRef<
return ( return (
<input <input
{...itemProps} {...itemProps}
ref={forwardedRef} ref={ref}
type="text" type="text"
onFocus={handleOnFocus} onFocus={handleOnFocus}
onBlur={handleOnBlur} onBlur={handleOnBlur}