Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | 25x 25x 25x 5x 1x | // src/components/ReviewForm.jsx
import React from "react";
import { Button, Form } from "react-bootstrap";
import { useForm } from "react-hook-form";
import { useNavigate } from "react-router-dom";
function ReviewForm({
initialContents,
submitAction,
buttonLabel = "Create",
itemName = "", // New prop for the item name
}) {
const {
register,
formState: { errors },
handleSubmit,
} = useForm({ defaultValues: initialContents || {} });
const navigate = useNavigate();
return (
<Form onSubmit={handleSubmit(submitAction)}>
{initialContents?.id && (
<Form.Group className="mb-3">
<Form.Label htmlFor="id">Id</Form.Label>
<Form.Control
id="id"
type="text"
{...register("id")}
value={initialContents.id}
disabled
/>
</Form.Group>
)}
{/* Disabled item name field */}
<Form.Group className="mb-3">
<Form.Label htmlFor="itemName">Item Being Reviewed</Form.Label>
<Form.Control
id="itemName"
type="text"
value={itemName}
disabled
placeholder="Item name will be displayed here"
/>
</Form.Group>
{/* Stars rating */}
<Form.Group className="mb-3">
<Form.Label htmlFor="stars">Stars</Form.Label>
<Form.Control
id="stars"
type="number"
step="1"
min="1"
max="5"
placeholder="Please enter an integer from 1 to 5"
title="Enter a whole number between 1 and 5"
isInvalid={Boolean(errors.stars)}
{...register("stars", {
required: "Stars is required.",
min: { value: 1, message: "Minimum 1 star" },
max: { value: 5, message: "Maximum 5 stars" },
validate: (v) =>
Number.isInteger(Number(v)) || "Please enter a whole number.",
})}
/>
<Form.Control.Feedback type="invalid">
{errors.stars?.message}
</Form.Control.Feedback>
</Form.Group>
{/* Comments field */}
<Form.Group className="mb-3">
<Form.Label htmlFor="comments">
Comments <span className="text-muted">(optional)</span>
</Form.Label>
<Form.Control
id="comments"
as="textarea"
rows={3}
isInvalid={Boolean(errors.comments)}
{...register("comments", {
maxLength: {
value: 255,
message: "Max length is 255 characters",
},
})}
placeholder="Write your thoughts... (optional)"
/>
<Form.Control.Feedback type="invalid">
{errors.comments?.message}
</Form.Control.Feedback>
</Form.Group>
{/* Date-time picker for when item was served */}
<Form.Group className="mb-3">
<Form.Label htmlFor="dateServed">Date & Time Served</Form.Label>
<Form.Control
id="dateServed"
type="datetime-local"
isInvalid={Boolean(errors.dateServed)}
{...register("dateServed", {
required: "Date and time served is required.",
})}
/>
<Form.Control.Feedback type="invalid">
{errors.dateServed?.message}
</Form.Control.Feedback>
</Form.Group>
<Button type="submit">{buttonLabel}</Button>
<Button variant="secondary" className="ms-2" onClick={() => navigate(-1)}>
Cancel
</Button>
</Form>
);
}
export default ReviewForm;
|