NutritionManualFieldControl.tsx
1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React from "react";
import { Input } from "../ui/input";
import { Label } from "../ui/label";
import { Checkbox } from "../ui/checkbox";
import type { NutritionManualFieldSpec } from "../../lib/nutritionManualEntry";
function isTruthyNutritionFlag(raw: string): boolean {
const v = String(raw ?? "").trim().toLowerCase();
return v === "true" || v === "1" || v === "yes" || v === "on";
}
export function NutritionManualFieldControl({
spec,
value,
onChange,
inputClassName,
showLabel = true,
}: {
spec: NutritionManualFieldSpec;
value: string;
onChange: (next: string) => void;
inputClassName?: string;
showLabel?: boolean;
}) {
if (spec.inputType === "checkbox") {
return (
<div className="flex items-center gap-2 min-h-10">
<Checkbox
id={`nut-${spec.subKey}`}
checked={isTruthyNutritionFlag(value)}
onCheckedChange={(checked) => onChange(checked ? "true" : "false")}
/>
{showLabel ? (
<Label htmlFor={`nut-${spec.subKey}`} className="text-sm font-normal cursor-pointer">
{spec.columnLabel}
</Label>
) : null}
</div>
);
}
return (
<div className="space-y-1.5 w-full min-w-0">
{showLabel ? <Label className="block">{spec.columnLabel}</Label> : null}
<Input
className={inputClassName ?? "h-10 w-full min-w-0 box-border"}
value={value}
onChange={(e) => onChange(e.target.value)}
placeholder={`Enter ${spec.columnLabel}`}
/>
</div>
);
}