Media: eis ML-17 (kiezen of uploaden) in requirements, ontwerp en aannamelijst; pure ImagePicker-component
Continuous Integration / config-sls (pull_request) Successful in 9s
Continuous Integration / config-asf (pull_request) Successful in 5s
Continuous Integration / changes (pull_request) Canceled after 2m2s
Continuous Integration / backend-build (pull_request) Canceled after 0s
Continuous Integration / compile-sites (pull_request) Canceled after 0s
Continuous Integration / backend-test (pull_request) Canceled after 0s
Continuous Integration / vulnerability-scan (pull_request) Canceled after 0s
Continuous Integration / frontend-prepare (pull_request) Canceled after 0s
Continuous Integration / frontend-build (pull_request) Canceled after 0s
Continuous Integration / frontend-test (pull_request) Canceled after 0s
Continuous Integration / frontend-lint (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Continuous Integration / config-sls (pull_request) Successful in 9s
Continuous Integration / config-asf (pull_request) Successful in 5s
Continuous Integration / changes (pull_request) Canceled after 2m2s
Continuous Integration / backend-build (pull_request) Canceled after 0s
Continuous Integration / compile-sites (pull_request) Canceled after 0s
Continuous Integration / backend-test (pull_request) Canceled after 0s
Continuous Integration / vulnerability-scan (pull_request) Canceled after 0s
Continuous Integration / frontend-prepare (pull_request) Canceled after 0s
Continuous Integration / frontend-build (pull_request) Canceled after 0s
Continuous Integration / frontend-test (pull_request) Canceled after 0s
Continuous Integration / frontend-lint (pull_request) Canceled after 0s
Continuous Integration / publish-sls-test (pull_request) Canceled after 0s
Continuous Integration / publish-sls-production (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-test (pull_request) Canceled after 0s
Continuous Integration / deploy-sls-production (pull_request) Canceled after 0s
Continuous Integration / publish-asf-test (pull_request) Canceled after 0s
Continuous Integration / deploy-asf-test (pull_request) Canceled after 0s
Continuous Integration / smoke-sls-test (pull_request) Canceled after 0s
Continuous Integration / smoke-asf-test (pull_request) Canceled after 0s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
import { useId, useRef, useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { ImagePlus } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { validateUpload } from '../lib/validateUpload';
|
||||
import type { PhotoItem } from './PhotoSelectionList';
|
||||
|
||||
interface ImagePickerProps {
|
||||
/** The photos of the library to choose from, newest first. */
|
||||
items: readonly PhotoItem[];
|
||||
/** The id of the chosen photo, or null. */
|
||||
value: string | null;
|
||||
onChange: (id: string) => void;
|
||||
/**
|
||||
* Uploads one file and answers with the photo as the library lists it. The module decides how (the hero uses its own
|
||||
* upload and background processing); the picker only shows the state and chooses the result. Without it there is no
|
||||
* upload button.
|
||||
*/
|
||||
onUpload?: (file: File) => Promise<PhotoItem>;
|
||||
/** The largest file the host accepts, in bytes. */
|
||||
maxUploadBytes: number;
|
||||
/** A short line about formats and limits, shown under the buttons. */
|
||||
limitsText?: string;
|
||||
/** Accessible name of the group (what the image is for, for example "Image of slide 2"). */
|
||||
label: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* "Choose an image or upload one": the photos of the library as a group of radio buttons (arrow keys move and choose)
|
||||
* and an upload button that adds the new photo to the library and chooses it at once. One component for every module that
|
||||
* needs an image. It holds no data of its own and calls nothing by itself: the module passes the list and the upload.
|
||||
*/
|
||||
export function ImagePicker({ items, value, onChange, onUpload, maxUploadBytes, limitsText, label }: ImagePickerProps) {
|
||||
const { t } = useTranslation();
|
||||
const groupName = useId();
|
||||
const fileInput = useRef<HTMLInputElement>(null);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [problem, setProblem] = useState<string | null>(null);
|
||||
|
||||
const upload = async (file: File | undefined) => {
|
||||
if (!file || !onUpload || uploading) return;
|
||||
const refused = validateUpload(file, maxUploadBytes);
|
||||
if (refused) {
|
||||
setProblem(refused === 'tooLarge' ? t('media.picker.tooLarge', { max: Math.round(maxUploadBytes / (1024 * 1024)) }) : t('media.picker.notAnImage'));
|
||||
return;
|
||||
}
|
||||
setProblem(null);
|
||||
setUploading(true);
|
||||
try {
|
||||
const photo = await onUpload(file);
|
||||
onChange(photo.id);
|
||||
} catch {
|
||||
setProblem(t('media.picker.failed'));
|
||||
} finally {
|
||||
setUploading(false);
|
||||
if (fileInput.current) fileInput.current.value = '';
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-3" data-testid="image-picker">
|
||||
{onUpload && (
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<input
|
||||
ref={fileInput}
|
||||
type="file"
|
||||
accept="image/*"
|
||||
className="sr-only"
|
||||
aria-label={t('media.picker.upload')}
|
||||
data-testid="picker-file"
|
||||
onChange={(event) => void upload(event.target.files?.[0])}
|
||||
/>
|
||||
<Button type="button" variant="outline" disabled={uploading} onClick={() => fileInput.current?.click()} data-testid="picker-upload-button">
|
||||
<ImagePlus className="mr-2 size-4" aria-hidden="true" />
|
||||
{uploading ? t('media.picker.uploading') : t('media.picker.upload')}
|
||||
</Button>
|
||||
{limitsText && <p className="text-xs text-muted-foreground">{limitsText}</p>}
|
||||
</div>
|
||||
)}
|
||||
{problem && (
|
||||
<p role="alert" className="text-sm text-destructive" data-testid="picker-problem">
|
||||
{problem}
|
||||
</p>
|
||||
)}
|
||||
{items.length === 0 ? (
|
||||
<p className="rounded-md border border-dashed p-4 text-sm text-muted-foreground" data-testid="picker-empty">
|
||||
{t('media.picker.empty')}
|
||||
</p>
|
||||
) : (
|
||||
<div role="radiogroup" aria-label={label} className="grid grid-cols-2 gap-3 sm:grid-cols-4" data-testid="picker-grid">
|
||||
{items.map((item) => (
|
||||
<label key={item.id} className="relative block cursor-pointer" data-testid={`picker-item-${item.id}`}>
|
||||
<input
|
||||
type="radio"
|
||||
name={groupName}
|
||||
value={item.id}
|
||||
checked={value === item.id}
|
||||
onChange={() => onChange(item.id)}
|
||||
aria-label={item.name}
|
||||
className="peer sr-only"
|
||||
/>
|
||||
<span
|
||||
className={cn(
|
||||
'block overflow-hidden rounded-md border-2 border-transparent bg-muted transition-colors',
|
||||
'peer-checked:border-primary peer-focus-visible:ring-2 peer-focus-visible:ring-ring peer-focus-visible:ring-offset-2 ring-offset-background',
|
||||
'hover:border-muted-foreground forced-colors:peer-checked:border-[Highlight]',
|
||||
)}
|
||||
>
|
||||
{item.thumbnailUrl ? (
|
||||
<img src={item.thumbnailUrl} alt="" loading="lazy" className="aspect-[3/2] w-full object-cover" />
|
||||
) : (
|
||||
<span className="flex aspect-[3/2] items-center justify-center text-xs text-muted-foreground">{t('media.list.noThumbnail')}</span>
|
||||
)}
|
||||
</span>
|
||||
<span className="mt-1 block truncate text-xs">{item.name}</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user