Skip to content

Instantly share code, notes, and snippets.

@mitchallen
Created December 16, 2021 21:24
Show Gist options
  • Save mitchallen/b29e22eddfbcbbbc0b51b9e585b5e117 to your computer and use it in GitHub Desktop.
Save mitchallen/b29e22eddfbcbbbc0b51b9e585b5e117 to your computer and use it in GitHub Desktop.
// 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