107 lines
3.9 KiB
TypeScript
107 lines
3.9 KiB
TypeScript
import { IconPlus, IconTrash } from "@tabler/icons-react";
|
|
|
|
/**
|
|
* แก้รายการรูปโรงหนัง (อาร์เรย์ `photos`) — ใส่เป็น URL หรือ path ในโปรเจกต์
|
|
* รูปแรกใช้เป็นภาพหลักของแกลเลอรี · ลากสลับลำดับไม่ได้ ใช้ปุ่ม ↑/↓ แทน
|
|
*/
|
|
type Props = {
|
|
photos: string[];
|
|
onChange: (next: string[]) => void;
|
|
inputClass: string;
|
|
labelClass: string;
|
|
};
|
|
|
|
const smallButtonClass =
|
|
"flex min-h-9 items-center gap-1 rounded-lg border border-[#303030] px-2 text-xs text-[#999] transition hover:border-[#A92626] hover:text-[#D47272]";
|
|
|
|
export function PhotosEditor({ photos, onChange, inputClass, labelClass }: Props) {
|
|
function updateAt(index: number, value: string) {
|
|
onChange(photos.map((item, i) => (i === index ? value : item)));
|
|
}
|
|
|
|
return (
|
|
<div className="flex flex-col gap-2">
|
|
<span className="text-xs text-[#999]">รูปภาพโรงหนัง</span>
|
|
|
|
{photos.length === 0 ? (
|
|
<span className="text-[11px] text-[#777]">
|
|
ยังไม่มีรูป — หน้าเว็บจะแสดงช่องแทน (ภาพที่ 1/2/3)
|
|
</span>
|
|
) : null}
|
|
|
|
{photos.map((photo, index) => (
|
|
<div
|
|
key={index}
|
|
className="grid items-center gap-2 rounded-lg border border-[#242424] p-2 sm:grid-cols-[64px_minmax(0,1fr)_auto_auto]"
|
|
>
|
|
<div className="size-16 overflow-hidden rounded-md border border-[#303030] bg-[#141414]">
|
|
{photo ? (
|
|
// eslint-disable-next-line jsx-a11y/alt-text
|
|
<img
|
|
src={photo}
|
|
alt=""
|
|
className="size-full object-cover"
|
|
loading="lazy"
|
|
/>
|
|
) : null}
|
|
</div>
|
|
<label className={labelClass}>
|
|
{index === 0 ? "รูปหลัก (URL หรือ /images/...)" : `รูปที่ ${index + 1}`}
|
|
<input
|
|
className={inputClass}
|
|
value={photo}
|
|
placeholder="/images/hero.png หรือ https://…/photo.jpg"
|
|
onChange={(event) => updateAt(index, event.target.value)}
|
|
/>
|
|
</label>
|
|
<div className="flex gap-1">
|
|
<button
|
|
type="button"
|
|
className={smallButtonClass}
|
|
aria-label={`เลื่อนรูปที่ ${index + 1} ขึ้น`}
|
|
disabled={index === 0}
|
|
onClick={() => {
|
|
const next = [...photos];
|
|
[next[index - 1], next[index]] = [next[index], next[index - 1]];
|
|
onChange(next);
|
|
}}
|
|
>
|
|
↑
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className={smallButtonClass}
|
|
aria-label={`เลื่อนรูปที่ ${index + 1} ลง`}
|
|
disabled={index === photos.length - 1}
|
|
onClick={() => {
|
|
const next = [...photos];
|
|
[next[index + 1], next[index]] = [next[index], next[index + 1]];
|
|
onChange(next);
|
|
}}
|
|
>
|
|
↓
|
|
</button>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
className={smallButtonClass}
|
|
aria-label={`ลบรูปที่ ${index + 1}`}
|
|
onClick={() => onChange(photos.filter((_, i) => i !== index))}
|
|
>
|
|
<IconTrash size={14} />
|
|
</button>
|
|
</div>
|
|
))}
|
|
|
|
<button
|
|
type="button"
|
|
className={`${smallButtonClass} w-fit`}
|
|
onClick={() => onChange([...photos, ""])}
|
|
>
|
|
<IconPlus size={14} />
|
|
เพิ่มรูป
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|