@betterinternship/components 3.2.9 → 3.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -67,9 +67,16 @@ const FileUpload = React.forwardRef(({ label, name, variant = "compact", accept,
|
|
|
67
67
|
if (disabled)
|
|
68
68
|
return;
|
|
69
69
|
selectFiles(Array.from(event.dataTransfer.files).slice(0, multiple ? undefined : 1));
|
|
70
|
-
}, className: cn(
|
|
70
|
+
}, className: cn(
|
|
71
|
+
// min-w-0: as a grid item the button would otherwise refuse to
|
|
72
|
+
// shrink below its text, overflowing narrow containers (phones).
|
|
73
|
+
"flex w-full min-w-0 cursor-pointer items-center gap-2 border border-input text-left text-sm text-muted-foreground transition-colors hover:border-input-hover hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none disabled:cursor-not-allowed disabled:opacity-60", variant === "dropzone"
|
|
71
74
|
? "min-h-36 flex-col justify-center rounded-card border-solid border-border px-6 py-6 text-center"
|
|
72
|
-
: "min-h-11 rounded-control-lg px-4 py-2", dragging && "border-primary bg-primary-tint", error && "border-destructive"), children: [jsx(Upload, { className: "size-5 shrink-0 text-primary" }), jsx("span", { className: cn("min-w-0
|
|
75
|
+
: "min-h-11 rounded-control-lg px-4 py-2", dragging && "border-primary bg-primary-tint", error && "border-destructive"), children: [jsx(Upload, { className: "size-5 shrink-0 text-primary" }), jsx("span", { className: cn("min-w-0",
|
|
76
|
+
// The dropzone has room to wrap; the compact field truncates.
|
|
77
|
+
variant === "dropzone"
|
|
78
|
+
? "max-w-full text-balance [overflow-wrap:anywhere]"
|
|
79
|
+
: "truncate", fileName && "text-foreground"), children: fileName ?? placeholder ?? "Drop a file here or click to upload" })] }), jsx("input", { ref: (node) => {
|
|
73
80
|
inputRef.current = node;
|
|
74
81
|
if (typeof ref === "function")
|
|
75
82
|
ref(node);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-upload.js","sources":["../../src/components/file-upload.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport { Upload } from \"lucide-react\";\nimport { cn } from \"@/utils\";\nimport { FieldLabel } from \"./label\";\n\nexport type FileUploadVariant = \"compact\" | \"dropzone\";\nexport type FileRejectReason = \"type\" | \"size\";\nexport type FileUploadProps = {\n label: string;\n name: string;\n variant?: FileUploadVariant;\n /** Same format as the input's `accept`: extensions (\".pdf\") and/or MIME types (\"image/*\"). Also checked on drop. */\n accept?: string;\n /** Files larger than this are rejected through `onReject` instead of being selected. */\n maxSizeMB?: number;\n multiple?: boolean;\n required?: boolean;\n placeholder?: string;\n disabled?: boolean;\n className?: string;\n /**\n * The selected file, when the parent owns it. Pass `null` to clear the field.\n * Leave undefined to let the field remember the last pick itself.\n */\n value?: File | null;\n /** Shown under the field and marks it invalid. */\n error?: string | null;\n onFileSelect?: (file: File | null) => void;\n onFilesSelect?: (files: File[]) => void;\n /** Called with each file that fails `accept` or `maxSizeMB`. */\n onReject?: (file: File, reason: FileRejectReason) => void;\n};\n\n/** True when the file matches one of the `accept` entries (or there are none). */\nfunction matchesAccept(file: File, accept?: string) {\n if (!accept) return true;\n const name = file.name.toLowerCase();\n const type = file.type.toLowerCase();\n return accept\n .split(\",\")\n .map((entry) => entry.trim().toLowerCase())\n .filter(Boolean)\n .some((entry) =>\n entry.startsWith(\".\")\n ? name.endsWith(entry)\n : entry.endsWith(\"/*\")\n ? type.startsWith(entry.slice(0, -1))\n : type === entry,\n );\n}\n\nexport const FileUpload = React.forwardRef<HTMLInputElement, FileUploadProps>(\n (\n {\n label,\n name,\n variant = \"compact\",\n accept,\n maxSizeMB,\n multiple,\n required,\n placeholder,\n disabled,\n className,\n value,\n error,\n onFileSelect,\n onFilesSelect,\n onReject,\n },\n ref,\n ) => {\n const inputRef = React.useRef<HTMLInputElement>(null);\n const [pickedName, setPickedName] = React.useState<string | null>(null);\n const [dragging, setDragging] = React.useState(false);\n const errorId = `${name}-error`;\n const controlled = value !== undefined;\n const fileName = controlled ? (value?.name ?? null) : pickedName;\n\n const selectFiles = (files: File[]) => {\n const accepted = files.filter((file) => {\n if (!matchesAccept(file, accept)) {\n onReject?.(file, \"type\");\n return false;\n }\n if (maxSizeMB !== undefined && file.size > maxSizeMB * 1024 * 1024) {\n onReject?.(file, \"size\");\n return false;\n }\n return true;\n });\n // Every file was rejected: keep the current selection as it was.\n if (files.length > 0 && accepted.length === 0) return;\n\n const file = accepted[0] ?? null;\n setPickedName(\n accepted.length > 1\n ? `${accepted.length} files selected`\n : (file?.name ?? null),\n );\n onFileSelect?.(file);\n onFilesSelect?.(accepted);\n };\n const openPicker = () => {\n if (!disabled) inputRef.current?.click();\n };\n\n return (\n <div className={cn(\"grid gap-field-label-gap\", className)}>\n <FieldLabel htmlFor={name}>\n {label}\n {required && <span className=\"text-destructive\"> *</span>}\n </FieldLabel>\n <button\n type=\"button\"\n disabled={disabled}\n onClick={openPicker}\n aria-invalid={error ? true : undefined}\n aria-describedby={error ? errorId : undefined}\n onDragEnter={(event) => {\n event.preventDefault();\n setDragging(true);\n }}\n onDragOver={(event) => {\n event.preventDefault();\n if (!disabled) setDragging(true);\n }}\n onDragLeave={() => setDragging(false)}\n onDrop={(event) => {\n event.preventDefault();\n setDragging(false);\n if (disabled) return;\n selectFiles(\n Array.from(event.dataTransfer.files).slice(\n 0,\n multiple ? undefined : 1,\n ),\n );\n }}\n className={cn(\n \"flex w-full cursor-pointer items-center gap-2 border border-input text-left text-sm text-muted-foreground transition-colors hover:border-input-hover hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none disabled:cursor-not-allowed disabled:opacity-60\",\n variant === \"dropzone\"\n ? \"min-h-36 flex-col justify-center rounded-card border-solid border-border px-6 py-6 text-center\"\n : \"min-h-11 rounded-control-lg px-4 py-2\",\n dragging && \"border-primary bg-primary-tint\",\n error && \"border-destructive\",\n )}\n >\n <Upload className=\"size-5 shrink-0 text-primary\" />\n <span
|
|
1
|
+
{"version":3,"file":"file-upload.js","sources":["../../src/components/file-upload.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport { Upload } from \"lucide-react\";\nimport { cn } from \"@/utils\";\nimport { FieldLabel } from \"./label\";\n\nexport type FileUploadVariant = \"compact\" | \"dropzone\";\nexport type FileRejectReason = \"type\" | \"size\";\nexport type FileUploadProps = {\n label: string;\n name: string;\n variant?: FileUploadVariant;\n /** Same format as the input's `accept`: extensions (\".pdf\") and/or MIME types (\"image/*\"). Also checked on drop. */\n accept?: string;\n /** Files larger than this are rejected through `onReject` instead of being selected. */\n maxSizeMB?: number;\n multiple?: boolean;\n required?: boolean;\n placeholder?: string;\n disabled?: boolean;\n className?: string;\n /**\n * The selected file, when the parent owns it. Pass `null` to clear the field.\n * Leave undefined to let the field remember the last pick itself.\n */\n value?: File | null;\n /** Shown under the field and marks it invalid. */\n error?: string | null;\n onFileSelect?: (file: File | null) => void;\n onFilesSelect?: (files: File[]) => void;\n /** Called with each file that fails `accept` or `maxSizeMB`. */\n onReject?: (file: File, reason: FileRejectReason) => void;\n};\n\n/** True when the file matches one of the `accept` entries (or there are none). */\nfunction matchesAccept(file: File, accept?: string) {\n if (!accept) return true;\n const name = file.name.toLowerCase();\n const type = file.type.toLowerCase();\n return accept\n .split(\",\")\n .map((entry) => entry.trim().toLowerCase())\n .filter(Boolean)\n .some((entry) =>\n entry.startsWith(\".\")\n ? name.endsWith(entry)\n : entry.endsWith(\"/*\")\n ? type.startsWith(entry.slice(0, -1))\n : type === entry,\n );\n}\n\nexport const FileUpload = React.forwardRef<HTMLInputElement, FileUploadProps>(\n (\n {\n label,\n name,\n variant = \"compact\",\n accept,\n maxSizeMB,\n multiple,\n required,\n placeholder,\n disabled,\n className,\n value,\n error,\n onFileSelect,\n onFilesSelect,\n onReject,\n },\n ref,\n ) => {\n const inputRef = React.useRef<HTMLInputElement>(null);\n const [pickedName, setPickedName] = React.useState<string | null>(null);\n const [dragging, setDragging] = React.useState(false);\n const errorId = `${name}-error`;\n const controlled = value !== undefined;\n const fileName = controlled ? (value?.name ?? null) : pickedName;\n\n const selectFiles = (files: File[]) => {\n const accepted = files.filter((file) => {\n if (!matchesAccept(file, accept)) {\n onReject?.(file, \"type\");\n return false;\n }\n if (maxSizeMB !== undefined && file.size > maxSizeMB * 1024 * 1024) {\n onReject?.(file, \"size\");\n return false;\n }\n return true;\n });\n // Every file was rejected: keep the current selection as it was.\n if (files.length > 0 && accepted.length === 0) return;\n\n const file = accepted[0] ?? null;\n setPickedName(\n accepted.length > 1\n ? `${accepted.length} files selected`\n : (file?.name ?? null),\n );\n onFileSelect?.(file);\n onFilesSelect?.(accepted);\n };\n const openPicker = () => {\n if (!disabled) inputRef.current?.click();\n };\n\n return (\n <div className={cn(\"grid gap-field-label-gap\", className)}>\n <FieldLabel htmlFor={name}>\n {label}\n {required && <span className=\"text-destructive\"> *</span>}\n </FieldLabel>\n <button\n type=\"button\"\n disabled={disabled}\n onClick={openPicker}\n aria-invalid={error ? true : undefined}\n aria-describedby={error ? errorId : undefined}\n onDragEnter={(event) => {\n event.preventDefault();\n setDragging(true);\n }}\n onDragOver={(event) => {\n event.preventDefault();\n if (!disabled) setDragging(true);\n }}\n onDragLeave={() => setDragging(false)}\n onDrop={(event) => {\n event.preventDefault();\n setDragging(false);\n if (disabled) return;\n selectFiles(\n Array.from(event.dataTransfer.files).slice(\n 0,\n multiple ? undefined : 1,\n ),\n );\n }}\n className={cn(\n // min-w-0: as a grid item the button would otherwise refuse to\n // shrink below its text, overflowing narrow containers (phones).\n \"flex w-full min-w-0 cursor-pointer items-center gap-2 border border-input text-left text-sm text-muted-foreground transition-colors hover:border-input-hover hover:bg-accent focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none disabled:cursor-not-allowed disabled:opacity-60\",\n variant === \"dropzone\"\n ? \"min-h-36 flex-col justify-center rounded-card border-solid border-border px-6 py-6 text-center\"\n : \"min-h-11 rounded-control-lg px-4 py-2\",\n dragging && \"border-primary bg-primary-tint\",\n error && \"border-destructive\",\n )}\n >\n <Upload className=\"size-5 shrink-0 text-primary\" />\n <span\n className={cn(\n \"min-w-0\",\n // The dropzone has room to wrap; the compact field truncates.\n variant === \"dropzone\"\n ? \"max-w-full text-balance [overflow-wrap:anywhere]\"\n : \"truncate\",\n fileName && \"text-foreground\",\n )}\n >\n {fileName ?? placeholder ?? \"Drop a file here or click to upload\"}\n </span>\n </button>\n <input\n ref={(node) => {\n inputRef.current = node;\n if (typeof ref === \"function\") ref(node);\n else if (ref) ref.current = node;\n }}\n id={name}\n type=\"file\"\n name={name}\n accept={accept}\n multiple={multiple}\n required={required}\n disabled={disabled}\n onChange={(event) => {\n selectFiles(Array.from(event.target.files ?? []));\n // Let the same file be picked again, e.g. after a rejection.\n event.target.value = \"\";\n }}\n className=\"sr-only\"\n />\n {error && (\n <p id={errorId} role=\"alert\" className=\"m-0 text-xs text-destructive\">\n {error}\n </p>\n )}\n </div>\n );\n },\n);\nFileUpload.displayName = \"FileUpload\";\n"],"names":[],"mappings":";;;;;;;AAmCA;AACA;AACE;AAAa;;;AAGb;;AAEG;;;AAIG;AACA;AACE;AACA;AAEV;;;AAwBI;AACA;AACA;AACA;AACA;AAEA;;;AAGM;AACA;;AAEF;AACE;AACA;;AAEF;AACF;;;;;AAKA;AAEI;;AAGJ;AACA;AACF;;AAEE;AAAe;AACjB;AAEA;;;AAeM;;AAGE;;AACF;;;AAKE;;;;;;;AAaE;;;AAWA;AACE;;AAUJ;;;AAEK;AAAS;AAChB;AASE;;AAEA;;AAWV;AAEF;;"}
|