@barguide/react-hooks
Version:
TypeScript | React Hooks
30 lines (24 loc) • 730 B
text/typescript
import { useState } from 'react';
type FormCheckboxOnChange = (
event: React.ChangeEvent<HTMLInputElement>
) => void;
interface FormCheckbox {
onChange: FormCheckboxOnChange;
setValue: (val: boolean) => void;
value: boolean;
}
/**
* @name useFormCheckbox
* @description A convenience hook for working with text inputs of type checkbox
*/
const useFormCheckbox = (initialValue = false): FormCheckbox => {
// Hooks
const [value, setValue] = useState(initialValue);
// Handlers
const onChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setValue(event.target.checked);
};
return { onChange, setValue, value };
};
export { useFormCheckbox };
export type { FormCheckboxOnChange, FormCheckbox };