// The real DecimalInput, compiled for the harnesses that load components with // their own `require`, plus helpers to type into it from a walked element tree. import fs from 'node:fs'; import { compileFunction } from 'node:vm'; import { createRequire } from 'node:module'; import React from 'react'; import ts from 'typescript'; import * as decimal from '../../../resources/js/lib/decimal-input.ts'; const require = createRequire(import.meta.url); const module = { exports: {} }; compileFunction( ts.transpileModule( fs.readFileSync( new URL( '../../../resources/js/components/decimal-input.tsx', import.meta.url, ), 'utf8', ), { compilerOptions: { module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX, }, }, ).outputText, ['require', 'module', 'exports'], )( (id) => id === 'react' ? React : id === '@/lib/decimal-input' ? decimal : require(id), module, module.exports, ); export const decimalInputModule = module.exports; export const DecimalInput = module.exports.DecimalInput; /** A number field in a walked tree: the decimal field or a plain number input. */ export const isNumberField = (node) => node?.type === DecimalInput || (node?.type === 'input' && node.props?.type === 'number'); /** Types `value` as the investor would, through whichever field the tree holds. */ export function typeNumber(node, value) { const text = String(value); if (node.type === DecimalInput) { const normalized = decimal.normalizeDecimalInput(text); node.props.onValueChange(normalized === null ? text : normalized); return; } node.props.onChange({ target: { value: text } }); }