|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { Grid, Select, MenuItem } from '@mui/material'; |
| 3 | +import TextFieldWithDesc from '@components/options/TextFieldWithDesc'; |
| 4 | +import Qty from 'js-quantities'; |
| 5 | +// |
| 6 | + |
| 7 | +const siPrefixes: { [key: string]: number } = { |
| 8 | + 'Default prefix': 1, |
| 9 | + k: 1000, |
| 10 | + M: 1000000, |
| 11 | + G: 1000000000, |
| 12 | + T: 1000000000000, |
| 13 | + m: 0.001, |
| 14 | + u: 0.000001, |
| 15 | + n: 0.000000001, |
| 16 | + p: 0.000000000001 |
| 17 | +}; |
| 18 | + |
| 19 | +export default function NumericInputWithUnit(props: { |
| 20 | + value: { value: number; unit: string }; |
| 21 | + disabled?: boolean; |
| 22 | + disableChangingUnit?: boolean; |
| 23 | + onOwnChange?: (value: { value: number; unit: string }) => void; |
| 24 | + defaultPrefix?: string; |
| 25 | +}) { |
| 26 | + const [inputValue, setInputValue] = useState(props.value.value); |
| 27 | + const [prefix, setPrefix] = useState(props.defaultPrefix || 'Default prefix'); |
| 28 | + |
| 29 | + // internal display unit |
| 30 | + const [unit, setUnit] = useState(''); |
| 31 | + |
| 32 | + // Whether user has overridden the unit |
| 33 | + const [userSelectedUnit, setUserSelectedUnit] = useState(false); |
| 34 | + const [unitKind, setUnitKind] = useState(''); |
| 35 | + const [unitOptions, setUnitOptions] = useState<string[]>([]); |
| 36 | + |
| 37 | + const [disabled, setDisabled] = useState(props.disabled); |
| 38 | + const [disableChangingUnit, setDisableChangingUnit] = useState( |
| 39 | + props.disableChangingUnit |
| 40 | + ); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + setDisabled(props.disabled); |
| 44 | + setDisableChangingUnit(props.disableChangingUnit); |
| 45 | + }, [props.disabled, props.disableChangingUnit]); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (unitKind != Qty(props.value.unit).kind()) { |
| 49 | + // Update the options for what units similar to this one are available |
| 50 | + const kind = Qty(props.value.unit).kind(); |
| 51 | + let units: string[] = []; |
| 52 | + if (kind) { |
| 53 | + units = Qty.getUnits(kind); |
| 54 | + } |
| 55 | + |
| 56 | + if (!units.includes(props.value.unit)) { |
| 57 | + units.push(props.value.unit); |
| 58 | + } |
| 59 | + |
| 60 | + // Workaround because the lib doesn't list them |
| 61 | + if (kind == 'area') { |
| 62 | + units.push('km^2'); |
| 63 | + units.push('mile^2'); |
| 64 | + units.push('inch^2'); |
| 65 | + units.push('m^2'); |
| 66 | + units.push('cm^2'); |
| 67 | + } |
| 68 | + setUnitOptions(units); |
| 69 | + setInputValue(props.value.value); |
| 70 | + setUnit(props.value.unit); |
| 71 | + setUnitKind(kind); |
| 72 | + setUserSelectedUnit(false); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (userSelectedUnit) { |
| 77 | + if (!isNaN(props.value.value)) { |
| 78 | + const converted = Qty(props.value.value, props.value.unit).to( |
| 79 | + unit |
| 80 | + ).scalar; |
| 81 | + setInputValue(converted); |
| 82 | + } else { |
| 83 | + setInputValue(props.value.value); |
| 84 | + } |
| 85 | + } else { |
| 86 | + setInputValue(props.value.value); |
| 87 | + setUnit(props.value.unit); |
| 88 | + } |
| 89 | + }, [props.value.value, props.value.unit, unit]); |
| 90 | + |
| 91 | + const handleUserValueChange = (newValue: number) => { |
| 92 | + setInputValue(newValue); |
| 93 | + |
| 94 | + if (props.onOwnChange) { |
| 95 | + try { |
| 96 | + const converted = Qty(newValue * siPrefixes[prefix], unit).to( |
| 97 | + props.value.unit |
| 98 | + ).scalar; |
| 99 | + |
| 100 | + props.onOwnChange({ unit: props.value.unit, value: converted }); |
| 101 | + } catch (error) { |
| 102 | + console.error('Conversion error', error); |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + const handlePrefixChange = (newPrefix: string) => { |
| 108 | + setPrefix(newPrefix); |
| 109 | + }; |
| 110 | + |
| 111 | + const handleUserUnitChange = (newUnit: string) => { |
| 112 | + if (!newUnit) return; |
| 113 | + const oldInputValue = inputValue; |
| 114 | + const oldUnit = unit; |
| 115 | + setUnit(newUnit); |
| 116 | + setPrefix('Default prefix'); |
| 117 | + |
| 118 | + const convertedValue = Qty(oldInputValue * siPrefixes[prefix], oldUnit).to( |
| 119 | + newUnit |
| 120 | + ).scalar; |
| 121 | + setInputValue(convertedValue); |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <Grid container spacing={2} alignItems="center"> |
| 126 | + <Grid item xs={12} md={4}> |
| 127 | + <TextFieldWithDesc |
| 128 | + disabled={disabled} |
| 129 | + type="number" |
| 130 | + fullWidth |
| 131 | + sx={{ width: { xs: '75%', sm: '80%', md: '90%' } }} |
| 132 | + value={(inputValue / siPrefixes[prefix]) |
| 133 | + .toFixed(9) |
| 134 | + .replace(/(\d*\.\d+?)0+$/, '$1')} |
| 135 | + onOwnChange={(value) => handleUserValueChange(parseFloat(value))} |
| 136 | + /> |
| 137 | + </Grid> |
| 138 | + |
| 139 | + <Grid item xs={12} md={3}> |
| 140 | + <Select |
| 141 | + fullWidth |
| 142 | + disabled={disableChangingUnit} |
| 143 | + value={prefix} |
| 144 | + sx={{ width: { xs: '75%', sm: '80%', md: '90%' } }} |
| 145 | + onChange={(evt) => { |
| 146 | + handlePrefixChange(evt.target.value || ''); |
| 147 | + }} |
| 148 | + > |
| 149 | + {Object.keys(siPrefixes).map((key) => ( |
| 150 | + <MenuItem key={key} value={key}> |
| 151 | + {key} |
| 152 | + </MenuItem> |
| 153 | + ))} |
| 154 | + </Select> |
| 155 | + </Grid> |
| 156 | + |
| 157 | + <Grid item xs={12} md={5}> |
| 158 | + <Select |
| 159 | + fullWidth |
| 160 | + disabled={disableChangingUnit} |
| 161 | + placeholder={'Unit'} |
| 162 | + sx={{ width: { xs: '75%', sm: '80%', md: '90%' } }} |
| 163 | + value={unit} |
| 164 | + onChange={(event) => { |
| 165 | + setUserSelectedUnit(true); |
| 166 | + handleUserUnitChange(event.target.value || ''); |
| 167 | + }} |
| 168 | + > |
| 169 | + {unitOptions.map((key) => ( |
| 170 | + <MenuItem key={key} value={key}> |
| 171 | + {key} |
| 172 | + </MenuItem> |
| 173 | + ))} |
| 174 | + </Select> |
| 175 | + </Grid> |
| 176 | + </Grid> |
| 177 | + ); |
| 178 | +} |
0 commit comments