| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- import { Plus, X } from 'lucide-react';
- import { useState } from 'react';
- import InputError from '@/components/input-error';
- import { Button } from '@/components/ui/button';
- import { Input } from '@/components/ui/input';
- import { Label } from '@/components/ui/label';
-
- type Row = { title: string; body: string };
-
- const MAX_ROWS = 5;
-
- const fieldClass =
- 'w-full min-w-0 rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-xs outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 md:text-sm';
-
- /**
- * Extra sections that fold open on the product page, such as "How to use" or "Care".
- * The inputs carry the names the server reads: details[i][title] and details[i][body].
- */
- export default function DetailsEditor({
- initial,
- errors,
- }: {
- initial: Row[];
- errors: Record<string, string>;
- }) {
- const [rows, setRows] = useState<Row[]>(initial);
-
- function update(index: number, field: keyof Row, value: string) {
- setRows((current) =>
- current.map((row, i) =>
- i === index ? { ...row, [field]: value } : row,
- ),
- );
- }
-
- const listError =
- errors.details ??
- Object.entries(errors).find(([key]) => key.startsWith('details.'))?.[1];
-
- return (
- <div className="space-y-3" data-test="details-editor">
- <div>
- <p className="text-sm font-medium">More information</p>
- <p className="text-xs text-muted-foreground">
- Sections shoppers can open on the product page, such as "How
- to use" or "Care". Shipping is added by itself.
- </p>
- </div>
-
- {rows.map((row, index) => (
- <div key={index} className="grid gap-2 rounded-md border p-3">
- <div className="flex items-center gap-2">
- <Label
- htmlFor={`detail-title-${index}`}
- className="sr-only"
- >
- Section {index + 1} title
- </Label>
- <Input
- id={`detail-title-${index}`}
- name={`details[${index}][title]`}
- maxLength={60}
- value={row.title}
- onChange={(event) =>
- update(index, 'title', event.target.value)
- }
- placeholder="How to use"
- />
- <Button
- type="button"
- variant="ghost"
- size="icon"
- aria-label={`Remove section ${index + 1}`}
- onClick={() =>
- setRows((current) =>
- current.filter((_, i) => i !== index),
- )
- }
- >
- <X />
- </Button>
- </div>
- <Label htmlFor={`detail-body-${index}`} className="sr-only">
- Section {index + 1} text
- </Label>
- <textarea
- id={`detail-body-${index}`}
- name={`details[${index}][body]`}
- rows={3}
- maxLength={2000}
- value={row.body}
- onChange={(event) =>
- update(index, 'body', event.target.value)
- }
- className={fieldClass}
- placeholder="Apply a small amount to clean skin, morning and night."
- />
- </div>
- ))}
-
- {rows.length < MAX_ROWS && (
- <Button
- type="button"
- variant="outline"
- size="sm"
- onClick={() =>
- setRows((current) => [
- ...current,
- { title: '', body: '' },
- ])
- }
- >
- <Plus />
- Add a section
- </Button>
- )}
-
- <InputError message={listError} />
- </div>
- );
- }
|