calculadora

import React, { useState, useEffect } from 'react';
import { Card } from '@/components/ui/card';
import { Select } from '@/components/ui/select';

const TemperatureConverter = () => {
const [value, setValue] = useState('0');
const [unit, setUnit] = useState('celsius');

const convertTemp = (value, from) => {
const val = parseFloat(value);
if (isNaN(val)) return { celsius: '', fahrenheit: '', kelvin: '', rankine: '' };

let celsius, fahrenheit, kelvin, rankine;

switch(from) {
case 'celsius':
celsius = val;
fahrenheit = (val * 9/5) + 32;
kelvin = val + 273.15;
rankine = (val + 273.15) * 9/5;
break;
case 'fahrenheit':
celsius = (val - 32) * 5/9;
fahrenheit = val;
kelvin = (val - 32) * 5/9 + 273.15;
rankine = val + 459.67;
break;
case 'kelvin':
celsius = val - 273.15;
fahrenheit = (val * 9/5) - 459.67;
kelvin = val;
rankine = val * 9/5;
break;
case 'rankine':
celsius = (val - 491.67) * 5/9;
fahrenheit = val - 459.67;
kelvin = val * 5/9;
rankine = val;
break;
default:
return { celsius: '', fahrenheit: '', kelvin: '', rankine: '' };
}

return {
celsius: celsius.toFixed(2),
fahrenheit: fahrenheit.toFixed(2),
kelvin: kelvin.toFixed(2),
rankine: rankine.toFixed(2)
};
};

const [results, setResults] = useState(convertTemp(0, 'celsius'));

useEffect(() => {
setResults(convertTemp(value, unit));
}, [value, unit]);

return (

Conversor de unidades

setValue(e.target.value)}
className="w-full px-4 py-2 bg-slate-800 rounded text-white border border-slate-700 focus:outline-none focus:border-blue-500"
/>

{results.celsius}
°C (Celsius)
{results.fahrenheit}
°F (Fahrenheit)
{results.kelvin}
K (Kelvin)
{results.rankine}
°R (Rankine)


);
};

export default TemperatureConverter;

Subir