recipe_app/frontend/src/components/AddBulkSteps.tsx

65 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-07-10 16:34:53 -07:00
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>('');
useEffect(() => {
const textRepresentation = steps.map(step =>
`${step.instruction}`
2025-07-10 16:34:53 -07:00
).join('\n');
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() !== '');
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') {
parseAndUpdate(textValue);
}
};
const handleBlur = () => {
parseAndUpdate(textValue);
};
return (
<div>
2025-08-05 15:09:52 -07:00
<h3 className="text-xl font-bold">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}
2025-08-05 15:09:52 -07:00
placeholder="Enter steps separated by new line"
2025-07-11 17:06:41 -07:00
className="mb-4 p-2 border border-gray-300 rounded w-full"
2025-07-10 16:34:53 -07:00
/>
</div>
);
};
export default AddBulkSteps;