@ncds/ui-admin 1.8.16 → 1.8.17
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/cjs/src/components/forms-and-input/file-input/FileInput.js +25 -7
- package/dist/cjs/src/components/forms-and-input/file-input/__tests__/FileInput.test.js +168 -0
- package/dist/cjs/src/components/forms-and-input/image-file-input/ImageFileInput.js +76 -19
- package/dist/cjs/src/components/forms-and-input/image-file-input/__tests__/ImageFileInput.test.js +533 -0
- package/dist/cjs/src/components/forms-and-input/image-file-input/components/ImagePreview.js +10 -3
- package/dist/cjs/src/hooks/__tests__/useObjectUrl.test.js +120 -0
- package/dist/cjs/src/hooks/index.js +11 -0
- package/dist/cjs/src/hooks/useObjectUrl.js +39 -0
- package/dist/esm/src/components/forms-and-input/file-input/FileInput.js +25 -7
- package/dist/esm/src/components/forms-and-input/file-input/__tests__/FileInput.test.js +165 -0
- package/dist/esm/src/components/forms-and-input/image-file-input/ImageFileInput.js +71 -19
- package/dist/esm/src/components/forms-and-input/image-file-input/__tests__/ImageFileInput.test.js +530 -0
- package/dist/esm/src/components/forms-and-input/image-file-input/components/ImagePreview.js +10 -3
- package/dist/esm/src/hooks/__tests__/useObjectUrl.test.js +117 -0
- package/dist/esm/src/hooks/index.js +2 -1
- package/dist/esm/src/hooks/useObjectUrl.js +32 -0
- package/dist/temp/src/components/forms-and-input/file-input/FileInput.d.ts +8 -0
- package/dist/temp/src/components/forms-and-input/file-input/FileInput.js +16 -3
- package/dist/temp/src/components/forms-and-input/file-input/__tests__/FileInput.test.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/file-input/__tests__/FileInput.test.js +139 -0
- package/dist/temp/src/components/forms-and-input/image-file-input/ImageFileInput.d.ts +33 -1
- package/dist/temp/src/components/forms-and-input/image-file-input/ImageFileInput.js +63 -18
- package/dist/temp/src/components/forms-and-input/image-file-input/__tests__/ImageFileInput.test.d.ts +1 -0
- package/dist/temp/src/components/forms-and-input/image-file-input/__tests__/ImageFileInput.test.js +435 -0
- package/dist/temp/src/components/forms-and-input/image-file-input/components/ImagePreview.d.ts +20 -2
- package/dist/temp/src/components/forms-and-input/image-file-input/components/ImagePreview.js +7 -2
- package/dist/temp/src/hooks/__tests__/useObjectUrl.test.d.ts +1 -0
- package/dist/temp/src/hooks/__tests__/useObjectUrl.test.js +96 -0
- package/dist/temp/src/hooks/index.d.ts +1 -0
- package/dist/temp/src/hooks/index.js +1 -0
- package/dist/temp/src/hooks/useObjectUrl.d.ts +16 -0
- package/dist/temp/src/hooks/useObjectUrl.js +32 -0
- package/dist/types/src/components/forms-and-input/file-input/FileInput.d.ts +8 -0
- package/dist/types/src/components/forms-and-input/file-input/__tests__/FileInput.test.d.ts +1 -0
- package/dist/types/src/components/forms-and-input/image-file-input/ImageFileInput.d.ts +33 -1
- package/dist/types/src/components/forms-and-input/image-file-input/__tests__/ImageFileInput.test.d.ts +1 -0
- package/dist/types/src/components/forms-and-input/image-file-input/components/ImagePreview.d.ts +20 -2
- package/dist/types/src/hooks/__tests__/useObjectUrl.test.d.ts +1 -0
- package/dist/types/src/hooks/index.d.ts +1 -0
- package/dist/types/src/hooks/useObjectUrl.d.ts +16 -0
- package/dist/ui-admin/assets/styles/style.css +20 -4
- package/package.json +1 -1
|
@@ -68,4 +68,15 @@ Object.keys(_useMergeRefs).forEach(function (key) {
|
|
|
68
68
|
return _useMergeRefs[key];
|
|
69
69
|
}
|
|
70
70
|
});
|
|
71
|
+
});
|
|
72
|
+
var _useObjectUrl = require("./useObjectUrl");
|
|
73
|
+
Object.keys(_useObjectUrl).forEach(function (key) {
|
|
74
|
+
if (key === "default" || key === "__esModule") return;
|
|
75
|
+
if (key in exports && exports[key] === _useObjectUrl[key]) return;
|
|
76
|
+
Object.defineProperty(exports, key, {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
get: function () {
|
|
79
|
+
return _useObjectUrl[key];
|
|
80
|
+
}
|
|
81
|
+
});
|
|
71
82
|
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useObjectUrl = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect");
|
|
9
|
+
/**
|
|
10
|
+
* Creates an object URL for a file and revokes it when the file changes
|
|
11
|
+
* or the component unmounts.
|
|
12
|
+
*
|
|
13
|
+
* Calling `URL.createObjectURL` inline while rendering mints a new blob URL on
|
|
14
|
+
* every re-render and never releases it, so the browser holds every one of them
|
|
15
|
+
* for the lifetime of the document. Creating it in an effect gives us a cleanup
|
|
16
|
+
* hook to revoke on.
|
|
17
|
+
*
|
|
18
|
+
* A layout effect is used so the URL is in place before the browser paints —
|
|
19
|
+
* with a plain effect the image would blink out for a frame whenever the
|
|
20
|
+
* component remounts (for example when a sibling is removed and keys shift).
|
|
21
|
+
*
|
|
22
|
+
* Returns `null` while no file is given.
|
|
23
|
+
*/
|
|
24
|
+
const useObjectUrl = file => {
|
|
25
|
+
const [objectUrl, setObjectUrl] = (0, _react.useState)(null);
|
|
26
|
+
(0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
|
27
|
+
if (!file) {
|
|
28
|
+
setObjectUrl(null);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const createdUrl = URL.createObjectURL(file);
|
|
32
|
+
setObjectUrl(createdUrl);
|
|
33
|
+
return () => {
|
|
34
|
+
URL.revokeObjectURL(createdUrl);
|
|
35
|
+
};
|
|
36
|
+
}, [file]);
|
|
37
|
+
return objectUrl;
|
|
38
|
+
};
|
|
39
|
+
exports.useObjectUrl = useObjectUrl;
|
|
@@ -2,16 +2,35 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import { HelpCircle, Upload01 } from '@ncds/ui-admin-icon';
|
|
3
3
|
import classNames from 'classnames';
|
|
4
4
|
import { forwardRef, useEffect, useRef, useState } from 'react';
|
|
5
|
+
import { useObjectUrl } from '../../../hooks/useObjectUrl';
|
|
5
6
|
import { Button } from '../../action/button';
|
|
6
7
|
import { Tag } from '../../feedback-and-status/tag/Tag';
|
|
7
8
|
import { HintText, Label } from '../../shared';
|
|
9
|
+
/**
|
|
10
|
+
* Thumbnail next to a file tag.
|
|
11
|
+
*
|
|
12
|
+
* Split out so the object URL can live in a hook — hooks cannot be called
|
|
13
|
+
* from inside the file list's map callback.
|
|
14
|
+
*/
|
|
15
|
+
const FileTagImage = _ref2 => {
|
|
16
|
+
let {
|
|
17
|
+
file
|
|
18
|
+
} = _ref2;
|
|
19
|
+
const objectUrl = useObjectUrl(file);
|
|
20
|
+
if (!objectUrl) return null;
|
|
21
|
+
return _jsx("img", {
|
|
22
|
+
className: "ncua-file-input__file-image",
|
|
23
|
+
src: objectUrl,
|
|
24
|
+
alt: file.name
|
|
25
|
+
});
|
|
26
|
+
};
|
|
8
27
|
export var FileInputErrorType;
|
|
9
28
|
(function (FileInputErrorType) {
|
|
10
29
|
FileInputErrorType["ALREADY_UPLOADED"] = "ALREADY_UPLOADED";
|
|
11
30
|
FileInputErrorType["EXCEED_MAX_FILE_SIZE"] = "EXCEED_MAX_FILE_SIZE";
|
|
12
31
|
FileInputErrorType["EXCEED_MAX_FILE_COUNT"] = "EXCEED_MAX_FILE_COUNT";
|
|
13
32
|
})(FileInputErrorType || (FileInputErrorType = {}));
|
|
14
|
-
export const FileInput = /*#__PURE__*/forwardRef((
|
|
33
|
+
export const FileInput = /*#__PURE__*/forwardRef((_ref3, _ref) => {
|
|
15
34
|
let {
|
|
16
35
|
size = 'xs',
|
|
17
36
|
accept,
|
|
@@ -31,8 +50,9 @@ export const FileInput = /*#__PURE__*/forwardRef((_ref2, _ref) => {
|
|
|
31
50
|
isRequired,
|
|
32
51
|
showHelpIcon,
|
|
33
52
|
hintText,
|
|
53
|
+
showFileTags = true,
|
|
34
54
|
...props
|
|
35
|
-
} =
|
|
55
|
+
} = _ref3;
|
|
36
56
|
const fileInputRef = useRef(null);
|
|
37
57
|
const [internalFiles, setInternalFiles] = useState([]);
|
|
38
58
|
// Determine if component is controlled or uncontrolled
|
|
@@ -108,7 +128,7 @@ export const FileInput = /*#__PURE__*/forwardRef((_ref2, _ref) => {
|
|
|
108
128
|
updateFiles(newFiles);
|
|
109
129
|
};
|
|
110
130
|
const renderFileTagList = () => {
|
|
111
|
-
if (files.length === 0) return null;
|
|
131
|
+
if (!showFileTags || files.length === 0) return null;
|
|
112
132
|
return _jsx("div", {
|
|
113
133
|
className: "ncua-file-input__file-tags",
|
|
114
134
|
children: files.map((file, index) => _jsxs("div", {
|
|
@@ -118,10 +138,8 @@ export const FileInput = /*#__PURE__*/forwardRef((_ref2, _ref) => {
|
|
|
118
138
|
size: size === 'xs' ? 'sm' : 'md',
|
|
119
139
|
close: true,
|
|
120
140
|
onButtonClick: () => handleRemoveFile(index)
|
|
121
|
-
}), file.type.startsWith('image/') && _jsx(
|
|
122
|
-
|
|
123
|
-
src: URL.createObjectURL(file),
|
|
124
|
-
alt: file.name
|
|
141
|
+
}), file.type.startsWith('image/') && _jsx(FileTagImage, {
|
|
142
|
+
file: file
|
|
125
143
|
})]
|
|
126
144
|
}, `${file.name}-${index}`))
|
|
127
145
|
});
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { createElement } from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { act } from 'react-dom/test-utils';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
6
|
+
import { FileInput } from '../FileInput';
|
|
7
|
+
const originalCreateObjectURL = URL.createObjectURL;
|
|
8
|
+
const originalRevokeObjectURL = URL.revokeObjectURL;
|
|
9
|
+
let createObjectURL;
|
|
10
|
+
let revokeObjectURL;
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
let issued = 0;
|
|
13
|
+
createObjectURL = vi.fn(() => `blob:mock/${++issued}`);
|
|
14
|
+
revokeObjectURL = vi.fn();
|
|
15
|
+
URL.createObjectURL = createObjectURL;
|
|
16
|
+
URL.revokeObjectURL = revokeObjectURL;
|
|
17
|
+
});
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
URL.createObjectURL = originalCreateObjectURL;
|
|
20
|
+
URL.revokeObjectURL = originalRevokeObjectURL;
|
|
21
|
+
});
|
|
22
|
+
/** 내용은 검증 대상이 아니므로 최소 크기만 채운다 */
|
|
23
|
+
const DUMMY_FILE_BYTES = 4;
|
|
24
|
+
const makeFile = (name, type) => new File([new Uint8Array(DUMMY_FILE_BYTES)], name, {
|
|
25
|
+
type
|
|
26
|
+
});
|
|
27
|
+
function mountFileInput(files) {
|
|
28
|
+
let extraProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
29
|
+
const container = document.createElement('div');
|
|
30
|
+
document.body.appendChild(container);
|
|
31
|
+
const root = createRoot(container);
|
|
32
|
+
const render = () => {
|
|
33
|
+
act(() => {
|
|
34
|
+
root.render(/*#__PURE__*/createElement(FileInput, {
|
|
35
|
+
value: files,
|
|
36
|
+
onChange: () => undefined,
|
|
37
|
+
...extraProps
|
|
38
|
+
}));
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
render();
|
|
42
|
+
const unmount = () => {
|
|
43
|
+
act(() => {
|
|
44
|
+
root.unmount();
|
|
45
|
+
});
|
|
46
|
+
container.remove();
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
container,
|
|
50
|
+
render,
|
|
51
|
+
unmount
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
describe('FileInput — 이미지 썸네일 object URL 해제', () => {
|
|
55
|
+
it('이미지 파일의 썸네일을 렌더한다', () => {
|
|
56
|
+
const view = mountFileInput([makeFile('a.jpg', 'image/jpeg')]);
|
|
57
|
+
const img = view.container.querySelector('.ncua-file-input__file-image');
|
|
58
|
+
expect(img).not.toBeNull();
|
|
59
|
+
expect(img?.getAttribute('src')).toBe('blob:mock/1');
|
|
60
|
+
expect(img?.getAttribute('alt')).toBe('a.jpg');
|
|
61
|
+
view.unmount();
|
|
62
|
+
});
|
|
63
|
+
it('리렌더를 반복해도 파일당 object URL을 한 번만 만든다', () => {
|
|
64
|
+
const view = mountFileInput([makeFile('a.jpg', 'image/jpeg')]);
|
|
65
|
+
view.render();
|
|
66
|
+
view.render();
|
|
67
|
+
view.render();
|
|
68
|
+
expect(createObjectURL).toHaveBeenCalledTimes(1);
|
|
69
|
+
view.unmount();
|
|
70
|
+
});
|
|
71
|
+
it('언마운트하면 만들었던 object URL을 해제한다', () => {
|
|
72
|
+
const view = mountFileInput([makeFile('a.jpg', 'image/jpeg')]);
|
|
73
|
+
expect(revokeObjectURL).not.toHaveBeenCalled();
|
|
74
|
+
view.unmount();
|
|
75
|
+
expect(revokeObjectURL).toHaveBeenCalledTimes(1);
|
|
76
|
+
expect(revokeObjectURL).toHaveBeenCalledWith('blob:mock/1');
|
|
77
|
+
});
|
|
78
|
+
it('이미지가 아닌 파일에는 object URL을 만들지 않는다', () => {
|
|
79
|
+
const view = mountFileInput([makeFile('doc.pdf', 'application/pdf')]);
|
|
80
|
+
expect(createObjectURL).not.toHaveBeenCalled();
|
|
81
|
+
expect(view.container.querySelector('.ncua-file-input__file-image')).toBeNull();
|
|
82
|
+
view.unmount();
|
|
83
|
+
});
|
|
84
|
+
it('이미지가 여러 개면 각각 하나씩만 만들고 언마운트 시 모두 해제한다', () => {
|
|
85
|
+
const view = mountFileInput([makeFile('a.jpg', 'image/jpeg'), makeFile('b.png', 'image/png')]);
|
|
86
|
+
expect(createObjectURL).toHaveBeenCalledTimes(2);
|
|
87
|
+
view.unmount();
|
|
88
|
+
expect(revokeObjectURL).toHaveBeenCalledTimes(2);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
/** 숨겨진 file input 에 파일을 넣고 change 를 발생시킨다 */
|
|
92
|
+
function selectFiles(container, files) {
|
|
93
|
+
const input = container.querySelector('input[type="file"]');
|
|
94
|
+
if (!input) throw new Error('file input을 찾을 수 없다');
|
|
95
|
+
Object.defineProperty(input, 'files', {
|
|
96
|
+
value: files,
|
|
97
|
+
configurable: true
|
|
98
|
+
});
|
|
99
|
+
act(() => {
|
|
100
|
+
input.dispatchEvent(new Event('change', {
|
|
101
|
+
bubbles: true
|
|
102
|
+
}));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
describe('FileInput — showFileTags', () => {
|
|
106
|
+
it('기본값에서는 파일 태그 목록을 렌더한다', () => {
|
|
107
|
+
const view = mountFileInput([makeFile('doc.pdf', 'application/pdf')]);
|
|
108
|
+
expect(view.container.querySelector('.ncua-file-input__file-tags')).not.toBeNull();
|
|
109
|
+
expect(view.container.querySelector('.ncua-tag')).not.toBeNull();
|
|
110
|
+
view.unmount();
|
|
111
|
+
});
|
|
112
|
+
it('false면 파일 태그 목록을 렌더하지 않는다', () => {
|
|
113
|
+
const view = mountFileInput([makeFile('doc.pdf', 'application/pdf')], {
|
|
114
|
+
showFileTags: false
|
|
115
|
+
});
|
|
116
|
+
expect(view.container.querySelector('.ncua-file-input__file-tags')).toBeNull();
|
|
117
|
+
expect(view.container.querySelector('.ncua-tag')).toBeNull();
|
|
118
|
+
view.unmount();
|
|
119
|
+
});
|
|
120
|
+
it('false여도 파일 찾기 버튼과 안내 문구는 그대로 렌더한다', () => {
|
|
121
|
+
const view = mountFileInput([makeFile('doc.pdf', 'application/pdf')], {
|
|
122
|
+
showFileTags: false,
|
|
123
|
+
hintItems: ['파일은 5MB 이내입니다.']
|
|
124
|
+
});
|
|
125
|
+
expect(view.container.querySelector('.ncua-btn')).not.toBeNull();
|
|
126
|
+
expect(view.container.querySelector('.ncua-file-input__hint-list')?.textContent).toContain('5MB');
|
|
127
|
+
view.unmount();
|
|
128
|
+
});
|
|
129
|
+
it('false면 이미지 썸네일 object URL을 만들지 않는다', () => {
|
|
130
|
+
const view = mountFileInput([makeFile('a.jpg', 'image/jpeg')], {
|
|
131
|
+
showFileTags: false
|
|
132
|
+
});
|
|
133
|
+
expect(createObjectURL).not.toHaveBeenCalled();
|
|
134
|
+
expect(view.container.querySelector('.ncua-file-input__file-image')).toBeNull();
|
|
135
|
+
view.unmount();
|
|
136
|
+
});
|
|
137
|
+
it('false여도 선택한 파일을 onChange로 전달한다', () => {
|
|
138
|
+
const onChange = vi.fn();
|
|
139
|
+
const view = mountFileInput([], {
|
|
140
|
+
showFileTags: false,
|
|
141
|
+
onChange
|
|
142
|
+
});
|
|
143
|
+
const picked = makeFile('a.pdf', 'application/pdf');
|
|
144
|
+
selectFiles(view.container, [picked]);
|
|
145
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
146
|
+
expect(onChange).toHaveBeenCalledWith([picked]);
|
|
147
|
+
view.unmount();
|
|
148
|
+
});
|
|
149
|
+
it('false여도 이미 등록된 파일은 중복으로 걸러 onFail로 알린다', () => {
|
|
150
|
+
const already = makeFile('a.pdf', 'application/pdf');
|
|
151
|
+
const onChange = vi.fn();
|
|
152
|
+
const onFail = vi.fn();
|
|
153
|
+
const view = mountFileInput([already], {
|
|
154
|
+
showFileTags: false,
|
|
155
|
+
onChange,
|
|
156
|
+
onFail
|
|
157
|
+
});
|
|
158
|
+
selectFiles(view.container, [makeFile('a.pdf', 'application/pdf')]);
|
|
159
|
+
expect(onChange).toHaveBeenCalledWith([already]);
|
|
160
|
+
expect(onFail).toHaveBeenCalledTimes(1);
|
|
161
|
+
expect(onFail.mock.calls[0][0]).toHaveLength(1);
|
|
162
|
+
expect(onFail.mock.calls[0][0][0].errorType).toBe('ALREADY_UPLOADED');
|
|
163
|
+
view.unmount();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -30,6 +30,8 @@ export const ImageFileInput = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
30
30
|
maxFileCount,
|
|
31
31
|
value,
|
|
32
32
|
onChange,
|
|
33
|
+
uploadedFiles = [],
|
|
34
|
+
onUploadedFilesChange,
|
|
33
35
|
onFileSelect,
|
|
34
36
|
onFail,
|
|
35
37
|
buttonLabel = '파일 찾기',
|
|
@@ -54,6 +56,9 @@ export const ImageFileInput = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
54
56
|
// Determine if component is controlled or uncontrolled
|
|
55
57
|
const isControlled = value !== undefined;
|
|
56
58
|
const files = isControlled ? value : internalFiles;
|
|
59
|
+
// An entry without a URL has nothing to render, so it must not occupy a slot either.
|
|
60
|
+
// TypeScript requires fileImageUrl, but CDN/vanilla consumers are untyped.
|
|
61
|
+
const visibleUploadedFiles = uploadedFiles.filter(uploadedFile => Boolean(uploadedFile.fileImageUrl));
|
|
57
62
|
// Sync internal state with controlled value
|
|
58
63
|
useEffect(() => {
|
|
59
64
|
if (isControlled && value) {
|
|
@@ -75,32 +80,57 @@ export const ImageFileInput = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
75
80
|
validFiles,
|
|
76
81
|
invalidFiles
|
|
77
82
|
} = validateFiles(Array.from(selectedFiles));
|
|
78
|
-
//
|
|
79
|
-
|
|
80
|
-
|
|
83
|
+
// Nothing passed validation — keep what the caller already had.
|
|
84
|
+
// Without this, a single slot would be overwritten with an empty list.
|
|
85
|
+
if (validFiles.length > 0) {
|
|
86
|
+
// Replace existing file if maxFileCount is 1
|
|
87
|
+
const nextFiles = maxFileCount === 1 ? validFiles : [...files, ...validFiles];
|
|
88
|
+
updateFiles(nextFiles);
|
|
89
|
+
// A single slot holds one image, so a new selection also clears the stored one.
|
|
90
|
+
// Entries we never rendered occupy no slot, so they are left in the caller's list.
|
|
91
|
+
if (maxFileCount === 1 && visibleUploadedFiles.length > 0) {
|
|
92
|
+
onUploadedFilesChange?.(uploadedFiles.filter(uploadedFile => !uploadedFile.fileImageUrl));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
81
95
|
if (onFail && invalidFiles.length > 0) {
|
|
82
96
|
onFail(invalidFiles);
|
|
83
97
|
}
|
|
84
98
|
event.target.value = '';
|
|
85
99
|
};
|
|
100
|
+
/**
|
|
101
|
+
* Returns why a file must be rejected, or null when it is acceptable.
|
|
102
|
+
* `acceptedCount` is how many files of the same selection already passed.
|
|
103
|
+
*/
|
|
104
|
+
const findRejectionReason = (file, acceptedCount) => {
|
|
105
|
+
if (files.some(f => f.name === file.name && f.size === file.size)) {
|
|
106
|
+
return ImageFileInputErrorType.ALREADY_UPLOADED;
|
|
107
|
+
}
|
|
108
|
+
// Stored images carry no size, so they can only be matched by name.
|
|
109
|
+
// A single slot is meant to be replaced, so re-picking the same name is allowed there.
|
|
110
|
+
if (maxFileCount !== 1 && visibleUploadedFiles.some(uploadedFile => uploadedFile.fileName === file.name)) {
|
|
111
|
+
return ImageFileInputErrorType.ALREADY_UPLOADED;
|
|
112
|
+
}
|
|
113
|
+
if (!!maxFileSize && file.size > maxFileSize) {
|
|
114
|
+
return ImageFileInputErrorType.EXCEED_MAX_FILE_SIZE;
|
|
115
|
+
}
|
|
116
|
+
// Skip max count check if maxFileCount is 1 (allow replacement).
|
|
117
|
+
// Stored images occupy slots too, so they count toward the limit.
|
|
118
|
+
const usedSlots = visibleUploadedFiles.length + files.length + acceptedCount;
|
|
119
|
+
if (!!maxFileCount && maxFileCount !== 1 && usedSlots >= maxFileCount) {
|
|
120
|
+
return ImageFileInputErrorType.EXCEED_MAX_FILE_COUNT;
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
};
|
|
86
124
|
const validateFiles = fileList => {
|
|
87
125
|
const validFiles = [];
|
|
88
126
|
const invalidFiles = [];
|
|
89
127
|
for (const file of fileList) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
invalidFiles.push(toInvalidFile(file, ImageFileInputErrorType.EXCEED_MAX_FILE_SIZE));
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
// Skip max count check if maxFileCount is 1 (allow replacement)
|
|
99
|
-
if (!!maxFileCount && maxFileCount !== 1 && files.length + validFiles.length >= maxFileCount) {
|
|
100
|
-
invalidFiles.push(toInvalidFile(file, ImageFileInputErrorType.EXCEED_MAX_FILE_COUNT));
|
|
101
|
-
continue;
|
|
128
|
+
const rejectionReason = findRejectionReason(file, validFiles.length);
|
|
129
|
+
if (rejectionReason) {
|
|
130
|
+
invalidFiles.push(toInvalidFile(file, rejectionReason));
|
|
131
|
+
} else {
|
|
132
|
+
validFiles.push(file);
|
|
102
133
|
}
|
|
103
|
-
validFiles.push(file);
|
|
104
134
|
}
|
|
105
135
|
return {
|
|
106
136
|
validFiles,
|
|
@@ -117,13 +147,28 @@ export const ImageFileInput = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
117
147
|
newFiles.splice(index, 1);
|
|
118
148
|
updateFiles(newFiles);
|
|
119
149
|
};
|
|
150
|
+
// Removed by identity, not index, so keys stay stable and the caller's
|
|
151
|
+
// untouched entries (including ones we skip rendering) are preserved
|
|
152
|
+
const handleRemoveUploadedFile = target => {
|
|
153
|
+
onUploadedFilesChange?.(uploadedFiles.filter(uploadedFile => uploadedFile !== target));
|
|
154
|
+
};
|
|
120
155
|
const renderImagePreview = function () {
|
|
121
156
|
let files = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
122
|
-
const
|
|
157
|
+
const totalCount = visibleUploadedFiles.length + files.length;
|
|
158
|
+
const showEmptySlot = maxFileCount ? totalCount < maxFileCount : totalCount === 0;
|
|
123
159
|
return _jsxs("div", {
|
|
124
160
|
className: "ncua-image-file-input__previews",
|
|
125
|
-
children: [
|
|
161
|
+
children: [visibleUploadedFiles.map((uploadedFile, index) => _jsx(ImagePreview
|
|
162
|
+
// Removal is by identity, so the index here only breaks ties between
|
|
163
|
+
// entries that share both URL and file name.
|
|
164
|
+
, {
|
|
165
|
+
imageUrl: uploadedFile.fileImageUrl,
|
|
166
|
+
alt: uploadedFile.fileName,
|
|
167
|
+
disabled: disabled,
|
|
168
|
+
onRemove: () => handleRemoveUploadedFile(uploadedFile)
|
|
169
|
+
}, `uploaded-${index}-${uploadedFile.fileImageUrl}-${uploadedFile.fileName}`)), files.map((file, index) => _jsx(ImagePreview, {
|
|
126
170
|
file: file,
|
|
171
|
+
disabled: disabled,
|
|
127
172
|
onRemove: () => handleRemoveFile(index)
|
|
128
173
|
}, `${file.name}-${index}`)), showEmptySlot && _jsxs("div", {
|
|
129
174
|
className: "ncua-image-file-input__empty-slot-wrapper",
|
|
@@ -199,4 +244,11 @@ export const ImageFileInput = /*#__PURE__*/forwardRef((_ref, ref) => {
|
|
|
199
244
|
}), showHintText && renderHintList()]
|
|
200
245
|
})]
|
|
201
246
|
});
|
|
202
|
-
});
|
|
247
|
+
});
|
|
248
|
+
/**
|
|
249
|
+
* Rejection reasons reported through `onFail`.
|
|
250
|
+
*
|
|
251
|
+
* Re-exported under the ImageFileInput name so callers do not have to reach
|
|
252
|
+
* into FileInput for it. Same enum, same values.
|
|
253
|
+
*/
|
|
254
|
+
export { ImageFileInputErrorType };
|