Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions packages/pkg.fileuploader/FileUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatBytesSize, plural } from '@xipkg/utils';
import { cva } from 'class-variance-authority';
import { ChangeEvent, DragEvent, useId, useRef, useState } from 'react';
import { FileUploaderProps, DefaultInputPropsT } from './types';
import { stopDefaultEvents, validateSize } from './utils';
import { stopDefaultEvents } from './utils';

const containerStyles = cva(
'flex group items-center rounded-lg border border-dashed border-gray-40 bg-gray-0 dark:border-gray-60 dark:bg-gray-100 transition-[outline_shadow] px-2 max-w-[500px] gap-3 focus-within:border-solid focus-within:border-gray-80 dark:focus-within:border-gray-40',
Expand Down Expand Up @@ -59,8 +59,7 @@ export const FileUploader = ({
disabled,
isWarning,
onChange,
onError,
enableErrorHandling = true,
onFileError,
limit = 3,
bytesSizeLimit = DEFAULT_SIZE_LIMIT,
children,
Expand All @@ -80,15 +79,13 @@ export const FileUploader = ({
const formatedSizeLimit = formatBytesSize(bytesSizeLimit);

const handleError = (fileList: File[]): boolean => {
if (!enableErrorHandling || !onError) {
return false;
}

if (fileList.length > limit) {
onError(`Можно отправить не более ${limit} ${plural(
pluralFiles,
limit,
)} общим объёмом до ${formatedSizeLimit}`);
onFileError(
`Можно отправить не более ${limit} ${plural(
pluralFiles,
limit,
)} общим объёмом до ${formatedSizeLimit}`,
);
return true;
}

Expand Down Expand Up @@ -133,15 +130,15 @@ export const FileUploader = ({
})
.join(', ');

onError(`Неподдерживаемый формат файла. Разрешены: ${displayTypes}`);
onFileError(`Неподдерживаемый формат файла. Разрешены: ${displayTypes}`);
return true;
}
}

if (validateBeforeUpload) {
const validationError = validateBeforeUpload(fileList);
if (validationError) {
onError(validationError);
onFileError(validationError.titleError, validationError.subtitleError);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pkg.fileuploader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xipkg/fileuploader",
"version": "2.0.13",
"version": "2.0.14",
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.mts",
Expand Down
7 changes: 4 additions & 3 deletions packages/pkg.fileuploader/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ export type DefaultInputPropsT = Omit<
'onChange' | 'size'
>;

type ValidationError = { titleError: string; subtitleError: string };

export type FileUploaderProps = {
size?: SizeType;
limit?: number;
onError?: (error: string) => void;
enableErrorHandling?: boolean;
onFileError: (titleError: string, subtitleError?: string) => void;
isWarning?: boolean;
descriptionText?: string;
onChange: (files: File[]) => void;
validateBeforeUpload?: (files: File[]) => string | undefined;
validateBeforeUpload?: (files: File[]) => ValidationError;
bytesSizeLimit?: number;
children?: React.ReactNode;
fileTypesHint?: string[];
Expand Down