zasuiv0.1
GitHub
Menú

Escaneo y rutas

Photo Capture

Tarjeta para tomar una foto con la cámara del móvil (o elegir un archivo), con el número de fotos y las ya hechas.

Dependencias: lucide-react

Foto de entrega

Notas

Solo pinta y entrega el archivo en `onCapture`: subirlo y mostrar las miniaturas (en `children`) lo hace quien la usa. En Distribuidor, la foto de entrega y la del ID del cliente.

Instalación

Con la CLI de zasui (ver Introducción): instala el componente con sus dependencias y queda listo para usar.

pnpm dlx @medialityc/zasui add photo-capture
Ver el código
'use client';

import { cn } from '@/lib/utils';
import { Camera, Loader2 } from 'lucide-react';
import * as React from 'react';

export interface PhotoCaptureProps {
	label: React.ReactNode;
	/** Fotos ya hechas (miniaturas, enlaces «Ver foto»…): van entre el título y el botón. */
	children?: React.ReactNode;
	/** Cuántas hay; con más de 0 sale «N foto(s)» junto al título. */
	count?: number;
	/** Recibe la foto elegida o tomada con la cámara. Sin él la tarjeta es de solo lectura: muestra las fotos y no ofrece tomar otra. */
	onCapture?: (file: File) => void;
	uploading?: boolean;
	disabled?: boolean;
	className?: string;
}

/** Tarjeta para tomar una foto con la cámara del móvil (o elegir un archivo) y ver las ya hechas. */
function PhotoCapture({ label, children, count = 0, onCapture, uploading, disabled, className }: PhotoCaptureProps) {
	const inputRef = React.useRef<HTMLInputElement>(null);
	const off = disabled || uploading;
	return (
		<div data-slot='photo-capture' className={cn('rounded-lg border border-gray-200 p-3 dark:border-gray-700', className)}>
			<div className='mb-2 flex items-center justify-between gap-2'>
				<span className='text-sm font-semibold'>{label}</span>
				{count > 0 && (
					<span className='text-xs text-emerald-600 dark:text-emerald-400'>
						{count} foto{count > 1 ? 's' : ''}
					</span>
				)}
			</div>
			{children}
			{onCapture && (
				<>
					<input
						ref={inputRef}
						type='file'
						accept='image/*'
						capture='environment'
						className='hidden'
						disabled={off}
						onChange={(e) => {
							const file = e.target.files?.[0];
							e.target.value = '';
							if (file) onCapture(file);
						}}
					/>
					<button
						type='button'
						onClick={() => inputRef.current?.click()}
						disabled={off}
						className='border-primary text-primary hover:bg-primary/5 inline-flex w-full items-center justify-center gap-2 rounded-lg border py-2 text-sm font-medium transition disabled:cursor-not-allowed disabled:opacity-50'
					>
						{uploading ? <Loader2 className='size-4 animate-spin' /> : <Camera className='size-4' />}
						{uploading ? 'Subiendo...' : count > 0 ? 'Añadir otra foto' : 'Tomar foto'}
					</button>
				</>
			)}
		</div>
	);
}

export { PhotoCapture };