-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSimpleFilesTable.tsx
More file actions
217 lines (207 loc) · 7.22 KB
/
Copy pathSimpleFilesTable.tsx
File metadata and controls
217 lines (207 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
import { ScrollTable } from '@nemo/common/src/components/ScrollTable';
import { useUploadModalContext } from '@nemo/common/src/components/UploadModal/Context/useUploadModalContext';
import { useInlinePickerSlot } from '@nemo/common/src/components/UploadModal/InlinePickerSlot';
import { UploadFile } from '@nemo/common/src/components/UploadModal/types';
import { formatFileSize } from '@nemo/common/src/components/UploadModal/utils';
import {
Button,
Checkbox,
Text,
TableColumnDefinition,
TableRowDefinition,
Flex,
Stack,
RadioGroupRoot,
RadioGroupItem,
RadioGroupInput,
} from '@nvidia/foundations-react-core';
import { CircleAlert } from 'lucide-react';
import { useCallback, useMemo, useRef } from 'react';
export const SimpleFilesTable = () => {
const [state, dispatch] = useUploadModalContext();
const { trailingButton } = useInlinePickerSlot();
const fileInputRef = useRef<HTMLInputElement>(null);
const {
files,
selectedFiles,
errors,
acceptableFileTypes,
allowMultipleFileSelection,
invalidFileMode,
} = state;
const fileExtension = (uploadFile: UploadFile): string => {
const name = uploadFile.type === 'existing' ? uploadFile.file.path : uploadFile.file.name;
const dot = name.lastIndexOf('.');
return dot >= 0 ? name.slice(dot).toLowerCase() : '';
};
const allowedExtensions = useMemo(
() => new Set((acceptableFileTypes ?? []).map((ext) => ext.toLowerCase())),
[acceptableFileTypes]
);
const isFileAllowed = (uploadFile: UploadFile): boolean => {
if (allowedExtensions.size === 0) return true;
return allowedExtensions.has(fileExtension(uploadFile));
};
const toggleFileSelection = useCallback(
(file: UploadFile) => {
dispatch({
type: 'TOGGLE_FILE_SELECTION',
payload: file,
});
},
[dispatch]
);
const handleSingleSelect = useCallback(
(id: string) => {
const file = files.find((f) => f.id === id);
if (!file) return;
dispatch({ type: 'TOGGLE_FILE_SELECTION', payload: file });
},
[dispatch, files]
);
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const files = event.target.files;
if (files) {
dispatch({
type: 'SET_FILES',
payload: Array.from(files).map((file) => ({ id: file.name, type: 'new', file })),
});
}
};
const columns: TableColumnDefinition[] = [
{ children: '' },
{ children: 'Name' },
{ children: 'Size' },
];
// ``invalidFileMode`` controls how files whose extension isn't in
// ``acceptableFileTypes`` are rendered. ``'hide'`` filters them out so the
// user only sees pickable files; ``'disable'`` keeps them visible but
// marks the radio/checkbox as ``disabled``; ``'show'`` (default) keeps
// the prior behaviour and lets the parent validate after submit.
const visibleFiles = useMemo(() => {
if (invalidFileMode !== 'hide' || allowedExtensions.size === 0) return files;
return files.filter(isFileAllowed);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [files, allowedExtensions, invalidFileMode]);
const disabledFilesMessage =
invalidFileMode === 'disable' &&
allowedExtensions.size > 0 &&
visibleFiles.some((file) => !isFileAllowed(file))
? `Only ${acceptableFileTypes.join(', ')} files can be selected. Upload a supported file or choose a different fileset.`
: null;
const rows = useMemo<TableRowDefinition[]>(
() =>
visibleFiles.map((uploadFile) => {
// In ``'disable'`` mode, mismatched-extension rows render but their
// selector control is ``disabled``. ``'hide'`` already filtered
// them; ``'show'`` keeps everything pickable.
const isDisabled = invalidFileMode === 'disable' && !isFileAllowed(uploadFile);
const name = uploadFile.type === 'existing' ? uploadFile.file.path : uploadFile.file.name;
const size = uploadFile.type === 'existing' ? uploadFile.file.size : uploadFile.file.size;
return {
id: uploadFile.id,
cells: [
{
children: allowMultipleFileSelection ? (
<Checkbox
name={name}
attributes={{ CheckboxInput: { 'aria-label': name } }}
checked={selectedFiles.some((file) => file.id === uploadFile.id)}
onCheckedChange={() => toggleFileSelection(uploadFile)}
disabled={isDisabled}
/>
) : (
<RadioGroupItem aria-label={name}>
<RadioGroupInput value={uploadFile.id} disabled={isDisabled} />
</RadioGroupItem>
),
},
{ children: name },
{ children: formatFileSize(size) },
],
};
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[visibleFiles, selectedFiles, toggleFileSelection, allowMultipleFileSelection, invalidFileMode]
);
return (
<Stack className="min-h-0 flex-1 w-full" gap="density-md">
{allowMultipleFileSelection ? (
<ScrollTable
pagination={false}
columns={columns}
rows={rows}
allowHorizontalScroll
className="pb-0 w-full"
/>
) : (
// ``RadioGroupRoot`` defaults to its content's natural width — force
// ``w-full`` so the inner ScrollTable fills the modal's width.
<RadioGroupRoot
name="simple-files-table"
value={selectedFiles[0]?.id ?? ''}
onValueChange={handleSingleSelect}
className="w-full"
>
<ScrollTable
pagination={false}
columns={columns}
rows={rows}
allowHorizontalScroll
className="pb-0 w-full"
/>
</RadioGroupRoot>
)}
{disabledFilesMessage ? (
<Flex gap="density-sm" align="center">
<CircleAlert className="text-feedback-warning shrink-0" />
<Text kind="label/regular/sm" className="text-feedback-warning">
{disabledFilesMessage}
</Text>
</Flex>
) : null}
{errors.file && (
<Flex gap="density-md" align="center">
<CircleAlert className="text-feedback-danger" />
<Text kind="label/regular/sm" className="text-feedback-danger">
{errors.file}
</Text>
</Flex>
)}
{trailingButton ? (
<Flex justify="between" align="center">
<Button
kind="tertiary"
onClick={() => {
fileInputRef.current?.click();
}}
>
Upload More Files
</Button>
{trailingButton}
</Flex>
) : (
<Button
kind="tertiary"
onClick={() => {
fileInputRef.current?.click();
}}
>
Upload More Files
</Button>
)}
<input
ref={fileInputRef}
type="file"
multiple
tabIndex={-1}
onChange={handleFileChange}
accept={acceptableFileTypes.join(',')}
className="sr-only"
aria-label="Upload more files"
/>
</Stack>
);
};