Skip to content

Commit

Permalink
reorganisze table layout
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette committed Dec 5, 2024
1 parent 8801fe5 commit acbc03a
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 176 deletions.
93 changes: 93 additions & 0 deletions src/pages/nutrition/NutrimentCell.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import * as React from "react";
import { UNITS } from "./config";
import { isValidUnit } from "./utils";

interface NutrimentCellProps {
nutrimentId: string;
nutrimentKey: string;
tabIndex: number;
value?: string;
unit?: string;
setValues: (object) => void;
}

export const NutrimentCell = (props: NutrimentCellProps) => {
const { nutrimentId, nutrimentKey, tabIndex, value, unit, setValues } = props;

return (
<td
data-label-id={nutrimentId}
style={{
width: "fit-content",
paddingRight: 20,
}}
onFocus={() => {
const element = document.querySelector(
`tr[data-label-id=${nutrimentId}]`,
);
element.classList.add("focused");
}}
onBlur={() => {
const element = document.querySelector(
`tr[data-label-id=${nutrimentId}]`,
);
element.classList.remove("focused");
}}
>
<input
style={{ marginRight: 4, maxWidth: 100 }}
value={value ?? ""}
tabIndex={tabIndex}
onChange={(event) =>
setValues((p) => ({
...p,
[nutrimentKey]: {
...p[nutrimentKey],
value: event.target.value,
},
}))
}
/>

{isValidUnit(unit) ? (
<select
style={{ width: 55 }}
value={unit}
tabIndex={tabIndex}
onChange={(event) => {
setValues((p) => ({
...p,
[nutrimentKey]: {
...p[nutrimentKey],
unit: event.target.value,
},
}));
}}
>
{UNITS.map((unit) => (
<option key={unit} value={unit}>
{unit}
</option>
))}
</select>
) : (
<span style={{ display: "inline-block", width: 55 }}>{unit}</span>
)}
<button
tabIndex={-1}
onClick={() => {
setValues((p) => ({
...p,
[nutrimentKey]: {
...p[nutrimentKey],
unit: null,
value: null,
},
}));
}}
>
X
</button>
</td>
);
};
Loading

0 comments on commit acbc03a

Please sign in to comment.