UNPKG

@smart-react-components/ui

Version:
50 lines (49 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("react"); function useInputMethods({ isDisabled, isReadOnly, onBlur, onChange, onFocus, setValue }) { const [isFocused, setFocused] = (0, react_1.useState)(false); /** * Calls onBlur methhod. * Sets focus status if event is not prevented. */ const handleOnBlur = (e) => { onBlur === null || onBlur === void 0 ? void 0 : onBlur(e); if (!e.defaultPrevented) { setFocused(false); } }; /** * Calls onChange method. * Sets value if event is not prevented. */ const handleOnChange = (e) => { if (isDisabled || isReadOnly) { return; } onChange === null || onChange === void 0 ? void 0 : onChange(e); if (!e.defaultPrevented) { setValue === null || setValue === void 0 ? void 0 : setValue(e.target.value); } }; /** * Calls onFocus method. * Sets focus status if event is not prevented. */ const handleOnFocus = (e) => { if (isDisabled) { return; } onFocus === null || onFocus === void 0 ? void 0 : onFocus(e); if (!e.defaultPrevented) { setFocused(true); } }; return { handleOnBlur, handleOnChange, handleOnFocus, isFocused, }; } exports.default = useInputMethods;