AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

details-editor.tsx 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { Plus, X } from 'lucide-react';
  2. import { useState } from 'react';
  3. import InputError from '@/components/input-error';
  4. import { Button } from '@/components/ui/button';
  5. import { Input } from '@/components/ui/input';
  6. import { Label } from '@/components/ui/label';
  7. type Row = { title: string; body: string };
  8. const MAX_ROWS = 5;
  9. const fieldClass =
  10. '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';
  11. /**
  12. * Extra sections that fold open on the product page, such as "How to use" or "Care".
  13. * The inputs carry the names the server reads: details[i][title] and details[i][body].
  14. */
  15. export default function DetailsEditor({
  16. initial,
  17. errors,
  18. }: {
  19. initial: Row[];
  20. errors: Record<string, string>;
  21. }) {
  22. const [rows, setRows] = useState<Row[]>(initial);
  23. function update(index: number, field: keyof Row, value: string) {
  24. setRows((current) =>
  25. current.map((row, i) =>
  26. i === index ? { ...row, [field]: value } : row,
  27. ),
  28. );
  29. }
  30. const listError =
  31. errors.details ??
  32. Object.entries(errors).find(([key]) => key.startsWith('details.'))?.[1];
  33. return (
  34. <div className="space-y-3" data-test="details-editor">
  35. <div>
  36. <p className="text-sm font-medium">More information</p>
  37. <p className="text-xs text-muted-foreground">
  38. Sections shoppers can open on the product page, such as "How
  39. to use" or "Care". Shipping is added by itself.
  40. </p>
  41. </div>
  42. {rows.map((row, index) => (
  43. <div key={index} className="grid gap-2 rounded-md border p-3">
  44. <div className="flex items-center gap-2">
  45. <Label
  46. htmlFor={`detail-title-${index}`}
  47. className="sr-only"
  48. >
  49. Section {index + 1} title
  50. </Label>
  51. <Input
  52. id={`detail-title-${index}`}
  53. name={`details[${index}][title]`}
  54. maxLength={60}
  55. value={row.title}
  56. onChange={(event) =>
  57. update(index, 'title', event.target.value)
  58. }
  59. placeholder="How to use"
  60. />
  61. <Button
  62. type="button"
  63. variant="ghost"
  64. size="icon"
  65. aria-label={`Remove section ${index + 1}`}
  66. onClick={() =>
  67. setRows((current) =>
  68. current.filter((_, i) => i !== index),
  69. )
  70. }
  71. >
  72. <X />
  73. </Button>
  74. </div>
  75. <Label htmlFor={`detail-body-${index}`} className="sr-only">
  76. Section {index + 1} text
  77. </Label>
  78. <textarea
  79. id={`detail-body-${index}`}
  80. name={`details[${index}][body]`}
  81. rows={3}
  82. maxLength={2000}
  83. value={row.body}
  84. onChange={(event) =>
  85. update(index, 'body', event.target.value)
  86. }
  87. className={fieldClass}
  88. placeholder="Apply a small amount to clean skin, morning and night."
  89. />
  90. </div>
  91. ))}
  92. {rows.length < MAX_ROWS && (
  93. <Button
  94. type="button"
  95. variant="outline"
  96. size="sm"
  97. onClick={() =>
  98. setRows((current) => [
  99. ...current,
  100. { title: '', body: '' },
  101. ])
  102. }
  103. >
  104. <Plus />
  105. Add a section
  106. </Button>
  107. )}
  108. <InputError message={listError} />
  109. </div>
  110. );
  111. }