mirror of
https://github.com/zhigang1992/react.git
synced 2026-01-26 22:27:03 +08:00
33 lines
812 B
TypeScript
33 lines
812 B
TypeScript
import React, { Dispatch, MutableRefObject, SetStateAction } from 'react'
|
|
import useCurrentState from '../utils/use-current-state'
|
|
|
|
const useInput = (
|
|
initialValue: string,
|
|
): {
|
|
state: string
|
|
setState: Dispatch<SetStateAction<string>>
|
|
currentRef: MutableRefObject<string>
|
|
reset: () => void
|
|
bindings: {
|
|
value: string
|
|
onChange: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void
|
|
}
|
|
} => {
|
|
const [state, setState, currentRef] = useCurrentState<string>(initialValue)
|
|
|
|
return {
|
|
state,
|
|
setState,
|
|
currentRef,
|
|
reset: () => setState(initialValue),
|
|
bindings: {
|
|
value: state,
|
|
onChange: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
setState(event.target.value)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
export default useInput
|