UNPKG

@hitachivantara/uikit-react-core

Version:
56 lines (55 loc) 2.02 kB
import { HvAdornment } from "../FormElement/Adornment/Adornment.js"; import { HvIcon } from "../icons.js"; import { changeInputValue } from "../Input/utils.js"; import { HvInput } from "../Input/Input.js"; import { useClasses } from "./NumberInput.styles.js"; import { useDefaultProps } from "@hitachivantara/uikit-react-utils"; import { forwardRef, useMemo, useRef } from "react"; import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime"; import { useForkRef } from "@mui/material/utils"; //#region src/NumberInput/NumberInput.tsx /** * A Number Input lets users enter numeric values and provides buttons to increment or decrement the value. * It extends the HvInput component—refer to its documentation for additional available props. * * @extends HvInput */ var HvNumberInput = forwardRef(function HvNumberInput(props, ref) { const { classes: classesProp, className, readOnly, ...others } = useDefaultProps("HvNumberInput", props); const { classes, cx } = useClasses(classesProp); const inputRef = useRef(null); const forkedRef = useForkRef(ref, inputRef); const buttons = useMemo(() => { const handleIncrease = () => { if (inputRef.current) { inputRef.current.stepUp(); changeInputValue(inputRef.current, inputRef.current.value); } }; const handleDecrease = () => { if (inputRef.current) { inputRef.current.stepDown(); changeInputValue(inputRef.current, inputRef.current.value); } }; return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(HvAdornment, { icon: /* @__PURE__ */ jsx(HvIcon, { name: "Remove" }), onClick: handleDecrease }), /* @__PURE__ */ jsx(HvAdornment, { icon: /* @__PURE__ */ jsx(HvIcon, { name: "Add" }), onClick: handleIncrease })] }); }, []); return /* @__PURE__ */ jsx(HvInput, { ref: forkedRef, type: "number", className: cx(classes.root, className), endAdornment: !readOnly && buttons, classes, readOnly, disableClear: true, ...others }); }); //#endregion export { HvNumberInput };