Files
omni-tools/src/components/input/NumericInputWithUnit.tsx

176 lines
4.7 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
2025-04-04 00:55:18 -06:00
import { Grid, Select, MenuItem } from '@mui/material';
2025-04-02 19:42:13 -06:00
import TextFieldWithDesc from '@components/options/TextFieldWithDesc';
import Qty from 'js-quantities';
2025-04-03 05:32:47 -06:00
//
const siPrefixes: { [key: string]: number } = {
2025-04-05 06:10:25 +00:00
'Default prefix': 1,
2025-04-03 05:32:47 -06:00
k: 1000,
M: 1000000,
G: 1000000000,
T: 1000000000000,
m: 0.001,
u: 0.000001,
n: 0.000000001,
p: 0.000000000001
};
export default function NumericInputWithUnit(props: {
value: { value: number; unit: string };
2025-04-03 05:48:01 -06:00
disabled?: boolean;
disableChangingUnit?: boolean;
2025-04-05 04:36:22 +00:00
onOwnChange?: (value: { value: number; unit: string }) => void;
2025-04-03 05:32:47 -06:00
defaultPrefix?: string;
}) {
const [inputValue, setInputValue] = useState(props.value.value);
2025-04-05 06:10:25 +00:00
const [prefix, setPrefix] = useState(props.defaultPrefix || 'Default prefix');
// internal display unit
const [unit, setUnit] = useState('');
// Whether user has overridden the unit
const [userSelectedUnit, setUserSelectedUnit] = useState(false);
const [unitKind, setUnitKind] = useState('');
const [unitOptions, setUnitOptions] = useState<string[]>([]);
const [disabled, setDisabled] = useState(props.disabled);
const [disableChangingUnit, setDisableChangingUnit] = useState(
props.disableChangingUnit
);
useEffect(() => {
setDisabled(props.disabled);
setDisableChangingUnit(props.disableChangingUnit);
}, [props.disabled, props.disableChangingUnit]);
useEffect(() => {
if (unitKind != Qty(props.value.unit).kind()) {
// Update the options for what units similar to this one are available
const kind = Qty(props.value.unit).kind();
let units: string[] = [];
if (kind) {
units = Qty.getUnits(kind);
}
2025-04-03 05:48:01 -06:00
if (!units.includes(props.value.unit)) {
units.push(props.value.unit);
}
2025-04-04 01:22:16 -06:00
// Workaround because the lib doesn't list them
if (kind == 'area') {
units.push('km^2');
units.push('mile^2');
units.push('inch^2');
units.push('m^2');
units.push('cm^2');
}
setUnitOptions(units);
setInputValue(props.value.value);
setUnit(props.value.unit);
setUnitKind(kind);
setUserSelectedUnit(false);
return;
}
if (userSelectedUnit) {
if (!isNaN(props.value.value)) {
const converted = Qty(props.value.value, props.value.unit).to(
unit
).scalar;
setInputValue(converted);
} else {
setInputValue(props.value.value);
}
} else {
setInputValue(props.value.value);
setUnit(props.value.unit);
}
}, [props.value.value, props.value.unit, unit]);
const handleUserValueChange = (newValue: number) => {
setInputValue(newValue);
2025-04-02 19:42:13 -06:00
if (props.onOwnChange) {
try {
const converted = Qty(newValue * siPrefixes[prefix], unit).to(
props.value.unit
).scalar;
props.onOwnChange({ unit: props.value.unit, value: converted });
} catch (error) {
console.error('Conversion error', error);
}
}
};
2025-04-03 05:32:47 -06:00
const handlePrefixChange = (newPrefix: string) => {
setPrefix(newPrefix);
};
const handleUserUnitChange = (newUnit: string) => {
if (!newUnit) return;
2025-04-03 05:32:47 -06:00
const oldInputValue = inputValue;
const oldUnit = unit;
setUnit(newUnit);
2025-04-07 22:45:13 +01:00
setPrefix('Default prefix');
2025-04-03 05:32:47 -06:00
const convertedValue = Qty(oldInputValue * siPrefixes[prefix], oldUnit).to(
newUnit
).scalar;
setInputValue(convertedValue);
};
return (
2025-04-05 06:10:25 +00:00
<Grid container spacing={2} alignItems="center">
<Grid item xs={12} md={4}>
2025-04-02 19:42:13 -06:00
<TextFieldWithDesc
disabled={disabled}
type="number"
fullWidth
2025-04-03 05:32:47 -06:00
value={(inputValue / siPrefixes[prefix])
.toFixed(9)
.replace(/(\d*\.\d+?)0+$/, '$1')}
onOwnChange={(value) => handleUserValueChange(parseFloat(value))}
/>
</Grid>
2025-04-03 05:32:47 -06:00
2025-04-05 06:10:25 +00:00
<Grid item xs={12} md={3}>
2025-04-03 05:32:47 -06:00
<Select
fullWidth
disabled={disableChangingUnit}
2025-04-03 05:32:47 -06:00
value={prefix}
onChange={(evt) => {
handlePrefixChange(evt.target.value || '');
2025-04-03 05:32:47 -06:00
}}
>
{Object.keys(siPrefixes).map((key) => (
<MenuItem key={key} value={key}>
{key}
</MenuItem>
))}
</Select>
</Grid>
2025-04-05 06:10:25 +00:00
<Grid item xs={12} md={5}>
2025-04-03 05:48:01 -06:00
<Select
fullWidth
disabled={disableChangingUnit}
2025-04-05 06:10:25 +00:00
placeholder={'Unit'}
value={unit}
onChange={(event) => {
setUserSelectedUnit(true);
handleUserUnitChange(event.target.value || '');
}}
2025-04-03 05:48:01 -06:00
>
{unitOptions.map((key) => (
<MenuItem key={key} value={key}>
{key}
</MenuItem>
))}
</Select>
</Grid>
</Grid>
);
}