@uniai-fe/uds-primitives 0.2.3 → 0.2.5
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.
- package/dist/styles.css +56 -0
- package/package.json +1 -1
- package/src/components/input/hooks/index.ts +3 -0
- package/src/components/input/hooks/useInputFile.ts +254 -0
- package/src/components/input/hooks/useInputFileContext.ts +244 -0
- package/src/components/input/hooks/useInputFileRHF.ts +105 -0
- package/src/components/input/index.scss +1 -0
- package/src/components/input/markup/file/DragAndDrop.tsx +59 -0
- package/src/components/input/markup/file/UploadButton.tsx +44 -0
- package/src/components/input/markup/file/UploadedChip.tsx +66 -0
- package/src/components/input/markup/file/index.tsx +25 -0
- package/src/components/input/markup/file/list/Body.tsx +55 -0
- package/src/components/input/markup/file/list/Container.tsx +97 -0
- package/src/components/input/markup/file/list/Header.tsx +86 -0
- package/src/components/input/markup/file/list/Item.tsx +35 -0
- package/src/components/input/markup/file/list/Provider.tsx +35 -0
- package/src/components/input/markup/file/list/Remove.tsx +54 -0
- package/src/components/input/markup/file/list/index.tsx +31 -0
- package/src/components/input/markup/foundation/SideSlot.tsx +0 -2
- package/src/components/input/markup/foundation/index.tsx +0 -2
- package/src/components/input/markup/index.tsx +2 -0
- package/src/components/input/markup/text/Search.tsx +0 -2
- package/src/components/input/styles/file.scss +60 -0
- package/src/components/input/types/date.ts +0 -80
- package/src/components/input/types/file.ts +531 -0
- package/src/components/input/types/index.ts +1 -0
- package/src/components/input/utils/file.ts +103 -0
- package/src/components/input/utils/index.tsx +1 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
InputFileEntry,
|
|
3
|
+
InputFileFormDataOptions,
|
|
4
|
+
InputFileMeta,
|
|
5
|
+
} from "../types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Input Utils; File Upload 확장자 추출 유틸
|
|
9
|
+
* @param {File} file 파일 객체
|
|
10
|
+
* @returns {string} 소문자 확장자
|
|
11
|
+
*/
|
|
12
|
+
export const resolveInputFileExtension = (file: File): string => {
|
|
13
|
+
const fileName = file.name ?? "";
|
|
14
|
+
const dotIndex = fileName.lastIndexOf(".");
|
|
15
|
+
if (dotIndex < 0 || dotIndex === fileName.length - 1) {
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
return fileName.slice(dotIndex + 1).toLowerCase();
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Input Utils; File Upload 제목 추출 유틸
|
|
23
|
+
* @param {File} file 파일 객체
|
|
24
|
+
* @returns {string} 확장자를 제외한 파일 제목
|
|
25
|
+
*/
|
|
26
|
+
export const resolveInputFileTitle = (file: File): string => {
|
|
27
|
+
const fileName = file.name ?? "";
|
|
28
|
+
const dotIndex = fileName.lastIndexOf(".");
|
|
29
|
+
if (dotIndex <= 0) {
|
|
30
|
+
return fileName;
|
|
31
|
+
}
|
|
32
|
+
return fileName.slice(0, dotIndex);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Input Utils; File Upload 포맷 추출 유틸
|
|
37
|
+
* @param {File} file 파일 객체
|
|
38
|
+
* @returns {string} MIME type 또는 확장자 기반 fallback
|
|
39
|
+
*/
|
|
40
|
+
export const resolveInputFileFormat = (file: File): string => {
|
|
41
|
+
if (file.type.length > 0) {
|
|
42
|
+
return file.type;
|
|
43
|
+
}
|
|
44
|
+
const extension = resolveInputFileExtension(file);
|
|
45
|
+
return extension.length > 0 ? extension : "unknown";
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Input Utils; File Upload 용량 포맷 유틸
|
|
50
|
+
* @param {number} size 파일 크기(Byte)
|
|
51
|
+
* @returns {string} 사람이 읽기 쉬운 용량 문자열
|
|
52
|
+
*/
|
|
53
|
+
export const formatInputFileSize = (size: number): string => {
|
|
54
|
+
if (size < 1024) {
|
|
55
|
+
return `${size} B`;
|
|
56
|
+
}
|
|
57
|
+
if (size < 1024 * 1024) {
|
|
58
|
+
return `${(size / 1024).toFixed(1)} KB`;
|
|
59
|
+
}
|
|
60
|
+
return `${(size / (1024 * 1024)).toFixed(1)} MB`;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Input Utils; File Upload 메타데이터 생성 유틸
|
|
65
|
+
* @param {InputFileEntry} entry 파일 엔트리
|
|
66
|
+
* @returns {InputFileMeta} 파일 메타데이터
|
|
67
|
+
*/
|
|
68
|
+
export const createInputFileMeta = (entry: InputFileEntry): InputFileMeta => {
|
|
69
|
+
const { file, id } = entry;
|
|
70
|
+
return {
|
|
71
|
+
id,
|
|
72
|
+
title: resolveInputFileTitle(file),
|
|
73
|
+
name: file.name,
|
|
74
|
+
size: file.size,
|
|
75
|
+
sizeLabel: formatInputFileSize(file.size),
|
|
76
|
+
format: resolveInputFileFormat(file),
|
|
77
|
+
extension: resolveInputFileExtension(file),
|
|
78
|
+
lastModified: file.lastModified,
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Input Utils; File Upload FormData 직렬화 유틸
|
|
84
|
+
* @param {File[]} files 원본 파일 목록
|
|
85
|
+
* @param {InputFileFormDataOptions} [options] FormData 직렬화 옵션
|
|
86
|
+
* @param {string} [options.fieldName="files"] FormData append 필드명
|
|
87
|
+
* @returns {FormData} 파일이 append된 FormData
|
|
88
|
+
* @example
|
|
89
|
+
* const formData = createInputFileFormData(fileContext.files, {
|
|
90
|
+
* fieldName: "attach_files",
|
|
91
|
+
* });
|
|
92
|
+
*/
|
|
93
|
+
export const createInputFileFormData = (
|
|
94
|
+
files: File[],
|
|
95
|
+
options?: InputFileFormDataOptions,
|
|
96
|
+
): FormData => {
|
|
97
|
+
const formData = new FormData();
|
|
98
|
+
const fieldName = options?.fieldName ?? "files";
|
|
99
|
+
files.forEach(file => {
|
|
100
|
+
formData.append(fieldName, file);
|
|
101
|
+
});
|
|
102
|
+
return formData;
|
|
103
|
+
};
|