diff --git a/BmiCalculator.jsx b/BmiCalculator.jsx new file mode 100644 index 0000000..7725215 --- /dev/null +++ b/BmiCalculator.jsx @@ -0,0 +1,323 @@ +import { useState } from 'react'; +import { Ruler, Scale, Activity, Info } from 'lucide-react'; + +const SEGMENTS = [ + { key: 'under', label: '偏瘦', range: '< 18.5', from: 10, to: 18.5, bar: 'bg-sky-400', chip: 'bg-sky-100 text-sky-700' }, + { key: 'normal', label: '正常', range: '18.5 – 24.9', from: 18.5, to: 25, bar: 'bg-emerald-400', chip: 'bg-emerald-100 text-emerald-700' }, + { key: 'over', label: '超重', range: '25 – 29.9', from: 25, to: 30, bar: 'bg-amber-400', chip: 'bg-amber-100 text-amber-700' }, + { key: 'obese', label: '肥胖', range: '≥ 30', from: 30, to: 40, bar: 'bg-rose-400', chip: 'bg-rose-100 text-rose-700' }, +]; + +const ADVICE = { + under: '体重偏低。建议均衡饮食、保证蛋白质与热量摄入,必要时咨询医生或营养师。', + normal: '体重处于健康范围。继续保持均衡饮食和规律运动即可。', + over: '体重略高于健康范围。建议增加有氧运动、控制精制糖与油脂摄入。', + obese: '体重明显偏高。建议咨询医生制定科学的减重与运动计划。', +}; + +const MIN_BMI = 10; +const MAX_BMI = 40; +const LB_PER_KG = 0.45359237; + +const clamp = (v, lo, hi) => Math.min(Math.max(v, lo), hi); + +function categoryFor(bmi) { + if (bmi < 18.5) return SEGMENTS[0]; + if (bmi < 25) return SEGMENTS[1]; + if (bmi < 30) return SEGMENTS[2]; + return SEGMENTS[3]; +} + +export default function BmiCalculator() { + const [unit, setUnit] = useState('metric'); + const [hCm, setHCm] = useState('170'); + const [wKg, setWKg] = useState('65'); + const [ft, setFt] = useState('5'); + const [inch, setInch] = useState('7'); + const [lb, setLb] = useState('143'); + + const meters = unit === 'metric' + ? (parseFloat(hCm) || 0) / 100 + : ((parseFloat(ft) || 0) * 12 + (parseFloat(inch) || 0)) * 0.0254; + + const kg = unit === 'metric' + ? (parseFloat(wKg) || 0) + : (parseFloat(lb) || 0) * LB_PER_KG; + + const valid = meters > 0.5 && meters < 3 && kg > 10 && kg < 500; + const bmi = valid ? kg / (meters * meters) : null; + const cat = bmi !== null ? categoryFor(bmi) : null; + const markerPct = bmi !== null + ? clamp(((bmi - MIN_BMI) / (MAX_BMI - MIN_BMI)) * 100, 0, 100) + : 0; + + const healthyMinKg = valid ? 18.5 * meters * meters : 0; + const healthyMaxKg = valid ? 24.9 * meters * meters : 0; + + const showWeight = (kgValue) => + unit === 'metric' + ? `${kgValue.toFixed(1)} kg` + : `${(kgValue / LB_PER_KG).toFixed(1)} lb`; + + function switchUnit(next) { + if (next === unit) return; + if (next === 'imperial') { + const m = (parseFloat(hCm) || 0) / 100; + const kgv = parseFloat(wKg) || 0; + const totalIn = m / 0.0254; + let f = Math.floor(totalIn / 12); + let i = Math.round(totalIn - f * 12); + if (i === 12) { f += 1; i = 0; } + setFt(String(f || 5)); + setInch(String(i)); + setLb(String(Math.round(kgv / LB_PER_KG) || 143)); + } else { + const m = ((parseFloat(ft) || 0) * 12 + (parseFloat(inch) || 0)) * 0.0254; + const kgv = (parseFloat(lb) || 0) * LB_PER_KG; + setHCm(String(Math.round(m * 100) || 170)); + setWKg(String(Math.round(kgv) || 65)); + } + setUnit(next); + } + + const inputClass = + 'w-full rounded-lg border border-slate-300 bg-white px-3 py-2 text-slate-800 outline-none transition focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200'; + + return ( +
输入身高与体重,实时查看身体质量指数与健康区间
+请输入有效的身高与体重数值。
+ )} +你的 BMI
+对应身高下的健康体重区间
++ {showWeight(healthyMinKg)} – {showWeight(healthyMaxKg)} +
+{ADVICE[cat.key]}
+ )} ++ BMI(身体质量指数)= 体重(kg) ÷ 身高(m)²,是世界卫生组织常用的体重分级筛査指标。 + 它无法区分肌肉与脂肪,也不适用于孕妇、运动员、儿童及 65 岁以上人群,仅供参考,不能替代医学诊断。 +
+