More units UI work

This commit is contained in:
Daniel Dunn
2025-04-03 05:48:01 -06:00
parent 70480d0920
commit 7726adf8a3
5 changed files with 46 additions and 32 deletions

View File

@@ -19,6 +19,8 @@ const siPrefixes: { [key: string]: number } = {
export default function NumericInputWithUnit(props: { export default function NumericInputWithUnit(props: {
value: { value: number; unit: string }; value: { value: number; unit: string };
disabled?: boolean;
disableChangingUnit?: boolean;
onOwnChange: (value: { value: number; unit: string }, ...baseProps) => void; onOwnChange: (value: { value: number; unit: string }, ...baseProps) => void;
defaultPrefix?: string; defaultPrefix?: string;
}) { }) {
@@ -31,6 +33,10 @@ export default function NumericInputWithUnit(props: {
try { try {
const kind = Qty(props.value.unit).kind(); const kind = Qty(props.value.unit).kind();
const units = Qty.getUnits(kind); const units = Qty.getUnits(kind);
if (!units.includes(props.value.unit)) {
units.push(props.value.unit);
}
setUnitOptions(units); setUnitOptions(units);
} catch (error) { } catch (error) {
console.error('Invalid unit kind', error); console.error('Invalid unit kind', error);
@@ -102,9 +108,10 @@ export default function NumericInputWithUnit(props: {
alignItems="center" alignItems="center"
style={{ minWidth: '20rem' }} style={{ minWidth: '20rem' }}
> >
<Grid item xs={6}> <Grid item xs={4}>
<TextFieldWithDesc <TextFieldWithDesc
{...props.baseProps} {...props.baseProps}
disabled={props.disabled}
type="number" type="number"
fullWidth fullWidth
value={(inputValue / siPrefixes[prefix]) value={(inputValue / siPrefixes[prefix])
@@ -120,11 +127,12 @@ export default function NumericInputWithUnit(props: {
<Grid item xs={3}> <Grid item xs={3}>
<Select <Select
fullWidth fullWidth
disabled={props.disableChangingUnit}
label="Prefix" label="Prefix"
title="Prefix" title="Prefix"
value={prefix} value={prefix}
onChange={(event, newValue) => { onChange={(event, newValue) => {
handlePrefixChange(newValue.props.value || ''); handlePrefixChange(newValue?.props?.value || '');
}} }}
> >
{Object.keys(siPrefixes).map((key) => ( {Object.keys(siPrefixes).map((key) => (
@@ -135,17 +143,23 @@ export default function NumericInputWithUnit(props: {
</Select> </Select>
</Grid> </Grid>
<Grid item xs={3}> <Grid item xs={5}>
<Autocomplete <Select
options={unitOptions} fullWidth
disabled={props.disableChangingUnit}
label="Unit"
title="Unit"
value={unit} value={unit}
onChange={(event, newValue) => { onChange={(event, newValue) => {
handleUnitChange(newValue || ''); handleUnitChange(newValue?.props?.value || '');
}} }}
renderInput={(params) => ( >
<TextField {...params} label="Unit" fullWidth /> {unitOptions.map((key) => (
)} <MenuItem key={key} value={key}>
/> {key}
</MenuItem>
))}
</Select>
</Grid> </Grid>
</Grid> </Grid>
); );

View File

@@ -1,14 +1,12 @@
export default { export default {
title: 'Material Electrical Properties', title: 'Material Electrical Properties',
columns: [ columns: {
{
resistivity_20c: { resistivity_20c: {
title: 'Resistivity at 20°C', title: 'Resistivity at 20°C',
type: 'number', type: 'number',
unit: 'Ω/m' unit: 'Ω/m'
} }
} },
],
data: { data: {
Copper: { Copper: {
resistivity_20c: 1.68e-8 resistivity_20c: 1.68e-8

View File

@@ -2,8 +2,7 @@ import type { DataTable } from '../types';
const data: DataTable = { const data: DataTable = {
title: 'American Wire Gauge', title: 'American Wire Gauge',
columns: [ columns: {
{
diameter: { diameter: {
title: 'Diameter', title: 'Diameter',
type: 'number', type: 'number',
@@ -12,10 +11,9 @@ const data: DataTable = {
area: { area: {
title: 'Area', title: 'Area',
type: 'number', type: 'number',
unit: 'mm^2' unit: 'mm2'
} }
} },
],
data: { data: {
'0000 AWG': { diameter: 11.684 }, '0000 AWG': { diameter: 11.684 },
'000 AWG': { diameter: 10.405 }, '000 AWG': { diameter: 10.405 },

View File

@@ -45,7 +45,7 @@ const voltagedropinwire: GenericCalcType = {
{ {
name: 'A', name: 'A',
title: 'Wire Area', title: 'Wire Area',
unit: 'mm', unit: 'mm2',
default: 1 default: 1
}, },

View File

@@ -252,6 +252,10 @@ export default async function makeTool(
values.outputVariable === variable.name || values.outputVariable === variable.name ||
valsBoundToPreset[variable.name] !== undefined valsBoundToPreset[variable.name] !== undefined
} }
disableChangingUnit={
values.outputVariable === variable.name ||
valsBoundToPreset[variable.name] !== undefined
}
onOwnChange={(val) => onOwnChange={(val) =>
updateVarField( updateVarField(
variable.name, variable.name,