recipe_app/frontend/src/components/AddBulkSteps.tsx

88 lines
2.1 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from "react";
interface Step {
step_number: number;
instruction: string;
}
2025-07-10 16:34:53 -07:00
interface AddBulkStepsProps {
steps: Step[];
onChange?: (steps: Step[]) => void;
}
const AddBulkSteps: React.FC<AddBulkStepsProps> = ({ steps, onChange }) => {
const [textValue, setTextValue] = useState<string>("");
2025-07-10 16:34:53 -07:00
useEffect(() => {
const textRepresentation = steps
.map((step) => `${step.instruction}`)
.join("\n");
2025-07-10 16:34:53 -07:00
setTextValue(textRepresentation);
}, [steps]);
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setTextValue(e.target.value);
};
const parseAndUpdate = (value: string) => {
const lines = value.split("\n").filter((line) => line.trim() !== "");
2025-07-10 16:34:53 -07:00
const parsedSteps: Step[] = lines.map((line, idx) => {
return { step_number: idx + 1, instruction: line };
});
2025-07-10 16:34:53 -07:00
if (onChange) onChange(parsedSteps);
};
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter") {
2025-07-10 16:34:53 -07:00
parseAndUpdate(textValue);
}
};
const handleBlur = () => {
parseAndUpdate(textValue);
document.body.style.overflow = "";
};
const manageOverflow = () => {
const lineCount = textValue
.split("\n")
.filter((line) => line.trim() !== "").length;
if (lineCount > 8) {
document.body.style.overflow = "hidden";
} else {
document.body.style.overflow = "";
}
};
const handleFocus = () => {
manageOverflow();
};
const handleTouchEnd = () => {
manageOverflow();
2025-07-10 16:34:53 -07:00
};
return (
<div>
<h3 className="text-xl font-bold text-[var(--color-secondaryTextDark)]">
Steps:
</h3>
2025-07-10 16:34:53 -07:00
<textarea
2025-07-11 17:06:41 -07:00
rows={8}
2025-07-10 16:34:53 -07:00
value={textValue}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
onBlur={handleBlur}
onFocus={handleFocus}
onTouchEnd={handleTouchEnd}
2025-08-05 15:09:52 -07:00
placeholder="Enter steps separated by new line"
2025-08-20 10:06:23 -07:00
className="mb-4 p-2 border border-[var(--color-primaryBorder)] rounded w-full"
2025-07-10 16:34:53 -07:00
/>
</div>
);
};
export default AddBulkSteps;