Files
react/components/input/use-input.ts
2020-05-10 15:56:05 +09:00

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