UNPKG

@netdata/netdata-ui

Version:

netdata UI kit

41 lines 1.17 kB
import { useState, useCallback } from "react"; export var useInputValue = function useInputValue(_ref) { var _ref$value = _ref.value, value = _ref$value === void 0 ? "" : _ref$value, onChange = _ref.onChange, maxChars = _ref.maxChars; var _useState = useState(value), inputValue = _useState[0], setValue = _useState[1]; var _useState2 = useState(false), isDirty = _useState2[0], setIsDirty = _useState2[1]; var handleChange = useCallback(function (e) { var newValue = e.target.value; if (maxChars && newValue.length > maxChars) { e.preventDefault(); e.stopPropagation(); return; } setValue(newValue); if (!isDirty) { setIsDirty(true); } if (onChange) { onChange(e); } }, [isDirty, maxChars, onChange]); var maxCharsIndicator = maxChars ? inputValue.length + "/" + maxChars : ""; var resetValue = useCallback(function (v) { if (v === void 0) { v = ""; } setValue(v); setIsDirty(false); }, []); return [inputValue, handleChange, maxCharsIndicator, isDirty, { setIsDirty: setIsDirty, setValue: setValue, resetValue: resetValue }]; };