Created
December 16, 2021 21:24
-
-
Save mitchallen/b29e22eddfbcbbbc0b51b9e585b5e117 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// File: UseTextInput.tsx | |
// Author: Mitch Allen | |
import {ChangeEventHandler, useCallback, useState} from "react"; | |
export interface TextInput { | |
id?: string, | |
name?: string, | |
label?: string, | |
helperText?: string, | |
init?: string, | |
} | |
export interface TextInputProps { | |
id?: string, | |
name?: string, | |
label?: string, | |
value: string, | |
onChange: ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> | undefined, | |
helperText?: string, | |
} | |
export function useTextInput(options: TextInput = {}): TextInputProps { | |
let { | |
id = undefined, | |
name = undefined, | |
label = 'Text', | |
helperText = undefined, | |
init = '', | |
} = options; | |
if (name === undefined) { | |
name = id; | |
} | |
const [value, setValue] = useState(init); | |
const onChange = useCallback((event) => { | |
setValue(event.target.value); | |
}, []); | |
return { | |
id, | |
name, | |
label, | |
value, | |
onChange, | |
helperText, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment