为营养计算器加入饮食记录功能
本次做出以下决策: - 在计算器内建立常见食物成分库,共 30 种,按每 100 g 记录热量、蛋白质、脂肪、碳水化合物、膳食纤维、钙、铁、锌、维生素 A、维生素 C、钠 - 食物按主食、蛋白质、奶豆、蔬菜、水果、油脂坚果 6 类分组,并支持分类筛选与按名称搜索 - 记录时按单位换算录入(鸡蛋按个、油条按根、全麦面包按片、水果按个),不必逐项称重 - 已摄入量按 90% / 110% 两个阈值判定并上色:不足、达标、超标 - 钠按上限指标单独处理,不超过目标 110% 即视为达标 - 增加一键填入示例早餐(鸡蛋 1 个 + 牛奶 500 ml + 油条 2 根)与逐条删除、全部清空 - 在算法说明中补充食物成分数据来源
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
import { useState, useMemo } from 'react';
|
import { useState, useMemo } from 'react';
|
||||||
import { Activity, Flame, Target, Droplets, Info, AlertTriangle, Footprints, Beef, Wheat, ChevronDown } from 'lucide-react';
|
import {
|
||||||
|
Activity, Flame, Target, Droplets, Info, AlertTriangle, Footprints, Beef, Wheat,
|
||||||
|
ChevronDown, Search, Plus, Trash2, Utensils, ClipboardList,
|
||||||
|
} from 'lucide-react';
|
||||||
|
|
||||||
// 年龄别 BMI 百分位(近似值,用于 <20 岁):[第5, 第85, 第95] 百分位
|
// 年龄别 BMI 百分位(近似值,用于 <20 岁):[第5, 第85, 第95] 百分位
|
||||||
const BMI_TABLE = {
|
const BMI_TABLE = {
|
||||||
@@ -29,17 +32,61 @@ const MEALS = [
|
|||||||
{ name: '晚餐', pct: 0.25, time: '18:30' },
|
{ name: '晚餐', pct: 0.25, time: '18:30' },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 饮食记录用食物成分库:数值均为「每 100 g(或 100 ml)可食部」
|
||||||
|
// gpu = 每个单位的克重;unit = 录入单位
|
||||||
const FOODS = [
|
const FOODS = [
|
||||||
{ name: '谷类(米面,生重)', base: 250, unit: 'g', note: '碳水、B 族维生素' },
|
// 主食
|
||||||
{ name: '薯类(土豆/红薯)', base: 150, unit: 'g', note: '碳水、钾' },
|
{ name: '米饭(熟)', cat: '主食', unit: 'g', gpu: 1, kcal: 116, p: 2.6, f: 0.3, c: 25.9, fiber: 0.3, ca: 7, fe: 1.3, zn: 0.6, va: 0, vc: 0, na: 2.5 },
|
||||||
{ name: '蔬菜', base: 350, unit: 'g', note: '纤维、维生素' },
|
{ name: '馒头', cat: '主食', unit: 'g', gpu: 1, kcal: 223, p: 7.0, f: 1.1, c: 47.0, fiber: 1.3, ca: 38, fe: 1.8, zn: 0.7, va: 0, vc: 0, na: 165 },
|
||||||
{ name: '水果', base: 200, unit: 'g', note: '维生素 C、钾' },
|
{ name: '面条(熟)', cat: '主食', unit: 'g', gpu: 1, kcal: 110, p: 2.7, f: 0.2, c: 24.3, fiber: 0.4, ca: 8, fe: 1.0, zn: 0.4, va: 0, vc: 0, na: 150 },
|
||||||
{ name: '奶类', base: 300, unit: 'ml', note: '钙、优质蛋白' },
|
{ name: '全麦面包', cat: '主食', unit: '片', gpu: 40, kcal: 246, p: 8.8, f: 3.3, c: 45.4, fiber: 5.0, ca: 42, fe: 2.5, zn: 1.2, va: 0, vc: 0, na: 380 },
|
||||||
{ name: '鸡蛋', base: 1, unit: '个', note: '优质蛋白、卵磷脂' },
|
{ name: '油条', cat: '主食', unit: '根', gpu: 70, kcal: 386, p: 6.9, f: 17.6, c: 51.0, fiber: 0.9, ca: 6, fe: 0.9, zn: 0.5, va: 0, vc: 0, na: 585 },
|
||||||
{ name: '肉 / 鱼虾', base: 120, unit: 'g', note: '铁、锌、蛋白' },
|
{ name: '白粥', cat: '主食', unit: 'g', gpu: 1, kcal: 46, p: 1.1, f: 0.3, c: 9.9, fiber: 0.1, ca: 7, fe: 0.1, zn: 0.2, va: 0, vc: 0, na: 2 },
|
||||||
{ name: '豆制品', base: 80, unit: 'g', note: '植物蛋白、钙' },
|
{ name: '燕麦片(干)', cat: '主食', unit: 'g', gpu: 1, kcal: 377, p: 15.0, f: 6.7, c: 61.0, fiber: 10.0, ca: 186, fe: 7.0, zn: 2.6, va: 0, vc: 0, na: 3 },
|
||||||
{ name: '坚果', base: 15, unit: 'g', note: '不饱和脂肪酸' },
|
{ name: '红薯', cat: '主食', unit: 'g', gpu: 1, kcal: 86, p: 1.6, f: 0.2, c: 20.1, fiber: 3.0, ca: 23, fe: 0.5, zn: 0.2, va: 125, vc: 26, na: 28 },
|
||||||
{ name: '烹调油', base: 25, unit: 'g', note: '必需脂肪酸' },
|
{ name: '土豆', cat: '主食', unit: 'g', gpu: 1, kcal: 77, p: 2.0, f: 0.2, c: 17.2, fiber: 1.2, ca: 8, fe: 0.8, zn: 0.4, va: 0, vc: 27, na: 2.7 },
|
||||||
|
// 蛋白质
|
||||||
|
{ name: '鸡蛋', cat: '蛋白质', unit: '个', gpu: 50, kcal: 144, p: 13.3, f: 8.8, c: 2.8, fiber: 0, ca: 56, fe: 2.0, zn: 1.1, va: 234, vc: 0, na: 131 },
|
||||||
|
{ name: '猪瘦肉', cat: '蛋白质', unit: 'g', gpu: 1, kcal: 143, p: 20.3, f: 6.2, c: 1.5, fiber: 0, ca: 6, fe: 3.0, zn: 3.0, va: 44, vc: 0, na: 57 },
|
||||||
|
{ name: '鸡胸肉', cat: '蛋白质', unit: 'g', gpu: 1, kcal: 133, p: 19.4, f: 5.0, c: 2.5, fiber: 0, ca: 3, fe: 0.6, zn: 0.5, va: 16, vc: 0, na: 34 },
|
||||||
|
{ name: '牛肉(瘦)', cat: '蛋白质', unit: 'g', gpu: 1, kcal: 106, p: 20.2, f: 2.3, c: 1.2, fiber: 0, ca: 9, fe: 2.8, zn: 3.7, va: 0, vc: 0, na: 53 },
|
||||||
|
{ name: '三文鱼', cat: '蛋白质', unit: 'g', gpu: 1, kcal: 139, p: 17.2, f: 7.8, c: 0, fiber: 0, ca: 13, fe: 0.3, zn: 0.4, va: 45, vc: 0, na: 63 },
|
||||||
|
{ name: '虾', cat: '蛋白质', unit: 'g', gpu: 1, kcal: 93, p: 18.6, f: 0.8, c: 2.8, fiber: 0, ca: 62, fe: 1.5, zn: 1.4, va: 0, vc: 0, na: 165 },
|
||||||
|
{ name: '北豆腐', cat: '蛋白质', unit: 'g', gpu: 1, kcal: 116, p: 12.2, f: 4.8, c: 3.8, fiber: 0.5, ca: 138, fe: 1.5, zn: 0.6, va: 0, vc: 0, na: 7 },
|
||||||
|
{ name: '乳清蛋白粉', cat: '蛋白质', unit: 'g', gpu: 1, kcal: 380, p: 80.0, f: 4.0, c: 6.0, fiber: 0, ca: 400, fe: 1.0, zn: 1.0, va: 0, vc: 0, na: 300 },
|
||||||
|
// 奶豆
|
||||||
|
{ name: '牛奶(全脂)', cat: '奶豆', unit: 'ml', gpu: 1, kcal: 64, p: 3.3, f: 3.6, c: 4.7, fiber: 0, ca: 104, fe: 0.1, zn: 0.4, va: 24, vc: 1, na: 37 },
|
||||||
|
{ name: '酸奶(全脂)', cat: '奶豆', unit: 'g', gpu: 1, kcal: 86, p: 3.2, f: 3.3, c: 10.0, fiber: 0, ca: 118, fe: 0.1, zn: 0.5, va: 26, vc: 1, na: 46 },
|
||||||
|
{ name: '豆浆', cat: '奶豆', unit: 'ml', gpu: 1, kcal: 31, p: 3.0, f: 1.6, c: 1.2, fiber: 1.1, ca: 10, fe: 0.5, zn: 0.2, va: 0, vc: 0, na: 3 },
|
||||||
|
// 蔬菜
|
||||||
|
{ name: '小白菜', cat: '蔬菜', unit: 'g', gpu: 1, kcal: 15, p: 1.5, f: 0.3, c: 2.7, fiber: 1.1, ca: 90, fe: 1.9, zn: 0.5, va: 280, vc: 28, na: 73 },
|
||||||
|
{ name: '西兰花', cat: '蔬菜', unit: 'g', gpu: 1, kcal: 36, p: 4.1, f: 0.6, c: 4.3, fiber: 1.6, ca: 67, fe: 1.0, zn: 0.8, va: 1202, vc: 51, na: 18 },
|
||||||
|
{ name: '番茄', cat: '蔬菜', unit: 'g', gpu: 1, kcal: 20, p: 0.9, f: 0.2, c: 4.0, fiber: 0.5, ca: 10, fe: 0.4, zn: 0.1, va: 92, vc: 19, na: 5 },
|
||||||
|
// 水果
|
||||||
|
{ name: '苹果', cat: '水果', unit: '个', gpu: 200, kcal: 53, p: 0.2, f: 0.2, c: 13.5, fiber: 1.2, ca: 4, fe: 0.6, zn: 0.2, va: 3, vc: 4, na: 1.6 },
|
||||||
|
{ name: '香蕉', cat: '水果', unit: '个', gpu: 100, kcal: 93, p: 1.4, f: 0.2, c: 22.0, fiber: 1.2, ca: 7, fe: 0.4, zn: 0.2, va: 10, vc: 8, na: 0.8 },
|
||||||
|
{ name: '橙子', cat: '水果', unit: '个', gpu: 180, kcal: 48, p: 0.8, f: 0.2, c: 11.1, fiber: 0.6, ca: 20, fe: 0.4, zn: 0.1, va: 27, vc: 33, na: 1.2 },
|
||||||
|
{ name: '猕猴桃', cat: '水果', unit: '个', gpu: 80, kcal: 61, p: 0.8, f: 0.6, c: 14.5, fiber: 2.6, ca: 27, fe: 1.2, zn: 0.6, va: 22, vc: 62, na: 10 },
|
||||||
|
// 油脂坚果
|
||||||
|
{ name: '混合坚果', cat: '油脂坚果', unit: 'g', gpu: 1, kcal: 600, p: 20.0, f: 50.0, c: 20.0, fiber: 8.0, ca: 100, fe: 3.0, zn: 3.0, va: 0, vc: 0, na: 250 },
|
||||||
|
{ name: '花生酱', cat: '油脂坚果', unit: 'g', gpu: 1, kcal: 600, p: 25.0, f: 50.0, c: 18.0, fiber: 6.0, ca: 40, fe: 2.0, zn: 2.9, va: 0, vc: 0, na: 400 },
|
||||||
|
{ name: '植物油', cat: '油脂坚果', unit: 'g', gpu: 1, kcal: 899, p: 0, f: 99.9, c: 0, fiber: 0, ca: 9, fe: 0.1, zn: 0, va: 0, vc: 0, na: 0 },
|
||||||
|
];
|
||||||
|
|
||||||
|
const CATS = ['全部', '主食', '蛋白质', '奶豆', '蔬菜', '水果', '油脂坚果'];
|
||||||
|
|
||||||
|
// 食物份量建议(按每日目标热量缩放)
|
||||||
|
const PORTIONS = [
|
||||||
|
{ name: '米面类(生重)', base: 250, unit: 'g', note: '碳水、B 族维生素' },
|
||||||
|
{ name: '燕麦 / 杂粮', base: 150, unit: 'g', note: '碳水、锌' },
|
||||||
|
{ name: '薯类', base: 350, unit: 'g', note: '纤维、维生素' },
|
||||||
|
{ name: '水果', base: 200, unit: 'g', note: '维生素 C、钾' },
|
||||||
|
{ name: '牛奶', base: 300, unit: 'ml', note: '钙、优质蛋白' },
|
||||||
|
{ name: '鸡蛋', base: 1, unit: '个', note: '优质蛋白、胆碱' },
|
||||||
|
{ name: '鱼 / 虾蟹', base: 120, unit: 'g', note: '铁、锌、蛋白' },
|
||||||
|
{ name: '豆制品', base: 80, unit: 'g', note: '植物蛋白、钾' },
|
||||||
|
{ name: '坚果', base: 15, unit: 'g', note: '不饱和脂肪酸' },
|
||||||
|
{ name: '烹调油', base: 25, unit: 'g', note: '必需脂肪酸' },
|
||||||
];
|
];
|
||||||
|
|
||||||
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
||||||
@@ -54,7 +101,7 @@ function bmiStatus(bmi, age, sex) {
|
|||||||
if (age < 20) {
|
if (age < 20) {
|
||||||
const a = clamp(r0(age), 10, 18);
|
const a = clamp(r0(age), 10, 18);
|
||||||
const [p5, p85, p95] = BMI_TABLE[sex][a];
|
const [p5, p85, p95] = BMI_TABLE[sex][a];
|
||||||
if (bmi < p5) return { label: '偏低 / 偏瘦', tone: 'sky', p5, p85, p95, youth: true };
|
if (bmi < p5) return { label: '偏瘦 / 偏轻', tone: 'sky', p5, p85, p95, youth: true };
|
||||||
if (bmi < p85) return { label: '健康范围', tone: 'emerald', p5, p85, p95, youth: true };
|
if (bmi < p85) return { label: '健康范围', tone: 'emerald', p5, p85, p95, youth: true };
|
||||||
if (bmi < p95) return { label: '偏重', tone: 'amber', p5, p85, p95, youth: true };
|
if (bmi < p95) return { label: '偏重', tone: 'amber', p5, p85, p95, youth: true };
|
||||||
return { label: '肥胖', tone: 'rose', p5, p85, p95, youth: true };
|
return { label: '肥胖', tone: 'rose', p5, p85, p95, youth: true };
|
||||||
@@ -72,21 +119,21 @@ const TONES = {
|
|||||||
rose: { text: 'text-rose-600', bg: 'bg-rose-50', border: 'border-rose-200', bar: 'bg-rose-400' },
|
rose: { text: 'text-rose-600', bg: 'bg-rose-50', border: 'border-rose-200', bar: 'bg-rose-400' },
|
||||||
};
|
};
|
||||||
|
|
||||||
function getMicros(age, sex, weight) {
|
function getMicros(age, sex) {
|
||||||
const youth = age < 18;
|
const youth = age < 18;
|
||||||
const male = sex === 'male';
|
const male = sex === 'male';
|
||||||
return [
|
return [
|
||||||
{ name: '膳食纤维', value: '25–30', unit: 'g', note: '全谷、蔬菜、水果' },
|
{ name: '膳食纤维', value: '25–30', unit: 'g', note: '全谷物、燕麦、水果' },
|
||||||
{ name: '钙', value: youth ? 1200 : 800, unit: 'mg', note: '500 ml 奶基本够' },
|
{ name: '钙', value: youth ? 1200 : 800, unit: 'mg', note: '500 ml 牛奶基本够' },
|
||||||
{ name: '铁', value: youth ? (male ? 12 : 15) : (male ? 12 : 18), unit: 'mg', note: '红肉、动物血' },
|
{ name: '铁', value: youth ? (male ? 12 : 15) : (male ? 12 : 18), unit: 'mg', note: '红肉、动物血' },
|
||||||
{ name: '锌', value: youth ? (male ? 12 : 9) : (male ? 12.5 : 7.5), unit: 'mg', note: '贝类、红肉、坚果' },
|
{ name: '锌', value: youth ? (male ? 12 : 9) : (male ? 12.5 : 7.5), unit: 'mg', note: '贝类、红肉、坚果' },
|
||||||
{ name: '镁', value: youth ? (male ? 400 : 360) : 330, unit: 'mg', note: '坚果、全谷、深绿叶菜' },
|
{ name: '镁', value: youth ? (male ? 400 : 360) : 330, unit: 'mg', note: '坚果、全谷、深绿色叶菜' },
|
||||||
{ name: '维生素 D', value: youth ? 15 : 10, unit: 'µg', note: '日照不足需补充' },
|
{ name: '维生素 D', value: youth ? 15 : 10, unit: 'µg', note: '日照不足需补充' },
|
||||||
{ name: '维生素 A', value: male ? (youth ? 900 : 800) : 700, unit: 'µg', note: '胡萝卜、蛋黄、肝脏' },
|
{ name: '维生素 A', value: male ? (youth ? 900 : 800) : 700, unit: 'µg', note: '胡萝卜、蛋黄、肝脏' },
|
||||||
{ name: '维生素 C', value: 100, unit: 'mg', note: '新鲜果蔬,助铁吸收' },
|
{ name: '维生素 C', value: 100, unit: 'mg', note: '新鲜果蔬,助铁吸收' },
|
||||||
{ name: '维生素 B1', value: youth ? (male ? 1.4 : 1.2) : (male ? 1.4 : 1.2), unit: 'mg', note: '全谷、瘦肉、豆类' },
|
{ name: '维生素 B1', value: youth ? (male ? 1.4 : 1.2) : 1.4, unit: 'mg', note: '全谷、猪肉、豆类' },
|
||||||
{ name: '维生素 B2', value: youth ? (male ? 1.5 : 1.2) : (male ? 1.4 : 1.2), unit: 'mg', note: '奶、蛋、肝脏' },
|
{ name: '维生素 B2', value: youth ? (male ? 1.5 : 1.2) : 1.4, unit: 'mg', note: '奶、蛋、肝脏' },
|
||||||
{ name: '钠(上限)', value: '≤ 2000', unit: 'mg', note: '相当于盐 < 5 g' },
|
{ name: '钠(上限)', value: '≤ 2000', unit: 'mg', note: '相当于盐 < 5 g' },
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,6 +153,22 @@ function StatCard({ icon, accent, title, value, unit, sub }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let seq = 0;
|
||||||
|
const nextId = () => ++seq;
|
||||||
|
|
||||||
|
function makeItem(food, qty) {
|
||||||
|
return {
|
||||||
|
id: nextId(),
|
||||||
|
name: food.name,
|
||||||
|
unit: food.unit,
|
||||||
|
gpu: food.gpu,
|
||||||
|
qty,
|
||||||
|
kcal: food.kcal, p: food.p, f: food.f, c: food.c,
|
||||||
|
fiber: food.fiber, ca: food.ca, fe: food.fe, zn: food.zn,
|
||||||
|
va: food.va, vc: food.vc, na: food.na,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function DailyNutritionCalculator() {
|
export default function DailyNutritionCalculator() {
|
||||||
const [sex, setSex] = useState('male');
|
const [sex, setSex] = useState('male');
|
||||||
const [age, setAge] = useState(16);
|
const [age, setAge] = useState(16);
|
||||||
@@ -115,6 +178,12 @@ export default function DailyNutritionCalculator() {
|
|||||||
const [goal, setGoal] = useState('gain');
|
const [goal, setGoal] = useState('gain');
|
||||||
const [showFormula, setShowFormula] = useState(false);
|
const [showFormula, setShowFormula] = useState(false);
|
||||||
|
|
||||||
|
// 饮食记录
|
||||||
|
const [log, setLog] = useState([]);
|
||||||
|
const [query, setQuery] = useState('');
|
||||||
|
const [cat, setCat] = useState('全部');
|
||||||
|
const [draft, setDraft] = useState({});
|
||||||
|
|
||||||
const r = useMemo(() => {
|
const r = useMemo(() => {
|
||||||
const H = Number(height) || 0;
|
const H = Number(height) || 0;
|
||||||
const W = Number(weight) || 0;
|
const W = Number(weight) || 0;
|
||||||
@@ -181,15 +250,74 @@ export default function DailyNutritionCalculator() {
|
|||||||
targetLo: hm > 0 ? 20 * hm * hm : 0,
|
targetLo: hm > 0 ? 20 * hm * hm : 0,
|
||||||
targetHi: hm > 0 ? 21 * hm * hm : 0,
|
targetHi: hm > 0 ? 21 * hm * hm : 0,
|
||||||
healthyMin: hm > 0 ? status.p5 * hm * hm : 0,
|
healthyMin: hm > 0 ? status.p5 * hm * hm : 0,
|
||||||
|
// 饮食记录判定用目标
|
||||||
|
fiberTarget: 27,
|
||||||
|
caTarget: youth ? 1200 : 800,
|
||||||
|
vcTarget: 100,
|
||||||
|
naTarget: 2000,
|
||||||
};
|
};
|
||||||
}, [sex, age, height, weight, steps, goal]);
|
}, [sex, age, height, weight, steps, goal]);
|
||||||
|
|
||||||
|
const totals = useMemo(() => {
|
||||||
|
const t = { kcal: 0, p: 0, f: 0, c: 0, fiber: 0, ca: 0, fe: 0, zn: 0, va: 0, vc: 0, na: 0 };
|
||||||
|
log.forEach((item) => {
|
||||||
|
const k = (item.qty * item.gpu) / 100;
|
||||||
|
t.kcal += item.kcal * k;
|
||||||
|
t.p += item.p * k;
|
||||||
|
t.f += item.f * k;
|
||||||
|
t.c += item.c * k;
|
||||||
|
t.fiber += item.fiber * k;
|
||||||
|
t.ca += item.ca * k;
|
||||||
|
t.fe += item.fe * k;
|
||||||
|
t.zn += item.zn * k;
|
||||||
|
t.va += item.va * k;
|
||||||
|
t.vc += item.vc * k;
|
||||||
|
t.na += item.na * k;
|
||||||
|
});
|
||||||
|
return t;
|
||||||
|
}, [log]);
|
||||||
|
|
||||||
const t = TONES[r.status.tone];
|
const t = TONES[r.status.tone];
|
||||||
const pctOf = (v) => clamp(((v - 13) / (30 - 13)) * 100, 0, 100);
|
const pctOf = (v) => clamp(((v - 13) / (30 - 13)) * 100, 0, 100);
|
||||||
const segW = (a, b) => Math.max(0, pctOf(b) - pctOf(a));
|
const segW = (a, b) => Math.max(0, pctOf(b) - pctOf(a));
|
||||||
const micros = getMicros(Number(age) || 0, sex, Number(weight) || 0);
|
const micros = getMicros(Number(age) || 0, sex);
|
||||||
const underweight = r.youth ? r.bmi < r.status.p5 : r.bmi < 18.5;
|
const underweight = r.youth ? r.bmi < r.status.p5 : r.bmi < 18.5;
|
||||||
|
|
||||||
|
const filtered = FOODS.filter(
|
||||||
|
(f) => (cat === '全部' || f.cat === cat) && f.name.includes(query.trim())
|
||||||
|
);
|
||||||
|
|
||||||
|
const addFood = (food) => {
|
||||||
|
const raw = draft[food.name];
|
||||||
|
const q = raw === undefined || raw === '' ? 1 : Number(raw);
|
||||||
|
if (!isFinite(q) || q <= 0) return;
|
||||||
|
setLog((prev) => [...prev, makeItem(food, q)]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeLog = (id) => setLog((prev) => prev.filter((i) => i.id !== id));
|
||||||
|
|
||||||
|
const fillDemoMeal = () => {
|
||||||
|
const picks = [['鸡蛋', 1], ['牛奶(全脂)', 500], ['油条', 2]];
|
||||||
|
const items = picks
|
||||||
|
.map(([name, q]) => {
|
||||||
|
const food = FOODS.find((f) => f.name === name);
|
||||||
|
return food ? makeItem(food, q) : null;
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
setLog((prev) => [...prev, ...items]);
|
||||||
|
};
|
||||||
|
|
||||||
|
const LOG_METRICS = [
|
||||||
|
{ key: 'kcal', label: '热量', unit: 'kcal', target: r.calMid, get: (x) => x.kcal, dec: 0 },
|
||||||
|
{ key: 'p', label: '蛋白质', unit: 'g', target: r.pMid, get: (x) => x.p, dec: 1 },
|
||||||
|
{ key: 'f', label: '脂肪', unit: 'g', target: r.fMid, get: (x) => x.f, dec: 1 },
|
||||||
|
{ key: 'c', label: '碳水化合物', unit: 'g', target: r.cMid, get: (x) => x.c, dec: 0 },
|
||||||
|
{ key: 'fiber', label: '膳食纤维', unit: 'g', target: r.fiberTarget, get: (x) => x.fiber, dec: 1 },
|
||||||
|
{ key: 'ca', label: '钙', unit: 'mg', target: r.caTarget, get: (x) => x.ca, dec: 0 },
|
||||||
|
{ key: 'vc', label: '维生素 C', unit: 'mg', target: r.vcTarget, get: (x) => x.vc, dec: 0 },
|
||||||
|
{ key: 'na', label: '钠', unit: 'mg', target: r.naTarget, inverse: true, get: (x) => x.na, dec: 0 },
|
||||||
|
];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-slate-50 p-4 sm:p-6">
|
<div className="min-h-screen bg-slate-50 p-4 sm:p-6">
|
||||||
<div className="max-w-3xl mx-auto">
|
<div className="max-w-3xl mx-auto">
|
||||||
@@ -200,7 +328,7 @@ export default function DailyNutritionCalculator() {
|
|||||||
每日营养摄入计算器
|
每日营养摄入计算器
|
||||||
</h1>
|
</h1>
|
||||||
<p className="text-sm text-slate-500 mt-1">
|
<p className="text-sm text-slate-500 mt-1">
|
||||||
由身高体重算出 BMI,再推算基础代谢、每日热量,并拆解成蛋白质 / 脂肪 / 碳水与各类微量营养素。
|
由身高体重算出 BMI,再推算基础代谢、每日热量,并拆解成蛋白质 / 脂肪 / 碳水与各类微量营养素,最后可按食物记录实际摄入并比对。
|
||||||
</p>
|
</p>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
@@ -307,7 +435,7 @@ export default function DailyNutritionCalculator() {
|
|||||||
sub="下面所有营养素均按此热量推算" />
|
sub="下面所有营养素均按此热量推算" />
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
{/* 三大营养素 */}
|
{/* 三大产能营养素 */}
|
||||||
<section className="rounded-2xl bg-white border border-slate-200 p-4 shadow-sm mb-4">
|
<section className="rounded-2xl bg-white border border-slate-200 p-4 shadow-sm mb-4">
|
||||||
<div className="flex items-center gap-2 mb-3">
|
<div className="flex items-center gap-2 mb-3">
|
||||||
<Beef size={16} className="text-rose-500" />
|
<Beef size={16} className="text-rose-500" />
|
||||||
@@ -323,7 +451,7 @@ export default function DailyNutritionCalculator() {
|
|||||||
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
|
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
|
||||||
{[
|
{[
|
||||||
{ name: '蛋白质', dot: 'bg-rose-400', g: `${r0(r.pLo)}–${r0(r.pHi)}`, kcal: r0(r.pKcal), pct: (r.pKcal / r.totalKcal) * 100, perKg: `${(GOALS[goal].protein[0]).toFixed(1)}–${(GOALS[goal].protein[1]).toFixed(1)} g/kg` },
|
{ name: '蛋白质', dot: 'bg-rose-400', g: `${r0(r.pLo)}–${r0(r.pHi)}`, kcal: r0(r.pKcal), pct: (r.pKcal / r.totalKcal) * 100, perKg: `${(GOALS[goal].protein[0]).toFixed(1)}–${(GOALS[goal].protein[1]).toFixed(1)} g/kg` },
|
||||||
{ name: '脂肪', dot: 'bg-amber-400', g: `${r0(r.fLo)}–${r0(r.fHi)}`, kcal: r0(r.fKcal), pct: (r.fKcal / r.totalKcal) * 100, perKg: `${(r.fMid / (Number(weight) || 1)).toFixed(1)} g/kg` },
|
{ name: '脂肪', dot: 'bg-amber-400', g: `${r0(r.fLo)}–${r0(r.fHi)}`, kcal: r0(r.fKcal), pct: (r.fKcal / r.totalKcal) * 100, perKg: `${(r.fMid / (Number(weight) || 1)).toFixed(1)} g/kg` },
|
||||||
{ name: '碳水化合物', dot: 'bg-sky-400', g: `${r0(r.cLo)}–${r0(r.cHi)}`, kcal: r0(r.cKcal), pct: (r.cKcal / r.totalKcal) * 100, perKg: `${(r.cMid / (Number(weight) || 1)).toFixed(1)} g/kg` },
|
{ name: '碳水化合物', dot: 'bg-sky-400', g: `${r0(r.cLo)}–${r0(r.cHi)}`, kcal: r0(r.cKcal), pct: (r.cKcal / r.totalKcal) * 100, perKg: `${(r.cMid / (Number(weight) || 1)).toFixed(1)} g/kg` },
|
||||||
].map((m) => (
|
].map((m) => (
|
||||||
<div key={m.name} className="rounded-xl bg-slate-50 p-3">
|
<div key={m.name} className="rounded-xl bg-slate-50 p-3">
|
||||||
@@ -401,13 +529,183 @@ export default function DailyNutritionCalculator() {
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
{/* 食物份量 */}
|
{/* 饮食记录 */}
|
||||||
|
<section className="rounded-2xl bg-white border border-slate-200 p-4 shadow-sm mb-4">
|
||||||
|
<div className="flex items-center justify-between flex-wrap gap-2 mb-3">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Utensils size={16} className="text-violet-500" />
|
||||||
|
<h2 className="text-sm font-semibold text-slate-700">饮食记录</h2>
|
||||||
|
</div>
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<button onClick={fillDemoMeal}
|
||||||
|
className="text-xs px-3 py-1.5 rounded-lg border border-slate-200 text-slate-600 hover:bg-slate-50 transition">
|
||||||
|
填入示例早餐
|
||||||
|
</button>
|
||||||
|
<button onClick={() => setLog([])}
|
||||||
|
className="text-xs px-3 py-1.5 rounded-lg border border-slate-200 text-slate-600 hover:bg-slate-50 transition">
|
||||||
|
全部清空
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center gap-2 rounded-lg border border-slate-200 px-3 py-1.5 mb-2">
|
||||||
|
<Search size={14} className="text-slate-400" />
|
||||||
|
<input value={query} onChange={(e) => setQuery(e.target.value)}
|
||||||
|
placeholder="搜索食物名称…"
|
||||||
|
className="w-full text-sm text-slate-800 outline-none bg-transparent" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex flex-wrap gap-1.5 mb-3">
|
||||||
|
{CATS.map((c) => (
|
||||||
|
<button key={c} onClick={() => setCat(c)}
|
||||||
|
className={`text-xs px-2.5 py-1 rounded-full border transition ${cat === c
|
||||||
|
? 'border-emerald-400 bg-emerald-50 text-emerald-700 font-medium'
|
||||||
|
: 'border-slate-200 text-slate-500 hover:bg-slate-50'}`}>
|
||||||
|
{c}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2 max-h-72 overflow-y-auto pr-1">
|
||||||
|
{filtered.map((food) => (
|
||||||
|
<div key={food.name}
|
||||||
|
className="rounded-xl border border-slate-100 bg-slate-50 p-2.5 flex items-center justify-between gap-2">
|
||||||
|
<div className="min-w-0">
|
||||||
|
<p className="text-sm text-slate-700 truncate">{food.name}</p>
|
||||||
|
<p className="text-xs text-slate-400">
|
||||||
|
{food.kcal} kcal / 100{food.unit === 'ml' ? 'ml' : 'g'} · 1{food.unit} ≈ {food.gpu}{food.unit === 'ml' ? 'ml' : 'g'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-1 shrink-0">
|
||||||
|
<input type="number" min={0} step={0.5}
|
||||||
|
value={draft[food.name] ?? 1}
|
||||||
|
onChange={(e) => setDraft((d) => ({ ...d, [food.name]: e.target.value }))}
|
||||||
|
className="w-14 text-sm text-right rounded-md border border-slate-200 px-1.5 py-1 outline-none focus:border-emerald-400" />
|
||||||
|
<span className="text-xs text-slate-400 w-5">{food.unit}</span>
|
||||||
|
<button onClick={() => addFood(food)}
|
||||||
|
className="rounded-md bg-emerald-500 text-white p-1.5 hover:bg-emerald-600 transition">
|
||||||
|
<Plus size={14} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
{filtered.length === 0 && (
|
||||||
|
<p className="text-xs text-slate-400 sm:col-span-2 text-center py-4">没有匹配的食物</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
{/* 已记录 */}
|
||||||
|
<section className="rounded-2xl bg-white border border-slate-200 p-4 shadow-sm mb-4">
|
||||||
|
<div className="flex items-center gap-2 mb-3">
|
||||||
|
<ClipboardList size={16} className="text-violet-500" />
|
||||||
|
<h2 className="text-sm font-semibold text-slate-700">已记录({log.length} 项)</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{log.length === 0 ? (
|
||||||
|
<p className="text-xs text-slate-400 py-3 text-center">
|
||||||
|
还没有记录。在上面选择食物、填好数量,点 + 加入。
|
||||||
|
</p>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div className="overflow-x-auto rounded-xl border border-slate-100">
|
||||||
|
<table className="w-full text-sm">
|
||||||
|
<thead className="bg-slate-50 text-xs text-slate-500">
|
||||||
|
<tr>
|
||||||
|
<th className="text-left font-medium px-3 py-2">食物</th>
|
||||||
|
<th className="text-right font-medium px-3 py-2">数量</th>
|
||||||
|
<th className="text-right font-medium px-3 py-2">热量</th>
|
||||||
|
<th className="text-right font-medium px-3 py-2">蛋白</th>
|
||||||
|
<th className="text-right font-medium px-3 py-2">脂肪</th>
|
||||||
|
<th className="text-right font-medium px-3 py-2">碳水</th>
|
||||||
|
<th className="px-2" />
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{log.map((item) => {
|
||||||
|
const k = (item.qty * item.gpu) / 100;
|
||||||
|
return (
|
||||||
|
<tr key={item.id} className="border-t border-slate-100">
|
||||||
|
<td className="px-3 py-2 text-slate-700">{item.name}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-500">{r1(item.qty)} {item.unit}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-800 font-medium">{r0(item.kcal * k)}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-600">{r1(item.p * k)}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-600">{r1(item.f * k)}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-600">{r1(item.c * k)}</td>
|
||||||
|
<td className="px-2 py-2 text-right">
|
||||||
|
<button onClick={() => removeLog(item.id)}
|
||||||
|
className="text-slate-300 hover:text-rose-500 transition">
|
||||||
|
<Trash2 size={14} />
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
<tr className="border-t border-slate-200 bg-slate-50 font-medium">
|
||||||
|
<td className="px-3 py-2 text-slate-700">合计</td>
|
||||||
|
<td className="px-3 py-2" />
|
||||||
|
<td className="px-3 py-2 text-right text-slate-800">{r0(totals.kcal)}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-800">{r1(totals.p)}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-800">{r1(totals.f)}</td>
|
||||||
|
<td className="px-3 py-2 text-right text-slate-800">{r1(totals.c)}</td>
|
||||||
|
<td />
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3 mt-4">
|
||||||
|
{LOG_METRICS.map((m) => {
|
||||||
|
const val = m.get(totals);
|
||||||
|
const pct = m.target > 0 ? (val / m.target) * 100 : 0;
|
||||||
|
let tone, label;
|
||||||
|
if (m.inverse) {
|
||||||
|
if (pct <= 110) { tone = 'emerald'; label = '达标'; }
|
||||||
|
else { tone = 'rose'; label = '超标'; }
|
||||||
|
} else if (pct < 90) { tone = 'amber'; label = '不足'; }
|
||||||
|
else if (pct <= 110) { tone = 'emerald'; label = '达标'; }
|
||||||
|
else { tone = 'rose'; label = '超标'; }
|
||||||
|
|
||||||
|
const palette = {
|
||||||
|
emerald: { bar: 'bg-emerald-400', text: 'text-emerald-600' },
|
||||||
|
amber: { bar: 'bg-amber-400', text: 'text-amber-600' },
|
||||||
|
rose: { bar: 'bg-rose-400', text: 'text-rose-600' },
|
||||||
|
}[tone];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div key={m.key} className="rounded-xl border border-slate-100 bg-slate-50 p-3">
|
||||||
|
<div className="flex items-center justify-between mb-1.5">
|
||||||
|
<span className="text-xs font-medium text-slate-600">{m.label}</span>
|
||||||
|
<span className={`text-xs font-semibold ${palette.text}`}>
|
||||||
|
{label} · {pct.toFixed(0)}%
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="h-2 rounded-full bg-slate-200 overflow-hidden">
|
||||||
|
<div className={`h-full ${palette.bar} transition-all`}
|
||||||
|
style={{ width: `${clamp(pct, 0, 100)}%` }} />
|
||||||
|
</div>
|
||||||
|
<p className="text-xs text-slate-500 mt-1.5">
|
||||||
|
已摄入 {val.toFixed(m.dec)} / 目标 {m.target.toFixed(m.dec)} {m.unit}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p className="text-xs text-slate-400 mt-3">
|
||||||
|
判定规则:非上限指标 90%–110% 为达标,低于 90% 为不足,高于 110% 为超标;钠为上限指标,不超过 110% 即达标。
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
|
||||||
|
{/* 食物份量建议 */}
|
||||||
<section className="rounded-2xl bg-white border border-slate-200 p-4 shadow-sm mb-4">
|
<section className="rounded-2xl bg-white border border-slate-200 p-4 shadow-sm mb-4">
|
||||||
<h2 className="text-sm font-semibold text-slate-700 mb-3">
|
<h2 className="text-sm font-semibold text-slate-700 mb-3">
|
||||||
折合成食物(按 {r0(r.calMid)} kcal 缩放)
|
搭配成食物(按 {r0(r.calMid)} kcal 缩放)
|
||||||
</h2>
|
</h2>
|
||||||
<div className="grid grid-cols-2 sm:grid-cols-3 gap-2">
|
<div className="grid grid-cols-2 sm:grid-cols-3 gap-2">
|
||||||
{FOODS.map((f) => {
|
{PORTIONS.map((f) => {
|
||||||
const v = f.base * r.scale;
|
const v = f.base * r.scale;
|
||||||
const shown = f.unit === '个' ? r1(v) : r0(v / 5) * 5;
|
const shown = f.unit === '个' ? r1(v) : r0(v / 5) * 5;
|
||||||
return (
|
return (
|
||||||
@@ -423,7 +721,7 @@ export default function DailyNutritionCalculator() {
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
{/* 提醒 */}
|
{/* 提示 */}
|
||||||
{underweight && (
|
{underweight && (
|
||||||
<section className="rounded-2xl bg-amber-50 border border-amber-200 p-4 mb-4">
|
<section className="rounded-2xl bg-amber-50 border border-amber-200 p-4 mb-4">
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
@@ -431,10 +729,10 @@ export default function DailyNutritionCalculator() {
|
|||||||
<div className="text-xs text-amber-800 leading-relaxed space-y-1">
|
<div className="text-xs text-amber-800 leading-relaxed space-y-1">
|
||||||
<p className="font-semibold text-sm">你的 BMI 已偏低,建议先就医确认</p>
|
<p className="font-semibold text-sm">你的 BMI 已偏低,建议先就医确认</p>
|
||||||
<p>
|
<p>
|
||||||
BMI {r1(r.bmi)} 低于同龄同性别第 5 百分位对应值({r1(r.status.p5)})。
|
BMI {r1(r.bmi)} 低于同年龄同性别第 5 百分位对应值({r1(r.status.p5)})。
|
||||||
青少年偏瘦有时只是“晚长”,也可能与肠胃吸收、甲状腺或饮食行为有关。
|
青少年偏瘦有时只是“晚长”,也可能与胃肠吸收、内分泌或饮食行为有关。
|
||||||
</p>
|
</p>
|
||||||
<p>建议挂<b>儿科 / 青少年保健科</b>,用<b>生长曲线</b>评估;有条件再请<b>临床营养师</b>细化食谱。</p>
|
<p>建议挂<b>儿科 / 青少年保健科</b>,用<b>生长曲线</b>评估;有条件再请<b>临床营养师</b>细化膳食。</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
@@ -453,10 +751,11 @@ export default function DailyNutritionCalculator() {
|
|||||||
<ol className="px-4 pb-4 text-xs text-slate-600 leading-relaxed space-y-2 list-decimal list-inside border-t border-slate-100 pt-3">
|
<ol className="px-4 pb-4 text-xs text-slate-600 leading-relaxed space-y-2 list-decimal list-inside border-t border-slate-100 pt-3">
|
||||||
<li><b>BMI</b> = 体重(kg) ÷ 身高(m)²。未满 20 岁按<b>年龄别百分位</b>判定,不套用成人标准。</li>
|
<li><b>BMI</b> = 体重(kg) ÷ 身高(m)²。未满 20 岁按<b>年龄别百分位</b>判定,不套用成人标准。</li>
|
||||||
<li><b>BMR</b> 取 Mifflin-St Jeor 与 Schofield 两公式的平均值,兼顾通用精度与发育期需求。</li>
|
<li><b>BMR</b> 取 Mifflin-St Jeor 与 Schofield 两公式的平均值,兼顾通用精度与发育期需求。</li>
|
||||||
<li><b>活动系数</b>由步数换算:PAL = 1.2 + 步数/1000 × 0.04,上限 1.95(1 万步 ≈ 1.6)。</li>
|
<li><b>活动系数</b>由步数推算:PAL = 1.2 + 步数/1000 × 0.04,上限 1.95(1 万步 ≈ 1.6)。</li>
|
||||||
<li><b>维持热量</b> = BMR × PAL;增重 +300~500,减脂 −300~500(不低于 BMR)。</li>
|
<li><b>维持热量</b> = BMR × PAL;增重 +300~500,减脂 −300~500(不低于 BMR)。</li>
|
||||||
<li><b>蛋白质</b>按 g/kg 设定(增重 1.6–2.0、维持 1.2–1.6、减脂 1.6–1.8);<b>脂肪</b>占总热量 22–33%;<b>碳水</b>取剩余热量 ÷ 4。</li>
|
<li><b>蛋白质</b>按 g/kg 设定(增重、维持、减脂各自区间);<b>脂肪</b>占总热量 22–33%;<b>碳水</b>取剩余热量 ÷ 4。</li>
|
||||||
<li><b>维生素与矿物质</b>按年龄性别查《中国居民膳食营养素参考摄入量》。</li>
|
<li><b>维生素与矿物质</b>按年龄性别查《中国居民膳食营养素参考摄入量》。</li>
|
||||||
|
<li><b>饮食记录</b>按食物成分库中每 100 g 的数值 × 实际重量累加;单位按“每单位克重”换算,数据同样取自《中国居民膳食营养素参考摄入量》与《中国食物成分表》。</li>
|
||||||
<li>额外用 IOM 的 <b>EER 青少年公式</b>交叉校核热量,两法通常相差 10% 以内。</li>
|
<li>额外用 IOM 的 <b>EER 青少年公式</b>交叉校核热量,两法通常相差 10% 以内。</li>
|
||||||
</ol>
|
</ol>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user