Skip to content

Commit 3503097

Browse files
committed
Merge branch 'ADP-5313' into develop
2 parents 2441d25 + 1e3db87 commit 3503097

3 files changed

Lines changed: 62 additions & 56 deletions

File tree

src/components/CompoundCalculator.tsx

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
sanitizeNumberInput,
77
makeDefaultValues,
88
useDarkMode,
9-
renderKatexInline,
109
evaluateExpression,
1110
buildReadableExpression,
1211
buildOptionDisplayValues,
@@ -17,12 +16,10 @@ import {
1716
buildSumReadable,
1817
roundResult,
1918
useInitialCalculation,
20-
VariableInput,
19+
LabeledVariableInput,
2120
RowGrid,
2221
ResultSection,
2322
CalculatorWrapper,
24-
getLabelColor,
25-
getSubtextColor,
2623
} from './calculatorUtils';
2724

2825
interface CompoundCalculatorProps {
@@ -31,7 +28,7 @@ interface CompoundCalculatorProps {
3128
variables: Variable[];
3229
rowFormula: string;
3330
resultFormula: string;
34-
defaultRows?: Array<Record<string, number>>;
31+
defaultRows?: Array<Record<string, number | string>>;
3532
}
3633

3734
export const CompoundCalculator: React.FC<CompoundCalculatorProps> = ({
@@ -126,23 +123,14 @@ export const CompoundCalculator: React.FC<CompoundCalculatorProps> = ({
126123
style={{ borderColor: isDark ? 'rgba(63, 63, 70, 0.5)' : '#e5e7eb' }}
127124
>
128125
{globalVariables.map(v => (
129-
<div key={v.variableName} className="flex items-center justify-center gap-3">
130-
<VariableInput
131-
variable={v}
132-
value={globalValues[v.variableName]}
133-
onChange={(val) => updateGlobalValue(v.variableName, val)}
134-
isDark={isDark}
135-
className="w-24"
136-
/>
137-
<span
138-
className="calculator-formula text-sm font-medium shrink-0"
139-
style={{ color: getLabelColor(isDark) }}
140-
dangerouslySetInnerHTML={{ __html: renderKatexInline(v.nameInTheFormula) }}
141-
/>
142-
<span className="text-sm" style={{ color: getSubtextColor(isDark) }}>
143-
{v.variableDescription}
144-
</span>
145-
</div>
126+
<LabeledVariableInput
127+
key={v.variableName}
128+
variable={v}
129+
value={globalValues[v.variableName]}
130+
onChange={(val) => updateGlobalValue(v.variableName, val)}
131+
isDark={isDark}
132+
centered
133+
/>
146134
))}
147135
</div>
148136
</div>

src/components/SimpleCalculator.tsx

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ import {
55
type CalculationResult,
66
sanitizeNumberInput,
77
useDarkMode,
8-
renderKatexInline,
98
initializeRows,
109
createRow,
1110
evaluateRows,
1211
formatSimpleResult,
1312
useInitialCalculation,
14-
VariableInput,
13+
LabeledVariableInput,
1514
RowGrid,
1615
ResultSection,
1716
CalculatorWrapper,
18-
getLabelColor,
19-
getSubtextColor,
2017
} from './calculatorUtils';
2118

2219
interface SimpleCalculatorProps {
@@ -26,7 +23,7 @@ interface SimpleCalculatorProps {
2623
formulaCalculation: string;
2724
isSum?: boolean;
2825
defaultRowCount?: number;
29-
defaultRows?: Array<Record<string, number>>;
26+
defaultRows?: Array<Record<string, number | string>>;
3027
}
3128

3229
export const SimpleCalculator: React.FC<SimpleCalculatorProps> = ({
@@ -76,23 +73,13 @@ export const SimpleCalculator: React.FC<SimpleCalculatorProps> = ({
7673
/* Single-row vertical layout */
7774
<div className="space-y-3">
7875
{variables.map(v => (
79-
<div key={v.variableName} className="flex items-center gap-3">
80-
<VariableInput
81-
variable={v}
82-
value={rows[0].values[v.variableName]}
83-
onChange={(val) => updateValue(rows[0].id, v.variableName, val)}
84-
isDark={isDark}
85-
className="w-24"
86-
/>
87-
<span
88-
className="calculator-formula text-sm font-medium shrink-0"
89-
style={{ color: getLabelColor(isDark) }}
90-
dangerouslySetInnerHTML={{ __html: renderKatexInline(v.nameInTheFormula) }}
91-
/>
92-
<span className="text-sm" style={{ color: getSubtextColor(isDark) }}>
93-
{v.variableDescription}
94-
</span>
95-
</div>
76+
<LabeledVariableInput
77+
key={v.variableName}
78+
variable={v}
79+
value={rows[0].values[v.variableName]}
80+
onChange={(val) => updateValue(rows[0].id, v.variableName, val)}
81+
isDark={isDark}
82+
/>
9683
))}
9784
</div>
9885
) : (

src/components/calculatorUtils.tsx

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,26 @@ export function makeDefaultValues(variables: Variable[]): Record<string, number>
8080

8181
export function initializeRows(
8282
variables: Variable[],
83-
defaultRows?: Array<Record<string, number>>,
83+
defaultRows?: Array<Record<string, number | string>>,
8484
count: number = 1,
8585
): RowData[] {
8686
const defaults = makeDefaultValues(variables);
8787
if (defaultRows) {
88-
return defaultRows.map(overrides => ({
89-
id: nanoid(),
90-
values: { ...defaults, ...overrides },
91-
}));
88+
return defaultRows.map(overrides => {
89+
const resolved: Record<string, number> = {};
90+
for (const [key, val] of Object.entries(overrides)) {
91+
if (typeof val === 'string') {
92+
try {
93+
resolved[key] = new Function(`return (${val})`)() as number;
94+
} catch {
95+
resolved[key] = parseFloat(val) || 0;
96+
}
97+
} else {
98+
resolved[key] = val;
99+
}
100+
}
101+
return { id: nanoid(), values: { ...defaults, ...resolved } };
102+
});
92103
}
93104
return Array.from({ length: count }, () => ({
94105
id: nanoid(),
@@ -271,13 +282,7 @@ export function getFormulaBoxStyle(isDark: boolean): React.CSSProperties {
271282
};
272283
}
273284

274-
export function getButtonStyle(isDark: boolean): React.CSSProperties {
275-
return {
276-
backgroundColor: isDark ? 'rgba(24, 24, 27, 0.7)' : '#ffffff',
277-
borderColor: isDark ? 'rgba(63, 63, 70, 0.5)' : '#e5e7eb',
278-
color: isDark ? '#f1f5f9' : '#0a0a0a',
279-
};
280-
}
285+
export const getButtonStyle = getInputStyle;
281286

282287
export function getAddButtonStyle(isDark: boolean): React.CSSProperties {
283288
return {
@@ -378,6 +383,32 @@ export const VariableInput: React.FC<{
378383
);
379384
};
380385

386+
export const LabeledVariableInput: React.FC<{
387+
variable: Variable;
388+
value: number | string;
389+
onChange: (value: string) => void;
390+
isDark: boolean;
391+
centered?: boolean;
392+
}> = ({ variable, value, onChange, isDark, centered }) => (
393+
<div className={`flex items-center gap-3${centered ? ' justify-center' : ''}`}>
394+
<VariableInput
395+
variable={variable}
396+
value={value}
397+
onChange={onChange}
398+
isDark={isDark}
399+
className="w-24"
400+
/>
401+
<span
402+
className="calculator-formula text-sm font-medium shrink-0"
403+
style={{ color: getLabelColor(isDark) }}
404+
dangerouslySetInnerHTML={{ __html: renderKatexInline(variable.nameInTheFormula) }}
405+
/>
406+
<span className="text-sm" style={{ color: getSubtextColor(isDark) }}>
407+
{variable.variableDescription}
408+
</span>
409+
</div>
410+
);
411+
381412
export const ColumnHeaders: React.FC<{
382413
variables: Variable[];
383414
isDark: boolean;

0 commit comments

Comments
 (0)