numora-react 3.0.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +241 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +9 -1
- package/dist/index.mjs +83 -466
- package/dist/index.mjs.map +1 -1
- package/package.json +15 -6
- package/rollup.config.mjs +2 -3
- package/src/index.tsx +101 -47
- package/tsconfig.json +1 -1
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../node_modules/.pnpm/react@19.2.3/node_modules/react/cjs/react-jsx-runtime.production.js","../../../node_modules/.pnpm/react@19.2.3/node_modules/react/cjs/react-jsx-runtime.development.js","../../../node_modules/.pnpm/react@19.2.3/node_modules/react/jsx-runtime.js","../src/handlers.ts","../src/index.tsx"],"sourcesContent":["/**\n * @license React\n * react-jsx-runtime.production.js\n *\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\n\"use strict\";\nvar REACT_ELEMENT_TYPE = Symbol.for(\"react.transitional.element\"),\n REACT_FRAGMENT_TYPE = Symbol.for(\"react.fragment\");\nfunction jsxProd(type, config, maybeKey) {\n var key = null;\n void 0 !== maybeKey && (key = \"\" + maybeKey);\n void 0 !== config.key && (key = \"\" + config.key);\n if (\"key\" in config) {\n maybeKey = {};\n for (var propName in config)\n \"key\" !== propName && (maybeKey[propName] = config[propName]);\n } else maybeKey = config;\n config = maybeKey.ref;\n return {\n $$typeof: REACT_ELEMENT_TYPE,\n type: type,\n key: key,\n ref: void 0 !== config ? config : null,\n props: maybeKey\n };\n}\nexports.Fragment = REACT_FRAGMENT_TYPE;\nexports.jsx = jsxProd;\nexports.jsxs = jsxProd;\n","/**\n * @license React\n * react-jsx-runtime.development.js\n *\n * Copyright (c) Meta Platforms, Inc. and affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n\n\"use strict\";\n\"production\" !== process.env.NODE_ENV &&\n (function () {\n function getComponentNameFromType(type) {\n if (null == type) return null;\n if (\"function\" === typeof type)\n return type.$$typeof === REACT_CLIENT_REFERENCE\n ? null\n : type.displayName || type.name || null;\n if (\"string\" === typeof type) return type;\n switch (type) {\n case REACT_FRAGMENT_TYPE:\n return \"Fragment\";\n case REACT_PROFILER_TYPE:\n return \"Profiler\";\n case REACT_STRICT_MODE_TYPE:\n return \"StrictMode\";\n case REACT_SUSPENSE_TYPE:\n return \"Suspense\";\n case REACT_SUSPENSE_LIST_TYPE:\n return \"SuspenseList\";\n case REACT_ACTIVITY_TYPE:\n return \"Activity\";\n }\n if (\"object\" === typeof type)\n switch (\n (\"number\" === typeof type.tag &&\n console.error(\n \"Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue.\"\n ),\n type.$$typeof)\n ) {\n case REACT_PORTAL_TYPE:\n return \"Portal\";\n case REACT_CONTEXT_TYPE:\n return type.displayName || \"Context\";\n case REACT_CONSUMER_TYPE:\n return (type._context.displayName || \"Context\") + \".Consumer\";\n case REACT_FORWARD_REF_TYPE:\n var innerType = type.render;\n type = type.displayName;\n type ||\n ((type = innerType.displayName || innerType.name || \"\"),\n (type = \"\" !== type ? \"ForwardRef(\" + type + \")\" : \"ForwardRef\"));\n return type;\n case REACT_MEMO_TYPE:\n return (\n (innerType = type.displayName || null),\n null !== innerType\n ? innerType\n : getComponentNameFromType(type.type) || \"Memo\"\n );\n case REACT_LAZY_TYPE:\n innerType = type._payload;\n type = type._init;\n try {\n return getComponentNameFromType(type(innerType));\n } catch (x) {}\n }\n return null;\n }\n function testStringCoercion(value) {\n return \"\" + value;\n }\n function checkKeyStringCoercion(value) {\n try {\n testStringCoercion(value);\n var JSCompiler_inline_result = !1;\n } catch (e) {\n JSCompiler_inline_result = !0;\n }\n if (JSCompiler_inline_result) {\n JSCompiler_inline_result = console;\n var JSCompiler_temp_const = JSCompiler_inline_result.error;\n var JSCompiler_inline_result$jscomp$0 =\n (\"function\" === typeof Symbol &&\n Symbol.toStringTag &&\n value[Symbol.toStringTag]) ||\n value.constructor.name ||\n \"Object\";\n JSCompiler_temp_const.call(\n JSCompiler_inline_result,\n \"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.\",\n JSCompiler_inline_result$jscomp$0\n );\n return testStringCoercion(value);\n }\n }\n function getTaskName(type) {\n if (type === REACT_FRAGMENT_TYPE) return \"<>\";\n if (\n \"object\" === typeof type &&\n null !== type &&\n type.$$typeof === REACT_LAZY_TYPE\n )\n return \"<...>\";\n try {\n var name = getComponentNameFromType(type);\n return name ? \"<\" + name + \">\" : \"<...>\";\n } catch (x) {\n return \"<...>\";\n }\n }\n function getOwner() {\n var dispatcher = ReactSharedInternals.A;\n return null === dispatcher ? null : dispatcher.getOwner();\n }\n function UnknownOwner() {\n return Error(\"react-stack-top-frame\");\n }\n function hasValidKey(config) {\n if (hasOwnProperty.call(config, \"key\")) {\n var getter = Object.getOwnPropertyDescriptor(config, \"key\").get;\n if (getter && getter.isReactWarning) return !1;\n }\n return void 0 !== config.key;\n }\n function defineKeyPropWarningGetter(props, displayName) {\n function warnAboutAccessingKey() {\n specialPropKeyWarningShown ||\n ((specialPropKeyWarningShown = !0),\n console.error(\n \"%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)\",\n displayName\n ));\n }\n warnAboutAccessingKey.isReactWarning = !0;\n Object.defineProperty(props, \"key\", {\n get: warnAboutAccessingKey,\n configurable: !0\n });\n }\n function elementRefGetterWithDeprecationWarning() {\n var componentName = getComponentNameFromType(this.type);\n didWarnAboutElementRef[componentName] ||\n ((didWarnAboutElementRef[componentName] = !0),\n console.error(\n \"Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.\"\n ));\n componentName = this.props.ref;\n return void 0 !== componentName ? componentName : null;\n }\n function ReactElement(type, key, props, owner, debugStack, debugTask) {\n var refProp = props.ref;\n type = {\n $$typeof: REACT_ELEMENT_TYPE,\n type: type,\n key: key,\n props: props,\n _owner: owner\n };\n null !== (void 0 !== refProp ? refProp : null)\n ? Object.defineProperty(type, \"ref\", {\n enumerable: !1,\n get: elementRefGetterWithDeprecationWarning\n })\n : Object.defineProperty(type, \"ref\", { enumerable: !1, value: null });\n type._store = {};\n Object.defineProperty(type._store, \"validated\", {\n configurable: !1,\n enumerable: !1,\n writable: !0,\n value: 0\n });\n Object.defineProperty(type, \"_debugInfo\", {\n configurable: !1,\n enumerable: !1,\n writable: !0,\n value: null\n });\n Object.defineProperty(type, \"_debugStack\", {\n configurable: !1,\n enumerable: !1,\n writable: !0,\n value: debugStack\n });\n Object.defineProperty(type, \"_debugTask\", {\n configurable: !1,\n enumerable: !1,\n writable: !0,\n value: debugTask\n });\n Object.freeze && (Object.freeze(type.props), Object.freeze(type));\n return type;\n }\n function jsxDEVImpl(\n type,\n config,\n maybeKey,\n isStaticChildren,\n debugStack,\n debugTask\n ) {\n var children = config.children;\n if (void 0 !== children)\n if (isStaticChildren)\n if (isArrayImpl(children)) {\n for (\n isStaticChildren = 0;\n isStaticChildren < children.length;\n isStaticChildren++\n )\n validateChildKeys(children[isStaticChildren]);\n Object.freeze && Object.freeze(children);\n } else\n console.error(\n \"React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.\"\n );\n else validateChildKeys(children);\n if (hasOwnProperty.call(config, \"key\")) {\n children = getComponentNameFromType(type);\n var keys = Object.keys(config).filter(function (k) {\n return \"key\" !== k;\n });\n isStaticChildren =\n 0 < keys.length\n ? \"{key: someKey, \" + keys.join(\": ..., \") + \": ...}\"\n : \"{key: someKey}\";\n didWarnAboutKeySpread[children + isStaticChildren] ||\n ((keys =\n 0 < keys.length ? \"{\" + keys.join(\": ..., \") + \": ...}\" : \"{}\"),\n console.error(\n 'A props object containing a \"key\" prop is being spread into JSX:\\n let props = %s;\\n <%s {...props} />\\nReact keys must be passed directly to JSX without using spread:\\n let props = %s;\\n <%s key={someKey} {...props} />',\n isStaticChildren,\n children,\n keys,\n children\n ),\n (didWarnAboutKeySpread[children + isStaticChildren] = !0));\n }\n children = null;\n void 0 !== maybeKey &&\n (checkKeyStringCoercion(maybeKey), (children = \"\" + maybeKey));\n hasValidKey(config) &&\n (checkKeyStringCoercion(config.key), (children = \"\" + config.key));\n if (\"key\" in config) {\n maybeKey = {};\n for (var propName in config)\n \"key\" !== propName && (maybeKey[propName] = config[propName]);\n } else maybeKey = config;\n children &&\n defineKeyPropWarningGetter(\n maybeKey,\n \"function\" === typeof type\n ? type.displayName || type.name || \"Unknown\"\n : type\n );\n return ReactElement(\n type,\n children,\n maybeKey,\n getOwner(),\n debugStack,\n debugTask\n );\n }\n function validateChildKeys(node) {\n isValidElement(node)\n ? node._store && (node._store.validated = 1)\n : \"object\" === typeof node &&\n null !== node &&\n node.$$typeof === REACT_LAZY_TYPE &&\n (\"fulfilled\" === node._payload.status\n ? isValidElement(node._payload.value) &&\n node._payload.value._store &&\n (node._payload.value._store.validated = 1)\n : node._store && (node._store.validated = 1));\n }\n function isValidElement(object) {\n return (\n \"object\" === typeof object &&\n null !== object &&\n object.$$typeof === REACT_ELEMENT_TYPE\n );\n }\n var React = require(\"react\"),\n REACT_ELEMENT_TYPE = Symbol.for(\"react.transitional.element\"),\n REACT_PORTAL_TYPE = Symbol.for(\"react.portal\"),\n REACT_FRAGMENT_TYPE = Symbol.for(\"react.fragment\"),\n REACT_STRICT_MODE_TYPE = Symbol.for(\"react.strict_mode\"),\n REACT_PROFILER_TYPE = Symbol.for(\"react.profiler\"),\n REACT_CONSUMER_TYPE = Symbol.for(\"react.consumer\"),\n REACT_CONTEXT_TYPE = Symbol.for(\"react.context\"),\n REACT_FORWARD_REF_TYPE = Symbol.for(\"react.forward_ref\"),\n REACT_SUSPENSE_TYPE = Symbol.for(\"react.suspense\"),\n REACT_SUSPENSE_LIST_TYPE = Symbol.for(\"react.suspense_list\"),\n REACT_MEMO_TYPE = Symbol.for(\"react.memo\"),\n REACT_LAZY_TYPE = Symbol.for(\"react.lazy\"),\n REACT_ACTIVITY_TYPE = Symbol.for(\"react.activity\"),\n REACT_CLIENT_REFERENCE = Symbol.for(\"react.client.reference\"),\n ReactSharedInternals =\n React.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,\n hasOwnProperty = Object.prototype.hasOwnProperty,\n isArrayImpl = Array.isArray,\n createTask = console.createTask\n ? console.createTask\n : function () {\n return null;\n };\n React = {\n react_stack_bottom_frame: function (callStackForError) {\n return callStackForError();\n }\n };\n var specialPropKeyWarningShown;\n var didWarnAboutElementRef = {};\n var unknownOwnerDebugStack = React.react_stack_bottom_frame.bind(\n React,\n UnknownOwner\n )();\n var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner));\n var didWarnAboutKeySpread = {};\n exports.Fragment = REACT_FRAGMENT_TYPE;\n exports.jsx = function (type, config, maybeKey) {\n var trackActualOwner =\n 1e4 > ReactSharedInternals.recentlyCreatedOwnerStacks++;\n return jsxDEVImpl(\n type,\n config,\n maybeKey,\n !1,\n trackActualOwner\n ? Error(\"react-stack-top-frame\")\n : unknownOwnerDebugStack,\n trackActualOwner ? createTask(getTaskName(type)) : unknownOwnerDebugTask\n );\n };\n exports.jsxs = function (type, config, maybeKey) {\n var trackActualOwner =\n 1e4 > ReactSharedInternals.recentlyCreatedOwnerStacks++;\n return jsxDEVImpl(\n type,\n config,\n maybeKey,\n !0,\n trackActualOwner\n ? Error(\"react-stack-top-frame\")\n : unknownOwnerDebugStack,\n trackActualOwner ? createTask(getTaskName(type)) : unknownOwnerDebugTask\n );\n };\n })();\n","'use strict';\n\nif (process.env.NODE_ENV === 'production') {\n module.exports = require('./cjs/react-jsx-runtime.production.js');\n} else {\n module.exports = require('./cjs/react-jsx-runtime.development.js');\n}\n","import type React from 'react';\nimport {\n handleOnChangeNumoraInput,\n handleOnKeyDownNumoraInput,\n handleOnPasteNumoraInput,\n formatValueForDisplay,\n type CaretPositionInfo,\n type FormattingOptions,\n FormatOn,\n} from 'numora';\n\ntype ChangeResult = {\n value: string;\n rawValue?: string;\n};\n\ntype PasteResult = ChangeResult;\ntype BlurResult = ChangeResult;\n\ntype BaseOptions = {\n decimalMaxLength: number;\n caretPositionBeforeChange?: CaretPositionInfo;\n formattingOptions: FormattingOptions & { rawValueMode?: boolean };\n};\n\nexport function handleNumoraOnChange(\n e: React.ChangeEvent<HTMLInputElement>,\n options: BaseOptions\n): ChangeResult {\n const { formatted, raw } = handleOnChangeNumoraInput(\n e.nativeEvent as unknown as Event,\n options.decimalMaxLength,\n options.caretPositionBeforeChange,\n options.formattingOptions\n );\n\n return {\n value: formatted,\n rawValue: raw,\n };\n}\n\nexport function handleNumoraOnPaste(\n e: React.ClipboardEvent<HTMLInputElement>,\n options: Omit<BaseOptions, 'caretPositionBeforeChange'>\n): PasteResult {\n const { formatted, raw } = handleOnPasteNumoraInput(\n e.nativeEvent as ClipboardEvent,\n options.decimalMaxLength,\n options.formattingOptions\n );\n\n return {\n value: formatted,\n rawValue: raw,\n };\n}\n\nexport function handleNumoraOnKeyDown(\n e: React.KeyboardEvent<HTMLInputElement>,\n formattingOptions: FormattingOptions\n): CaretPositionInfo | undefined {\n return handleOnKeyDownNumoraInput(\n e.nativeEvent as unknown as KeyboardEvent,\n formattingOptions\n );\n}\n\nexport function handleNumoraOnBlur(\n e: React.FocusEvent<HTMLInputElement>,\n options: {\n decimalMaxLength: number;\n formattingOptions: FormattingOptions & { rawValueMode?: boolean };\n }\n): BlurResult {\n if (options.formattingOptions.formatOn === FormatOn.Blur) {\n const { formatted, raw } = formatValueForDisplay(\n e.target.value,\n options.decimalMaxLength,\n { ...options.formattingOptions, formatOn: FormatOn.Change }\n );\n return {\n value: formatted,\n rawValue: raw,\n };\n }\n\n return {\n value: e.target.value,\n rawValue: undefined,\n };\n}\n","import {\n useRef,\n useState,\n useEffect,\n useLayoutEffect,\n forwardRef,\n useCallback,\n ClipboardEvent,\n ChangeEvent,\n FocusEvent,\n KeyboardEvent,\n InputHTMLAttributes\n} from 'react';\nimport {\n FormatOn,\n ThousandStyle,\n formatValueForDisplay,\n removeThousandSeparators,\n type CaretPositionInfo,\n type FormattingOptions,\n} from 'numora';\nimport {\n handleNumoraOnBlur,\n handleNumoraOnChange,\n handleNumoraOnKeyDown,\n handleNumoraOnPaste,\n} from './handlers';\n\nexport interface NumoraInputProps\n extends Omit<\n InputHTMLAttributes<HTMLInputElement>,\n 'onChange' | 'type' | 'inputMode' | 'onFocus' | 'onBlur'\n > {\n maxDecimals?: number;\n onChange?: (e: ChangeEvent<HTMLInputElement>) => void;\n onFocus?: (e: FocusEvent<HTMLInputElement>) => void;\n onBlur?: (e: FocusEvent<HTMLInputElement>) => void;\n\n formatOn?: FormatOn;\n thousandSeparator?: string;\n thousandStyle?: ThousandStyle;\n decimalSeparator?: string;\n decimalMinLength?: number;\n\n enableCompactNotation?: boolean;\n enableNegative?: boolean;\n enableLeadingZeros?: boolean;\n rawValueMode?: boolean;\n}\n\nconst NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref) => {\n const {\n maxDecimals = 2,\n onChange,\n onPaste,\n onBlur,\n onKeyDown,\n onFocus,\n formatOn = FormatOn.Blur,\n thousandSeparator = ',',\n thousandStyle = ThousandStyle.Thousand,\n decimalSeparator = '.',\n decimalMinLength,\n enableCompactNotation = false,\n enableNegative = false,\n enableLeadingZeros = false,\n rawValueMode = false,\n value: controlledValue,\n defaultValue,\n ...rest\n } = props;\n\n const internalInputRef = useRef<HTMLInputElement>(null);\n const caretInfoRef = useRef<CaretPositionInfo | undefined>(undefined);\n const lastCaretPosRef = useRef<number | null>(null);\n\n const formattingOptions: FormattingOptions = {\n formatOn,\n thousandSeparator,\n ThousandStyle: thousandStyle,\n decimalSeparator,\n decimalMinLength,\n enableCompactNotation,\n enableNegative,\n enableLeadingZeros,\n rawValueMode,\n };\n\n const getInitialValue = (): string => {\n const valueToFormat = controlledValue !== undefined ? controlledValue : defaultValue;\n if (valueToFormat !== undefined) {\n const { formatted } = formatValueForDisplay(String(valueToFormat), maxDecimals, formattingOptions);\n return formatted;\n }\n return '';\n };\n\n const [displayValue, setDisplayValue] = useState<string>(getInitialValue);\n\n // Sync external ref with internal ref\n useLayoutEffect(() => {\n if (!ref) return;\n if (typeof ref === 'function') {\n ref(internalInputRef.current);\n } else {\n ref.current = internalInputRef.current;\n }\n }, [ref]);\n\n // When controlled value changes from outside, update display value\n useEffect(() => {\n if (controlledValue !== undefined) {\n const { formatted, raw } = formatValueForDisplay(String(controlledValue), maxDecimals, formattingOptions);\n if (formatted !== displayValue) {\n setDisplayValue(formatted);\n\n // Fire onChange so the parent state stays in sync when formatting props\n // cause the value to be reformatted (e.g., maxDecimals changes from 4 to 2)\n if (onChange && internalInputRef.current) {\n const input = internalInputRef.current;\n // Set DOM value so e.target.value returns the formatted value\n const nativeSetter = Object.getOwnPropertyDescriptor(\n window.HTMLInputElement.prototype, 'value'\n )?.set;\n nativeSetter?.call(input, formatted);\n (input as any).rawValue = raw;\n const changeEvent = {\n target: input,\n currentTarget: input,\n } as unknown as ChangeEvent<HTMLInputElement>;\n onChange(changeEvent);\n }\n }\n }\n }, [controlledValue, maxDecimals, formattingOptions.ThousandStyle, formattingOptions.decimalSeparator, formattingOptions.decimalMinLength,\n formattingOptions.enableCompactNotation, formattingOptions.enableLeadingZeros, formattingOptions.enableNegative,\n formattingOptions.formatOn, formattingOptions.rawValueMode, formattingOptions.thousandSeparator]);\n\n // Restore cursor position after render\n useLayoutEffect(() => {\n if (internalInputRef.current && lastCaretPosRef.current !== null) {\n const input = internalInputRef.current;\n const pos = lastCaretPosRef.current;\n input.setSelectionRange(pos, pos);\n lastCaretPosRef.current = null;\n }\n });\n\n const handleChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {\n const { value, rawValue } = handleNumoraOnChange(e, {\n decimalMaxLength: maxDecimals,\n caretPositionBeforeChange: caretInfoRef.current,\n formattingOptions,\n });\n\n // Store cursor position AFTER core library has calculated and set it\n // The core library modifies the DOM element directly during handleNumoraOnChange\n // Read from the input ref (which is the same element) to get the position set by the core library\n if (internalInputRef.current) {\n const cursorPos = internalInputRef.current.selectionStart;\n if (cursorPos !== null && cursorPos !== undefined) {\n lastCaretPosRef.current = cursorPos;\n }\n }\n\n caretInfoRef.current = undefined;\n\n (e.target as any).rawValue = rawValue;\n\n setDisplayValue(value);\n\n if (onChange) {\n onChange(e);\n }\n }, [maxDecimals, formattingOptions, onChange]);\n\n const handleKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {\n const coreCaretInfo = handleNumoraOnKeyDown(e, formattingOptions);\n\n // Always capture cursor position info, even if core library doesn't return it\n // This is needed for cursor position calculation during normal typing (not just Delete/Backspace)\n if (!coreCaretInfo && internalInputRef.current) {\n const selectionStart = internalInputRef.current.selectionStart ?? 0;\n const selectionEnd = internalInputRef.current.selectionEnd ?? 0;\n caretInfoRef.current = {\n selectionStart,\n selectionEnd,\n };\n } else {\n caretInfoRef.current = coreCaretInfo;\n }\n\n if (onKeyDown) {\n onKeyDown(e);\n }\n }, [formattingOptions, onKeyDown]);\n\n const handlePaste = useCallback((e: ClipboardEvent<HTMLInputElement>) => {\n const { value, rawValue } = handleNumoraOnPaste(e, {\n decimalMaxLength: maxDecimals,\n formattingOptions,\n });\n\n // For paste, we often want to move cursor to the end of pasted content\n // handleNumoraOnPaste already handles DOM value and cursor, but React will overwrite it.\n // So we capture where the core logic set the cursor.\n lastCaretPosRef.current = (e.target as HTMLInputElement).selectionStart;\n (e.target as any).rawValue = rawValue;\n\n setDisplayValue(value);\n\n if (onPaste) {\n onPaste(e);\n }\n\n // Trigger onChange manually because paste event doesn't always trigger a ChangeEvent in all React versions\n // when we preventDefault.\n if (onChange) {\n const changeEvent = e as unknown as ChangeEvent<HTMLInputElement>;\n onChange(changeEvent);\n }\n }, [maxDecimals, formattingOptions, onPaste, onChange]);\n\n const handleFocus = useCallback((e: FocusEvent<HTMLInputElement>) => {\n if (formattingOptions.formatOn === FormatOn.Blur && formattingOptions.thousandSeparator) {\n const unformattedValue = removeThousandSeparators(displayValue, formattingOptions.thousandSeparator);\n setDisplayValue(unformattedValue);\n }\n\n if (onFocus) {\n onFocus(e);\n }\n }, [formattingOptions, onFocus, displayValue]);\n\n const handleBlur = useCallback((e: FocusEvent<HTMLInputElement>) => {\n const { value, rawValue } = handleNumoraOnBlur(e, {\n decimalMaxLength: maxDecimals,\n formattingOptions,\n });\n\n (e.target as any).rawValue = rawValue;\n setDisplayValue(value);\n\n if (onBlur) {\n onBlur(e);\n }\n }, [maxDecimals, formattingOptions, onBlur]);\n\n return (\n <input\n {...rest}\n ref={internalInputRef}\n value={displayValue}\n onChange={handleChange}\n onKeyDown={handleKeyDown}\n onPaste={handlePaste}\n onFocus={handleFocus}\n onBlur={handleBlur}\n type=\"text\"\n inputMode=\"decimal\"\n spellCheck={false}\n autoComplete=\"off\"\n />\n );\n});\n\nNumoraInput.displayName = 'NumoraInput';\n\nexport { NumoraInput };\nexport { FormatOn, ThousandStyle } from 'numora';\nexport type { FormattingOptions, CaretPositionInfo } from 'numora';\n"],"names":["jsxRuntimeModule","require$$0","require$$1","_jsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAWA,CAAA,IAAI,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC;AACjE,GAAE,mBAAmB,GAAG,MAAM,CAAC,GAAG,CAAC,gBAAgB,CAAC;AACpD,CAAA,SAAS,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE;GACvC,IAAI,GAAG,GAAG,IAAI;GACd,MAAM,KAAK,QAAQ,KAAK,GAAG,GAAG,EAAE,GAAG,QAAQ,CAAC;AAC9C,GAAE,MAAM,KAAK,MAAM,CAAC,GAAG,KAAK,GAAG,GAAG,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC;AAClD,GAAE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E,iBAAiB,CAAC,iBAAiB,CAAC;YACpG,eAAe,CAAC,gBAAgB,CAAC;QACnC;QAEA,IAAI,OAAO,EAAE;YACX,OAAO,CAAC,CAAC,CAAC;QACZ;IACF,CAAC,EAAE,CAAC,iBAAiB,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC;AAE9C,IAAA,MAAM,UAAU,GAAG,WAAW,CAAC,CAAC,CAA+B,KAAI;QACjE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,kBAAkB,CAAC,CAAC,EAAE;AAChD,YAAA,gBAAgB,EAAE,WAAW;YAC7B,iBAAiB;AAClB,SAAA,CAAC;AAED,QAAA,CAAC,CAAC,MAAc,CAAC,QAAQ,GAAG,QAAQ;QACrC,eAAe,CAAC,KAAK,CAAC;QAEtB,IAAI,MAAM,EAAE;YACV,MAAM,CAAC,CAAC,CAAC;QACX;IACF,CAAC,EAAE,CAAC,WAAW,EAAE,iBAAiB,EAAE,MAAM,CAAC,CAAC;IAE5C,QACEC,oCACM,IAAI,EACR,GAAG,EAAE,gBAAgB,EACrB,KAAK,EAAE,YAAY,EACnB,QAAQ,EAAE,YAAY,EACtB,SAAS,EAAE,aAAa,EACxB,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,WAAW,EACpB,MAAM,EAAE,UAAU,EAClB,IAAI,EAAC,MAAM,EACX,SAAS,EAAC,SAAS,EACnB,UAAU,EAAE,KAAK,EACjB,YAAY,EAAC,KAAK,EAAA,CAClB;AAEN,CAAC;AAED,WAAW,CAAC,WAAW,GAAG,aAAa;;;;","x_google_ignoreList":[0,1,2]}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/handlers.ts","../src/index.tsx"],"sourcesContent":["import type React from 'react';\nimport {\n handleOnChangeNumoraInput,\n handleOnKeyDownNumoraInput,\n handleOnPasteNumoraInput,\n formatValueForDisplay,\n type CaretPositionInfo,\n type FormattingOptions,\n FormatOn,\n} from 'numora';\n\ntype ChangeResult = {\n value: string;\n rawValue?: string;\n};\n\ntype PasteResult = ChangeResult;\ntype BlurResult = ChangeResult;\n\ntype BaseOptions = {\n decimalMaxLength: number;\n caretPositionBeforeChange?: CaretPositionInfo;\n formattingOptions: FormattingOptions & { rawValueMode?: boolean };\n};\n\nexport function handleNumoraOnChange(\n e: React.ChangeEvent<HTMLInputElement>,\n options: BaseOptions\n): ChangeResult {\n const { formatted, raw } = handleOnChangeNumoraInput(\n e.nativeEvent as unknown as Event,\n options.decimalMaxLength,\n options.caretPositionBeforeChange,\n options.formattingOptions\n );\n\n return {\n value: formatted,\n rawValue: raw,\n };\n}\n\nexport function handleNumoraOnPaste(\n e: React.ClipboardEvent<HTMLInputElement>,\n options: Omit<BaseOptions, 'caretPositionBeforeChange'>\n): PasteResult {\n const { formatted, raw } = handleOnPasteNumoraInput(\n e.nativeEvent as ClipboardEvent,\n options.decimalMaxLength,\n options.formattingOptions\n );\n\n return {\n value: formatted,\n rawValue: raw,\n };\n}\n\nexport function handleNumoraOnKeyDown(\n e: React.KeyboardEvent<HTMLInputElement>,\n formattingOptions: FormattingOptions\n): CaretPositionInfo | undefined {\n return handleOnKeyDownNumoraInput(\n e.nativeEvent as unknown as KeyboardEvent,\n formattingOptions\n );\n}\n\nexport function handleNumoraOnBlur(\n e: React.FocusEvent<HTMLInputElement>,\n options: {\n decimalMaxLength: number;\n formattingOptions: FormattingOptions & { rawValueMode?: boolean };\n }\n): BlurResult {\n if (options.formattingOptions.formatOn === FormatOn.Blur) {\n const { formatted, raw } = formatValueForDisplay(\n e.target.value,\n options.decimalMaxLength,\n { ...options.formattingOptions, formatOn: FormatOn.Change }\n );\n return {\n value: formatted,\n rawValue: raw,\n };\n }\n\n return {\n value: e.target.value,\n rawValue: undefined,\n };\n}\n","import {\n useRef,\n useState,\n useEffect,\n useLayoutEffect,\n forwardRef,\n useCallback,\n useMemo,\n ClipboardEvent,\n ChangeEvent,\n FocusEvent,\n KeyboardEvent,\n InputHTMLAttributes\n} from 'react';\nimport {\n FormatOn,\n ThousandStyle,\n formatValueForDisplay,\n removeThousandSeparators,\n validateNumoraInputOptions,\n type CaretPositionInfo,\n type FormattingOptions,\n} from 'numora';\nimport {\n handleNumoraOnBlur,\n handleNumoraOnChange,\n handleNumoraOnKeyDown,\n handleNumoraOnPaste,\n} from './handlers';\n\nexport interface NumoraHTMLInputElement extends HTMLInputElement {\n rawValue?: string;\n}\n\nexport type NumoraInputChangeEvent = Omit<ChangeEvent<HTMLInputElement>, 'target'> & {\n target: NumoraHTMLInputElement;\n};\n\n/**\n * Creates a complete synthetic change event from a real HTMLInputElement.\n * Used when a change needs to be signalled without an actual DOM change event\n * (e.g. after paste with preventDefault, or after a controlled-value reformat).\n */\nfunction createSyntheticChangeEvent(input: HTMLInputElement): NumoraInputChangeEvent {\n const nativeEvent = new Event('change', { bubbles: true, cancelable: false });\n return {\n nativeEvent,\n target: input as NumoraHTMLInputElement,\n currentTarget: input,\n type: 'change',\n bubbles: true,\n cancelable: false,\n defaultPrevented: false,\n eventPhase: Event.AT_TARGET,\n isTrusted: false,\n timeStamp: Date.now(),\n isDefaultPrevented: () => false,\n isPropagationStopped: () => false,\n persist: () => {},\n preventDefault: () => {},\n stopPropagation: () => {},\n stopImmediatePropagation: () => {},\n } as unknown as NumoraInputChangeEvent;\n}\n\nexport interface NumoraInputProps\n extends Omit<\n InputHTMLAttributes<HTMLInputElement>,\n 'onChange' | 'type' | 'inputMode' | 'onFocus' | 'onBlur'\n > {\n maxDecimals?: number;\n onChange?: (e: NumoraInputChangeEvent) => void;\n onFocus?: (e: FocusEvent<HTMLInputElement>) => void;\n onBlur?: (e: FocusEvent<HTMLInputElement>) => void;\n /** Called with the raw (unformatted) numeric string on every value change. */\n onRawValueChange?: (rawValue: string | undefined) => void;\n\n formatOn?: FormatOn;\n thousandSeparator?: string;\n thousandStyle?: ThousandStyle;\n decimalSeparator?: string;\n decimalMinLength?: number;\n\n enableCompactNotation?: boolean;\n enableNegative?: boolean;\n enableLeadingZeros?: boolean;\n rawValueMode?: boolean;\n}\n\nconst NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref) => {\n const {\n maxDecimals = 2,\n onChange,\n onPaste,\n onBlur,\n onKeyDown,\n onFocus,\n onRawValueChange,\n formatOn = FormatOn.Blur,\n thousandSeparator = ',',\n thousandStyle = ThousandStyle.Thousand,\n decimalSeparator = '.',\n decimalMinLength,\n enableCompactNotation = false,\n enableNegative = false,\n enableLeadingZeros = false,\n rawValueMode = false,\n value: controlledValue,\n defaultValue,\n ...rest\n } = props;\n\n validateNumoraInputOptions({\n decimalMaxLength: maxDecimals,\n decimalMinLength,\n formatOn,\n thousandSeparator,\n thousandStyle,\n decimalSeparator,\n enableCompactNotation,\n enableNegative,\n enableLeadingZeros,\n rawValueMode,\n });\n\n const internalInputRef = useRef<HTMLInputElement>(null);\n const caretInfoRef = useRef<CaretPositionInfo | undefined>(undefined);\n const lastCaretPosRef = useRef<number | null>(null);\n\n // Memoize to give callbacks a stable reference - avoids recreating all\n // useCallback functions on every render when primitive props haven't changed.\n const formattingOptions: FormattingOptions = useMemo(() => ({\n formatOn,\n thousandSeparator,\n ThousandStyle: thousandStyle,\n decimalSeparator,\n decimalMinLength,\n enableCompactNotation,\n enableNegative,\n enableLeadingZeros,\n rawValueMode,\n }), [formatOn, thousandSeparator, thousandStyle, decimalSeparator, decimalMinLength,\n enableCompactNotation, enableNegative, enableLeadingZeros, rawValueMode]);\n\n const getInitialValue = (): string => {\n const valueToFormat = controlledValue !== undefined ? controlledValue : defaultValue;\n if (valueToFormat !== undefined) {\n const { formatted } = formatValueForDisplay(String(valueToFormat), maxDecimals, formattingOptions);\n return formatted;\n }\n return '';\n };\n\n const [displayValue, setDisplayValue] = useState<string>(getInitialValue);\n\n // Track the current displayValue via a ref so the controlled-value useEffect\n // can compare against it without adding displayValue as a dependency (which\n // would cause the effect to re-run on every keystroke).\n const displayValueRef = useRef<string>(displayValue);\n displayValueRef.current = displayValue;\n\n // Sync external ref with internal ref\n useLayoutEffect(() => {\n if (!ref) return;\n if (typeof ref === 'function') {\n ref(internalInputRef.current);\n } else {\n ref.current = internalInputRef.current;\n }\n }, [ref]);\n\n // When the controlled value or formatting options change, reformat the display.\n // Uses displayValueRef (not displayValue in deps) to avoid re-running on every keystroke.\n // Does NOT call onChange - that would create a circular loop with react-hook-form Controller.\n useEffect(() => {\n if (controlledValue !== undefined) {\n const { formatted, raw } = formatValueForDisplay(String(controlledValue), maxDecimals, formattingOptions);\n if (formatted !== displayValueRef.current) {\n setDisplayValue(formatted);\n\n if (internalInputRef.current) {\n (internalInputRef.current as NumoraHTMLInputElement).rawValue = raw;\n }\n onRawValueChange?.(raw);\n }\n }\n }, [controlledValue, maxDecimals, formattingOptions, onRawValueChange]);\n\n // Restore cursor position after render.\n // No dependency array is intentional: this must run after every render so it catches\n // the re-render triggered by setDisplayValue in handleChange/handlePaste.\n // lastCaretPosRef is a ref (not reactive), so it cannot be a dependency.\n useLayoutEffect(() => {\n if (internalInputRef.current && lastCaretPosRef.current !== null) {\n const input = internalInputRef.current;\n const pos = lastCaretPosRef.current;\n input.setSelectionRange(pos, pos);\n lastCaretPosRef.current = null;\n }\n });\n\n const handleChange = useCallback((e: ChangeEvent<HTMLInputElement>) => {\n const { value, rawValue } = handleNumoraOnChange(e, {\n decimalMaxLength: maxDecimals,\n caretPositionBeforeChange: caretInfoRef.current,\n formattingOptions,\n });\n\n if (internalInputRef.current) {\n const cursorPos = internalInputRef.current.selectionStart;\n if (cursorPos !== null && cursorPos !== undefined) {\n lastCaretPosRef.current = cursorPos;\n }\n }\n\n caretInfoRef.current = undefined;\n\n (e.target as NumoraHTMLInputElement).rawValue = rawValue;\n onRawValueChange?.(rawValue);\n\n setDisplayValue(value);\n\n if (onChange) {\n onChange(e as unknown as NumoraInputChangeEvent);\n }\n }, [maxDecimals, formattingOptions, onChange, onRawValueChange]);\n\n const handleKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {\n const coreCaretInfo = handleNumoraOnKeyDown(e, formattingOptions);\n\n if (!coreCaretInfo && internalInputRef.current) {\n const selectionStart = internalInputRef.current.selectionStart ?? 0;\n const selectionEnd = internalInputRef.current.selectionEnd ?? 0;\n caretInfoRef.current = {\n selectionStart,\n selectionEnd,\n };\n } else {\n caretInfoRef.current = coreCaretInfo;\n }\n\n if (onKeyDown) {\n onKeyDown(e);\n }\n }, [formattingOptions, onKeyDown]);\n\n const handlePaste = useCallback((e: ClipboardEvent<HTMLInputElement>) => {\n const { value, rawValue } = handleNumoraOnPaste(e, {\n decimalMaxLength: maxDecimals,\n formattingOptions,\n });\n\n lastCaretPosRef.current = (e.target as HTMLInputElement).selectionStart;\n (e.target as NumoraHTMLInputElement).rawValue = rawValue;\n onRawValueChange?.(rawValue);\n\n setDisplayValue(value);\n\n if (onPaste) {\n onPaste(e);\n }\n\n // Paste calls e.preventDefault() internally, so React's onChange never fires.\n // We synthesise a proper change event so consumers see a typed ChangeEvent.\n if (onChange) {\n onChange(createSyntheticChangeEvent(e.target as HTMLInputElement));\n }\n }, [maxDecimals, formattingOptions, onPaste, onChange, onRawValueChange]);\n\n const handleFocus = useCallback((e: FocusEvent<HTMLInputElement>) => {\n if (\n formattingOptions.formatOn === FormatOn.Blur &&\n formattingOptions.thousandSeparator &&\n formattingOptions.ThousandStyle !== ThousandStyle.None\n ) {\n // Read directly from the DOM element to avoid a stale displayValue closure\n // and to eliminate displayValue from the deps array (which would recreate\n // this callback on every keystroke).\n const currentValue = (e.target as HTMLInputElement).value;\n setDisplayValue(removeThousandSeparators(currentValue, formattingOptions.thousandSeparator!));\n }\n\n if (onFocus) {\n onFocus(e);\n }\n }, [formattingOptions, onFocus]);\n\n const handleBlur = useCallback((e: FocusEvent<HTMLInputElement>) => {\n const { value, rawValue } = handleNumoraOnBlur(e, {\n decimalMaxLength: maxDecimals,\n formattingOptions,\n });\n\n (e.target as NumoraHTMLInputElement).rawValue = rawValue;\n onRawValueChange?.(rawValue);\n setDisplayValue(value);\n\n if (onBlur) {\n onBlur(e);\n }\n }, [maxDecimals, formattingOptions, onBlur, onRawValueChange]);\n\n return (\n <input\n {...rest}\n ref={internalInputRef}\n value={displayValue}\n onChange={handleChange}\n onKeyDown={handleKeyDown}\n onPaste={handlePaste}\n onFocus={handleFocus}\n onBlur={handleBlur}\n type=\"text\"\n inputMode=\"decimal\"\n spellCheck={false}\n autoComplete=\"off\"\n />\n );\n});\n\nNumoraInput.displayName = 'NumoraInput';\n\nexport { NumoraInput };\nexport { FormatOn, ThousandStyle } from 'numora';\nexport type { FormattingOptions, CaretPositionInfo } from 'numora';\n"],"names":["_jsx"],"mappings":";;;;;AAyBM,SAAU,oBAAoB,CAClC,CAAsC,EACtC,OAAoB,EAAA;IAEpB,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,yBAAyB,CAClD,CAAC,CAAC,WAA+B,EACjC,OAAO,CAAC,gBAAgB,EACxB,OAAO,CAAC,yBAAyB,EACjC,OAAO,CAAC,iBAAiB,CAC1B;IAED,OAAO;AACL,QAAA,KAAK,EAAE,SAAS;AAChB,QAAA,QAAQ,EAAE,GAAG;KACd;AACH;AAEM,SAAU,mBAAmB,CACjC,CAAyC,EACzC,OAAuD,EAAA;IAEvD,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,wBAAwB,CACjD,CAAC,CAAC,WAA6B,EAC/B,OAAO,CAAC,gBAAgB,EACxB,OAAO,CAAC,iBAAiB,CAC1B;IAED,OAAO;AACL,QAAA,KAAK,EAAE,SAAS;AAChB,QAAA,QAAQ,EAAE,GAAG;KACd;AACH;AAEM,SAAU,qBAAqB,CACnC,CAAwC,EACxC,iBAAoC,EAAA;IAEpC,OAAO,0BAA0B,CAC/B,CAAC,CAAC,WAAuC,EACzC,iBAAiB,CAClB;AACH;AAEM,SAAU,kBAAkB,CAChC,CAAqC,EACrC,OAGC,EAAA;IAED,IAAI,OAAO,CAAC,iBAAiB,CAAC,QAAQ,KAAK,QAAQ,CAAC,IAAI,EAAE;AACxD,QAAA,MAAM,E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
|
"name": "numora-react",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"main": "./dist/index.
|
|
6
|
-
"module": "./dist/index.mjs",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
7
6
|
"types": "./dist/index.d.ts",
|
|
8
7
|
"description": "React component wrapper for Numora",
|
|
9
8
|
"homepage": "https://numora.xyz/",
|
|
@@ -11,7 +10,17 @@
|
|
|
11
10
|
".": {
|
|
12
11
|
"types": "./dist/index.d.ts",
|
|
13
12
|
"import": "./dist/index.mjs",
|
|
14
|
-
"require": "./dist/index.
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"react": ">=16.8.0",
|
|
18
|
+
"react-dom": ">=16.8.0",
|
|
19
|
+
"numora": ">=3.0.2"
|
|
20
|
+
},
|
|
21
|
+
"peerDependenciesMeta": {
|
|
22
|
+
"react-dom": {
|
|
23
|
+
"optional": true
|
|
15
24
|
}
|
|
16
25
|
},
|
|
17
26
|
"sideEffects": false,
|
|
@@ -61,8 +70,8 @@
|
|
|
61
70
|
"rollup": "^4.53.5",
|
|
62
71
|
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
63
72
|
"tslib": "^2.8.1",
|
|
64
|
-
"typescript": "^5.
|
|
65
|
-
"numora": "^3.0
|
|
73
|
+
"typescript": "^5.8.2",
|
|
74
|
+
"numora": "^3.1.0"
|
|
66
75
|
},
|
|
67
76
|
"publishConfig": {
|
|
68
77
|
"access": "public"
|
package/rollup.config.mjs
CHANGED
|
@@ -2,20 +2,19 @@ import resolve from '@rollup/plugin-node-resolve';
|
|
|
2
2
|
import commonjs from '@rollup/plugin-commonjs';
|
|
3
3
|
import typescript from '@rollup/plugin-typescript';
|
|
4
4
|
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
|
|
5
|
-
import packageJson from './package.json' assert { type: 'json' };
|
|
6
5
|
|
|
7
6
|
export default {
|
|
8
7
|
input: 'src/index.tsx',
|
|
9
8
|
output: [
|
|
10
9
|
{
|
|
11
|
-
file:
|
|
10
|
+
file: 'dist/index.cjs',
|
|
12
11
|
format: 'cjs',
|
|
13
12
|
sourcemap: true,
|
|
14
13
|
exports: 'named',
|
|
15
14
|
interop: 'auto',
|
|
16
15
|
},
|
|
17
16
|
{
|
|
18
|
-
file:
|
|
17
|
+
file: 'dist/index.mjs',
|
|
19
18
|
format: 'esm',
|
|
20
19
|
sourcemap: true,
|
|
21
20
|
},
|
package/src/index.tsx
CHANGED
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
useLayoutEffect,
|
|
6
6
|
forwardRef,
|
|
7
7
|
useCallback,
|
|
8
|
+
useMemo,
|
|
8
9
|
ClipboardEvent,
|
|
9
10
|
ChangeEvent,
|
|
10
11
|
FocusEvent,
|
|
@@ -16,6 +17,7 @@ import {
|
|
|
16
17
|
ThousandStyle,
|
|
17
18
|
formatValueForDisplay,
|
|
18
19
|
removeThousandSeparators,
|
|
20
|
+
validateNumoraInputOptions,
|
|
19
21
|
type CaretPositionInfo,
|
|
20
22
|
type FormattingOptions,
|
|
21
23
|
} from 'numora';
|
|
@@ -26,15 +28,52 @@ import {
|
|
|
26
28
|
handleNumoraOnPaste,
|
|
27
29
|
} from './handlers';
|
|
28
30
|
|
|
31
|
+
export interface NumoraHTMLInputElement extends HTMLInputElement {
|
|
32
|
+
rawValue?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type NumoraInputChangeEvent = Omit<ChangeEvent<HTMLInputElement>, 'target'> & {
|
|
36
|
+
target: NumoraHTMLInputElement;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Creates a complete synthetic change event from a real HTMLInputElement.
|
|
41
|
+
* Used when a change needs to be signalled without an actual DOM change event
|
|
42
|
+
* (e.g. after paste with preventDefault, or after a controlled-value reformat).
|
|
43
|
+
*/
|
|
44
|
+
function createSyntheticChangeEvent(input: HTMLInputElement): NumoraInputChangeEvent {
|
|
45
|
+
const nativeEvent = new Event('change', { bubbles: true, cancelable: false });
|
|
46
|
+
return {
|
|
47
|
+
nativeEvent,
|
|
48
|
+
target: input as NumoraHTMLInputElement,
|
|
49
|
+
currentTarget: input,
|
|
50
|
+
type: 'change',
|
|
51
|
+
bubbles: true,
|
|
52
|
+
cancelable: false,
|
|
53
|
+
defaultPrevented: false,
|
|
54
|
+
eventPhase: Event.AT_TARGET,
|
|
55
|
+
isTrusted: false,
|
|
56
|
+
timeStamp: Date.now(),
|
|
57
|
+
isDefaultPrevented: () => false,
|
|
58
|
+
isPropagationStopped: () => false,
|
|
59
|
+
persist: () => {},
|
|
60
|
+
preventDefault: () => {},
|
|
61
|
+
stopPropagation: () => {},
|
|
62
|
+
stopImmediatePropagation: () => {},
|
|
63
|
+
} as unknown as NumoraInputChangeEvent;
|
|
64
|
+
}
|
|
65
|
+
|
|
29
66
|
export interface NumoraInputProps
|
|
30
67
|
extends Omit<
|
|
31
68
|
InputHTMLAttributes<HTMLInputElement>,
|
|
32
69
|
'onChange' | 'type' | 'inputMode' | 'onFocus' | 'onBlur'
|
|
33
70
|
> {
|
|
34
71
|
maxDecimals?: number;
|
|
35
|
-
onChange?: (e:
|
|
72
|
+
onChange?: (e: NumoraInputChangeEvent) => void;
|
|
36
73
|
onFocus?: (e: FocusEvent<HTMLInputElement>) => void;
|
|
37
74
|
onBlur?: (e: FocusEvent<HTMLInputElement>) => void;
|
|
75
|
+
/** Called with the raw (unformatted) numeric string on every value change. */
|
|
76
|
+
onRawValueChange?: (rawValue: string | undefined) => void;
|
|
38
77
|
|
|
39
78
|
formatOn?: FormatOn;
|
|
40
79
|
thousandSeparator?: string;
|
|
@@ -56,6 +95,7 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
56
95
|
onBlur,
|
|
57
96
|
onKeyDown,
|
|
58
97
|
onFocus,
|
|
98
|
+
onRawValueChange,
|
|
59
99
|
formatOn = FormatOn.Blur,
|
|
60
100
|
thousandSeparator = ',',
|
|
61
101
|
thousandStyle = ThousandStyle.Thousand,
|
|
@@ -70,11 +110,26 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
70
110
|
...rest
|
|
71
111
|
} = props;
|
|
72
112
|
|
|
113
|
+
validateNumoraInputOptions({
|
|
114
|
+
decimalMaxLength: maxDecimals,
|
|
115
|
+
decimalMinLength,
|
|
116
|
+
formatOn,
|
|
117
|
+
thousandSeparator,
|
|
118
|
+
thousandStyle,
|
|
119
|
+
decimalSeparator,
|
|
120
|
+
enableCompactNotation,
|
|
121
|
+
enableNegative,
|
|
122
|
+
enableLeadingZeros,
|
|
123
|
+
rawValueMode,
|
|
124
|
+
});
|
|
125
|
+
|
|
73
126
|
const internalInputRef = useRef<HTMLInputElement>(null);
|
|
74
127
|
const caretInfoRef = useRef<CaretPositionInfo | undefined>(undefined);
|
|
75
128
|
const lastCaretPosRef = useRef<number | null>(null);
|
|
76
129
|
|
|
77
|
-
|
|
130
|
+
// Memoize to give callbacks a stable reference - avoids recreating all
|
|
131
|
+
// useCallback functions on every render when primitive props haven't changed.
|
|
132
|
+
const formattingOptions: FormattingOptions = useMemo(() => ({
|
|
78
133
|
formatOn,
|
|
79
134
|
thousandSeparator,
|
|
80
135
|
ThousandStyle: thousandStyle,
|
|
@@ -84,7 +139,8 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
84
139
|
enableNegative,
|
|
85
140
|
enableLeadingZeros,
|
|
86
141
|
rawValueMode,
|
|
87
|
-
}
|
|
142
|
+
}), [formatOn, thousandSeparator, thousandStyle, decimalSeparator, decimalMinLength,
|
|
143
|
+
enableCompactNotation, enableNegative, enableLeadingZeros, rawValueMode]);
|
|
88
144
|
|
|
89
145
|
const getInitialValue = (): string => {
|
|
90
146
|
const valueToFormat = controlledValue !== undefined ? controlledValue : defaultValue;
|
|
@@ -97,6 +153,12 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
97
153
|
|
|
98
154
|
const [displayValue, setDisplayValue] = useState<string>(getInitialValue);
|
|
99
155
|
|
|
156
|
+
// Track the current displayValue via a ref so the controlled-value useEffect
|
|
157
|
+
// can compare against it without adding displayValue as a dependency (which
|
|
158
|
+
// would cause the effect to re-run on every keystroke).
|
|
159
|
+
const displayValueRef = useRef<string>(displayValue);
|
|
160
|
+
displayValueRef.current = displayValue;
|
|
161
|
+
|
|
100
162
|
// Sync external ref with internal ref
|
|
101
163
|
useLayoutEffect(() => {
|
|
102
164
|
if (!ref) return;
|
|
@@ -107,36 +169,27 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
107
169
|
}
|
|
108
170
|
}, [ref]);
|
|
109
171
|
|
|
110
|
-
// When controlled value
|
|
172
|
+
// When the controlled value or formatting options change, reformat the display.
|
|
173
|
+
// Uses displayValueRef (not displayValue in deps) to avoid re-running on every keystroke.
|
|
174
|
+
// Does NOT call onChange - that would create a circular loop with react-hook-form Controller.
|
|
111
175
|
useEffect(() => {
|
|
112
176
|
if (controlledValue !== undefined) {
|
|
113
177
|
const { formatted, raw } = formatValueForDisplay(String(controlledValue), maxDecimals, formattingOptions);
|
|
114
|
-
if (formatted !==
|
|
178
|
+
if (formatted !== displayValueRef.current) {
|
|
115
179
|
setDisplayValue(formatted);
|
|
116
180
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (onChange && internalInputRef.current) {
|
|
120
|
-
const input = internalInputRef.current;
|
|
121
|
-
// Set DOM value so e.target.value returns the formatted value
|
|
122
|
-
const nativeSetter = Object.getOwnPropertyDescriptor(
|
|
123
|
-
window.HTMLInputElement.prototype, 'value'
|
|
124
|
-
)?.set;
|
|
125
|
-
nativeSetter?.call(input, formatted);
|
|
126
|
-
(input as any).rawValue = raw;
|
|
127
|
-
const changeEvent = {
|
|
128
|
-
target: input,
|
|
129
|
-
currentTarget: input,
|
|
130
|
-
} as unknown as ChangeEvent<HTMLInputElement>;
|
|
131
|
-
onChange(changeEvent);
|
|
181
|
+
if (internalInputRef.current) {
|
|
182
|
+
(internalInputRef.current as NumoraHTMLInputElement).rawValue = raw;
|
|
132
183
|
}
|
|
184
|
+
onRawValueChange?.(raw);
|
|
133
185
|
}
|
|
134
186
|
}
|
|
135
|
-
}, [controlledValue, maxDecimals, formattingOptions
|
|
136
|
-
formattingOptions.enableCompactNotation, formattingOptions.enableLeadingZeros, formattingOptions.enableNegative,
|
|
137
|
-
formattingOptions.formatOn, formattingOptions.rawValueMode, formattingOptions.thousandSeparator]);
|
|
187
|
+
}, [controlledValue, maxDecimals, formattingOptions, onRawValueChange]);
|
|
138
188
|
|
|
139
|
-
// Restore cursor position after render
|
|
189
|
+
// Restore cursor position after render.
|
|
190
|
+
// No dependency array is intentional: this must run after every render so it catches
|
|
191
|
+
// the re-render triggered by setDisplayValue in handleChange/handlePaste.
|
|
192
|
+
// lastCaretPosRef is a ref (not reactive), so it cannot be a dependency.
|
|
140
193
|
useLayoutEffect(() => {
|
|
141
194
|
if (internalInputRef.current && lastCaretPosRef.current !== null) {
|
|
142
195
|
const input = internalInputRef.current;
|
|
@@ -153,9 +206,6 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
153
206
|
formattingOptions,
|
|
154
207
|
});
|
|
155
208
|
|
|
156
|
-
// Store cursor position AFTER core library has calculated and set it
|
|
157
|
-
// The core library modifies the DOM element directly during handleNumoraOnChange
|
|
158
|
-
// Read from the input ref (which is the same element) to get the position set by the core library
|
|
159
209
|
if (internalInputRef.current) {
|
|
160
210
|
const cursorPos = internalInputRef.current.selectionStart;
|
|
161
211
|
if (cursorPos !== null && cursorPos !== undefined) {
|
|
@@ -165,20 +215,19 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
165
215
|
|
|
166
216
|
caretInfoRef.current = undefined;
|
|
167
217
|
|
|
168
|
-
(e.target as
|
|
218
|
+
(e.target as NumoraHTMLInputElement).rawValue = rawValue;
|
|
219
|
+
onRawValueChange?.(rawValue);
|
|
169
220
|
|
|
170
221
|
setDisplayValue(value);
|
|
171
222
|
|
|
172
223
|
if (onChange) {
|
|
173
|
-
onChange(e);
|
|
224
|
+
onChange(e as unknown as NumoraInputChangeEvent);
|
|
174
225
|
}
|
|
175
|
-
}, [maxDecimals, formattingOptions, onChange]);
|
|
226
|
+
}, [maxDecimals, formattingOptions, onChange, onRawValueChange]);
|
|
176
227
|
|
|
177
228
|
const handleKeyDown = useCallback((e: KeyboardEvent<HTMLInputElement>) => {
|
|
178
229
|
const coreCaretInfo = handleNumoraOnKeyDown(e, formattingOptions);
|
|
179
230
|
|
|
180
|
-
// Always capture cursor position info, even if core library doesn't return it
|
|
181
|
-
// This is needed for cursor position calculation during normal typing (not just Delete/Backspace)
|
|
182
231
|
if (!coreCaretInfo && internalInputRef.current) {
|
|
183
232
|
const selectionStart = internalInputRef.current.selectionStart ?? 0;
|
|
184
233
|
const selectionEnd = internalInputRef.current.selectionEnd ?? 0;
|
|
@@ -201,11 +250,9 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
201
250
|
formattingOptions,
|
|
202
251
|
});
|
|
203
252
|
|
|
204
|
-
// For paste, we often want to move cursor to the end of pasted content
|
|
205
|
-
// handleNumoraOnPaste already handles DOM value and cursor, but React will overwrite it.
|
|
206
|
-
// So we capture where the core logic set the cursor.
|
|
207
253
|
lastCaretPosRef.current = (e.target as HTMLInputElement).selectionStart;
|
|
208
|
-
(e.target as
|
|
254
|
+
(e.target as NumoraHTMLInputElement).rawValue = rawValue;
|
|
255
|
+
onRawValueChange?.(rawValue);
|
|
209
256
|
|
|
210
257
|
setDisplayValue(value);
|
|
211
258
|
|
|
@@ -213,24 +260,30 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
213
260
|
onPaste(e);
|
|
214
261
|
}
|
|
215
262
|
|
|
216
|
-
//
|
|
217
|
-
//
|
|
263
|
+
// Paste calls e.preventDefault() internally, so React's onChange never fires.
|
|
264
|
+
// We synthesise a proper change event so consumers see a typed ChangeEvent.
|
|
218
265
|
if (onChange) {
|
|
219
|
-
|
|
220
|
-
onChange(changeEvent);
|
|
266
|
+
onChange(createSyntheticChangeEvent(e.target as HTMLInputElement));
|
|
221
267
|
}
|
|
222
|
-
}, [maxDecimals, formattingOptions, onPaste, onChange]);
|
|
268
|
+
}, [maxDecimals, formattingOptions, onPaste, onChange, onRawValueChange]);
|
|
223
269
|
|
|
224
270
|
const handleFocus = useCallback((e: FocusEvent<HTMLInputElement>) => {
|
|
225
|
-
if (
|
|
226
|
-
|
|
227
|
-
|
|
271
|
+
if (
|
|
272
|
+
formattingOptions.formatOn === FormatOn.Blur &&
|
|
273
|
+
formattingOptions.thousandSeparator &&
|
|
274
|
+
formattingOptions.ThousandStyle !== ThousandStyle.None
|
|
275
|
+
) {
|
|
276
|
+
// Read directly from the DOM element to avoid a stale displayValue closure
|
|
277
|
+
// and to eliminate displayValue from the deps array (which would recreate
|
|
278
|
+
// this callback on every keystroke).
|
|
279
|
+
const currentValue = (e.target as HTMLInputElement).value;
|
|
280
|
+
setDisplayValue(removeThousandSeparators(currentValue, formattingOptions.thousandSeparator!));
|
|
228
281
|
}
|
|
229
282
|
|
|
230
283
|
if (onFocus) {
|
|
231
284
|
onFocus(e);
|
|
232
285
|
}
|
|
233
|
-
}, [formattingOptions, onFocus
|
|
286
|
+
}, [formattingOptions, onFocus]);
|
|
234
287
|
|
|
235
288
|
const handleBlur = useCallback((e: FocusEvent<HTMLInputElement>) => {
|
|
236
289
|
const { value, rawValue } = handleNumoraOnBlur(e, {
|
|
@@ -238,13 +291,14 @@ const NumoraInput = forwardRef<HTMLInputElement, NumoraInputProps>((props, ref)
|
|
|
238
291
|
formattingOptions,
|
|
239
292
|
});
|
|
240
293
|
|
|
241
|
-
(e.target as
|
|
294
|
+
(e.target as NumoraHTMLInputElement).rawValue = rawValue;
|
|
295
|
+
onRawValueChange?.(rawValue);
|
|
242
296
|
setDisplayValue(value);
|
|
243
297
|
|
|
244
298
|
if (onBlur) {
|
|
245
299
|
onBlur(e);
|
|
246
300
|
}
|
|
247
|
-
}, [maxDecimals, formattingOptions, onBlur]);
|
|
301
|
+
}, [maxDecimals, formattingOptions, onBlur, onRawValueChange]);
|
|
248
302
|
|
|
249
303
|
return (
|
|
250
304
|
<input
|