@react-md/form
Version:
This package is for creating all the different form input types.
43 lines • 1.57 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useChecked = void 0;
var react_1 = require("react");
/**
* A small hook that can be used for controlling the state of a single Checkbox
* component.
*
* @param defaultChecked - Boolean if the input should be checked by default.
* Changing this value will not update the state after initial render.
* @param onChange - An optional change event handler to also call when the
* checked state changes.
* @returns a list containing the checked state, a change event handler, and
* then a manual set checked action.
*/
function useChecked(defaultChecked, onChange) {
var _a = __read((0, react_1.useState)(defaultChecked), 2), checked = _a[0], setChecked = _a[1];
var handleChange = (0, react_1.useCallback)(function (event) {
if (onChange) {
onChange(event);
}
setChecked(event.currentTarget.checked);
}, [onChange]);
return [checked, handleChange, setChecked];
}
exports.useChecked = useChecked;
//# sourceMappingURL=useChecked.js.map