feat: convert inputs when switching units

This commit is contained in:
w33ble
2026-01-11 19:47:03 -07:00
parent 7c512f5adb
commit 56c6ac47eb
4 changed files with 52 additions and 3 deletions

View File

@@ -1,4 +1,4 @@
import { calculateGridfinity, GridfinitySpec } from './calculator';
import { calculateGridfinity, GridfinitySpec, convertUnits } from './calculator';
// DOM Elements
const inputWidth = document.getElementById('input-width') as HTMLInputElement;
@@ -69,6 +69,17 @@ function updateResults() {
});
unitMmBtn.addEventListener('click', () => {
if (currentUnit === 'mm') return;
// Convert values
const width = parseFloat(inputWidth.value) || 0;
const length = parseFloat(inputLength.value) || 0;
const height = parseFloat(inputHeight.value) || 0;
if (width > 0) inputWidth.value = convertUnits(width, 'in', 'mm').toFixed(1);
if (length > 0) inputLength.value = convertUnits(length, 'in', 'mm').toFixed(1);
if (height > 0) inputHeight.value = convertUnits(height, 'in', 'mm').toFixed(1);
currentUnit = 'mm';
unitMmBtn.classList.add('bg-white', 'shadow-sm');
unitMmBtn.classList.remove('text-slate-600');
@@ -78,6 +89,17 @@ unitMmBtn.addEventListener('click', () => {
});
unitInBtn.addEventListener('click', () => {
if (currentUnit === 'in') return;
// Convert values
const width = parseFloat(inputWidth.value) || 0;
const length = parseFloat(inputLength.value) || 0;
const height = parseFloat(inputHeight.value) || 0;
if (width > 0) inputWidth.value = convertUnits(width, 'mm', 'in').toFixed(2);
if (length > 0) inputLength.value = convertUnits(length, 'mm', 'in').toFixed(2);
if (height > 0) inputHeight.value = convertUnits(height, 'mm', 'in').toFixed(2);
currentUnit = 'in';
unitInBtn.classList.add('bg-white', 'shadow-sm');
unitInBtn.classList.remove('text-slate-600');