2025-04-02 19:22:29 -06:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2025-04-03 05:32:47 -06:00
|
|
|
import { Grid, TextField, Select, MenuItem } from '@mui/material';
|
2025-04-02 19:42:13 -06:00
|
|
|
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
|
2025-04-02 19:22:29 -06:00
|
|
|
import Autocomplete from '@mui/material/Autocomplete';
|
|
|
|
|
import Qty from 'js-quantities';
|
2025-04-03 05:32:47 -06:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
const siPrefixes: { [key: string]: number } = {
|
|
|
|
|
'': 1,
|
|
|
|
|
k: 1000,
|
|
|
|
|
M: 1000000,
|
|
|
|
|
G: 1000000000,
|
|
|
|
|
T: 1000000000000,
|
|
|
|
|
m: 0.001,
|
|
|
|
|
u: 0.000001,
|
|
|
|
|
n: 0.000000001,
|
|
|
|
|
p: 0.000000000001
|
|
|
|
|
};
|
2025-04-02 19:22:29 -06:00
|
|
|
|
|
|
|
|
export default function NumericInputWithUnit(props: {
|
|
|
|
|
value: { value: number; unit: string };
|
2025-04-02 19:42:13 -06:00
|
|
|
onOwnChange: (value: { value: number; unit: string }, ...baseProps) => void;
|
2025-04-03 05:32:47 -06:00
|
|
|
defaultPrefix?: string;
|
2025-04-02 19:22:29 -06:00
|
|
|
}) {
|
|
|
|
|
const [inputValue, setInputValue] = useState(props.value.value);
|
2025-04-03 05:32:47 -06:00
|
|
|
const [prefix, setPrefix] = useState(props.defaultPrefix || '');
|
2025-04-02 19:22:29 -06:00
|
|
|
const [unit, setUnit] = useState(props.value.unit);
|
|
|
|
|
const [unitOptions, setUnitOptions] = useState<string[]>([]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
try {
|
|
|
|
|
const kind = Qty(props.value.unit).kind();
|
|
|
|
|
const units = Qty.getUnits(kind);
|
|
|
|
|
setUnitOptions(units);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Invalid unit kind', error);
|
|
|
|
|
}
|
|
|
|
|
}, [props.value.unit]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setInputValue(props.value.value);
|
|
|
|
|
setUnit(props.value.unit);
|
|
|
|
|
}, [props.value]);
|
|
|
|
|
|
2025-04-02 19:42:13 -06:00
|
|
|
const handleValueChange = (val: { value: number; unit: string }) => {
|
|
|
|
|
const newValue = val.value;
|
2025-04-02 19:22:29 -06:00
|
|
|
setInputValue(newValue);
|
2025-04-02 19:42:13 -06:00
|
|
|
if (props.onOwnChange) {
|
2025-04-02 19:22:29 -06:00
|
|
|
try {
|
2025-04-03 05:32:47 -06:00
|
|
|
const qty = Qty(newValue * siPrefixes[prefix], unit).to(val.unit);
|
2025-04-02 19:42:13 -06:00
|
|
|
props.onOwnChange({ unit: val.unit, value: qty.scalar });
|
2025-04-02 19:22:29 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Conversion error', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-03 05:32:47 -06:00
|
|
|
const handlePrefixChange = (newPrefix: string) => {
|
|
|
|
|
const oldPrefixValue = siPrefixes[prefix];
|
|
|
|
|
const newPrefixValue = siPrefixes[newPrefix];
|
|
|
|
|
|
|
|
|
|
setPrefix(newPrefix);
|
|
|
|
|
|
|
|
|
|
// Value does not change, it is just re-formatted for display
|
|
|
|
|
// handleValueChange({
|
|
|
|
|
// value: (inputValue * oldPrefixValue) / newPrefixValue,
|
|
|
|
|
// unit: unit
|
|
|
|
|
// });
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-02 19:22:29 -06:00
|
|
|
const handleUnitChange = (newUnit: string) => {
|
|
|
|
|
if (!newUnit) return;
|
2025-04-03 05:32:47 -06:00
|
|
|
const oldInputValue = inputValue;
|
|
|
|
|
const oldUnit = unit;
|
2025-04-02 19:22:29 -06:00
|
|
|
setUnit(newUnit);
|
2025-04-03 05:32:47 -06:00
|
|
|
setPrefix('');
|
|
|
|
|
|
2025-04-02 19:22:29 -06:00
|
|
|
try {
|
2025-04-03 05:32:47 -06:00
|
|
|
const convertedValue = Qty(
|
|
|
|
|
oldInputValue * siPrefixes[prefix],
|
|
|
|
|
oldUnit
|
|
|
|
|
).to(newUnit).scalar;
|
2025-04-02 19:22:29 -06:00
|
|
|
setInputValue(convertedValue);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Unit conversion error', error);
|
|
|
|
|
}
|
2025-04-02 19:42:13 -06:00
|
|
|
|
|
|
|
|
if (props.onOwnChange) {
|
|
|
|
|
try {
|
|
|
|
|
const qty = Qty(inputValue, unit).to(newUnit);
|
|
|
|
|
props.onOwnChange({ unit: newUnit, value: qty.scalar });
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Conversion error', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-02 19:22:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
2025-04-03 05:32:47 -06:00
|
|
|
<Grid
|
|
|
|
|
container
|
|
|
|
|
spacing={2}
|
|
|
|
|
alignItems="center"
|
|
|
|
|
style={{ minWidth: '20rem' }}
|
|
|
|
|
>
|
2025-04-02 19:22:29 -06:00
|
|
|
<Grid item xs={6}>
|
2025-04-02 19:42:13 -06:00
|
|
|
<TextFieldWithDesc
|
|
|
|
|
{...props.baseProps}
|
2025-04-02 19:22:29 -06:00
|
|
|
type="number"
|
|
|
|
|
fullWidth
|
2025-04-03 05:32:47 -06:00
|
|
|
value={(inputValue / siPrefixes[prefix])
|
|
|
|
|
.toFixed(9)
|
|
|
|
|
.replace(/(\d*\.\d+?)0+$/, '$1')}
|
2025-04-02 19:42:13 -06:00
|
|
|
onOwnChange={(value) =>
|
|
|
|
|
handleValueChange({ value: parseFloat(value), unit: unit })
|
|
|
|
|
}
|
2025-04-02 19:22:29 -06:00
|
|
|
label="Value"
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
2025-04-03 05:32:47 -06:00
|
|
|
|
|
|
|
|
<Grid item xs={3}>
|
|
|
|
|
<Select
|
|
|
|
|
fullWidth
|
|
|
|
|
label="Prefix"
|
|
|
|
|
title="Prefix"
|
|
|
|
|
value={prefix}
|
|
|
|
|
onChange={(event, newValue) => {
|
|
|
|
|
handlePrefixChange(newValue.props.value || '');
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{Object.keys(siPrefixes).map((key) => (
|
|
|
|
|
<MenuItem key={key} value={key}>
|
|
|
|
|
{key}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
</Grid>
|
|
|
|
|
|
|
|
|
|
<Grid item xs={3}>
|
2025-04-02 19:22:29 -06:00
|
|
|
<Autocomplete
|
|
|
|
|
options={unitOptions}
|
|
|
|
|
value={unit}
|
2025-04-02 19:42:13 -06:00
|
|
|
onChange={(event, newValue) => {
|
|
|
|
|
handleUnitChange(newValue || '');
|
2025-04-02 19:22:29 -06:00
|
|
|
}}
|
|
|
|
|
renderInput={(params) => (
|
|
|
|
|
<TextField {...params} label="Unit" fullWidth />
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
</Grid>
|
|
|
|
|
</Grid>
|
|
|
|
|
);
|
|
|
|
|
}
|