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 122 123 124 125 | 113x 101x 101x 101x 26x 26x 101x | import React, { useEffect } from "react";
import { Form, Button } from "react-bootstrap";
import { useForm } from "react-hook-form";
function dateTimeLocalValue(dateTime) {
return dateTime
? dateTime.slice(0, 16)
: new Date().toISOString().slice(0, 16);
}
export default function ReviewForm({
initialItemName = "",
initialComments = "",
initialStars = 5,
initialDateServed,
submitAction,
submitButtonText = "Submit Review",
}) {
const testIdPrefix = "ReviewForm";
// Stryker disable all
const {
register,
formState: { errors },
handleSubmit,
reset,
getValues,
} = useForm({
defaultValues: {
reviewerComments: initialComments,
itemsStars: initialStars,
dateItemServed: dateTimeLocalValue(initialDateServed),
},
});
// Stryker restore all
// Keep the form in sync when the initial values change. If no new date is
// supplied, keep whatever date is currently in the form.
useEffect(() => {
// Stryker disable next-line StringLiteral: equivalent mutant, RHF keeps the current field value when reset receives undefined
const currentDate = getValues("dateItemServed");
reset({
reviewerComments: initialComments,
itemsStars: initialStars,
dateItemServed: initialDateServed
? dateTimeLocalValue(initialDateServed)
: currentDate,
});
}, [initialComments, initialStars, initialDateServed, reset, getValues]);
return (
<Form onSubmit={handleSubmit(submitAction)}>
<Form.Group className="mb-3">
<Form.Label htmlFor="review-item-name">Item Name</Form.Label>
<Form.Control
id="review-item-name"
data-testid={testIdPrefix + "-itemName"}
type="text"
value={initialItemName}
disabled
readOnly
/>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="review-comments">Comments</Form.Label>
<Form.Control
id="review-comments"
data-testid={testIdPrefix + "-reviewerComments"}
as="textarea"
rows={3}
isInvalid={Boolean(errors.reviewerComments)}
{...register("reviewerComments")}
/>
<Form.Control.Feedback type="invalid">
{errors.reviewerComments?.message}
</Form.Control.Feedback>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="review-stars">Stars (1 to 5)</Form.Label>
<Form.Control
id="review-stars"
data-testid={testIdPrefix + "-itemsStars"}
type="number"
step="1"
isInvalid={Boolean(errors.itemsStars)}
{...register("itemsStars", {
required: "Stars is required.",
valueAsNumber: true,
min: { value: 1, message: "Stars must be between 1 and 5." },
max: { value: 5, message: "Stars must be between 1 and 5." },
})}
/>
<Form.Control.Feedback type="invalid">
{errors.itemsStars?.message}
</Form.Control.Feedback>
</Form.Group>
<Form.Group className="mb-3">
<Form.Label htmlFor="review-date">
Date and Time Item was Served
</Form.Label>
<Form.Control
id="review-date"
data-testid={testIdPrefix + "-dateItemServed"}
type="datetime-local"
isInvalid={Boolean(errors.dateItemServed)}
{...register("dateItemServed", {
required: "Date and time served is required.",
})}
/>
<Form.Control.Feedback type="invalid">
{errors.dateItemServed?.message}
</Form.Control.Feedback>
</Form.Group>
<Button type="submit" data-testid={testIdPrefix + "-submit"}>
{submitButtonText}
</Button>
</Form>
);
}
|