@trama-ui/react-native 0.0.0-stage → 0.7.0
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/LICENSE +21 -0
- package/README.md +198 -2
- package/dist/accordion.d.ts +29 -0
- package/dist/accordion.js +137 -0
- package/dist/alert.d.ts +30 -0
- package/dist/alert.js +95 -0
- package/dist/avatar.d.ts +36 -0
- package/dist/avatar.js +91 -0
- package/dist/bars.d.ts +44 -0
- package/dist/bars.js +74 -0
- package/dist/button.d.ts +30 -0
- package/dist/button.js +90 -0
- package/dist/calendar.d.ts +71 -0
- package/dist/calendar.js +276 -0
- package/dist/card.d.ts +41 -0
- package/dist/card.js +139 -0
- package/dist/choices.d.ts +87 -0
- package/dist/choices.js +228 -0
- package/dist/collections.d.ts +131 -0
- package/dist/collections.js +230 -0
- package/dist/color-code.d.ts +37 -0
- package/dist/color-code.js +158 -0
- package/dist/compositions.d.ts +110 -0
- package/dist/compositions.js +190 -0
- package/dist/controls.d.ts +85 -0
- package/dist/controls.js +221 -0
- package/dist/drag-drop.d.ts +65 -0
- package/dist/drag-drop.js +482 -0
- package/dist/feedback.d.ts +30 -0
- package/dist/feedback.js +81 -0
- package/dist/file-upload.d.ts +67 -0
- package/dist/file-upload.js +251 -0
- package/dist/generated/icons.d.ts +1037 -0
- package/dist/generated/icons.js +3 -0
- package/dist/generated/tokens.d.ts +64 -0
- package/dist/generated/tokens.js +65 -0
- package/dist/icon.d.ts +31 -0
- package/dist/icon.js +28 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +43 -0
- package/dist/input.d.ts +43 -0
- package/dist/input.js +115 -0
- package/dist/interactive-layout.d.ts +26 -0
- package/dist/interactive-layout.js +117 -0
- package/dist/internal/content.d.ts +17 -0
- package/dist/internal/content.js +24 -0
- package/dist/internal/drag-geometry.d.ts +16 -0
- package/dist/internal/drag-geometry.js +25 -0
- package/dist/internal/keyboard.d.ts +20 -0
- package/dist/internal/keyboard.js +101 -0
- package/dist/internal/motion.d.ts +1 -0
- package/dist/internal/motion.js +24 -0
- package/dist/internal/number-format.d.ts +4 -0
- package/dist/internal/number-format.js +25 -0
- package/dist/internal/pointer-capture.d.ts +1 -0
- package/dist/internal/pointer-capture.js +16 -0
- package/dist/internal/state.d.ts +6 -0
- package/dist/internal/state.js +28 -0
- package/dist/internal/system-drag.d.ts +48 -0
- package/dist/internal/system-drag.js +93 -0
- package/dist/internal/table-drag.d.ts +56 -0
- package/dist/internal/table-drag.js +237 -0
- package/dist/internal/table-editing.d.ts +49 -0
- package/dist/internal/table-editing.js +278 -0
- package/dist/internal/table-editor.d.ts +11 -0
- package/dist/internal/table-editor.js +31 -0
- package/dist/internal/table-grid.d.ts +44 -0
- package/dist/internal/table-grid.js +153 -0
- package/dist/internal/table-model.d.ts +132 -0
- package/dist/internal/table-model.js +231 -0
- package/dist/internal/table-source.d.ts +9 -0
- package/dist/internal/table-source.js +80 -0
- package/dist/internal/tour-target.d.ts +23 -0
- package/dist/internal/tour-target.js +81 -0
- package/dist/keyboard.d.ts +14 -0
- package/dist/keyboard.js +27 -0
- package/dist/layout.d.ts +35 -0
- package/dist/layout.js +77 -0
- package/dist/loader.d.ts +18 -0
- package/dist/loader.js +79 -0
- package/dist/localization.d.ts +106 -0
- package/dist/localization.js +310 -0
- package/dist/media.d.ts +35 -0
- package/dist/media.js +96 -0
- package/dist/menus.d.ts +103 -0
- package/dist/menus.js +422 -0
- package/dist/navigation.d.ts +111 -0
- package/dist/navigation.js +255 -0
- package/dist/overlays.d.ts +97 -0
- package/dist/overlays.js +364 -0
- package/dist/pill.d.ts +33 -0
- package/dist/pill.js +90 -0
- package/dist/progress.d.ts +27 -0
- package/dist/progress.js +154 -0
- package/dist/responsive.d.ts +15 -0
- package/dist/responsive.js +27 -0
- package/dist/select.d.ts +74 -0
- package/dist/select.js +181 -0
- package/dist/separator.d.ts +13 -0
- package/dist/separator.js +34 -0
- package/dist/switch.d.ts +26 -0
- package/dist/switch.js +69 -0
- package/dist/table.d.ts +122 -0
- package/dist/table.js +928 -0
- package/dist/text-fields.d.ts +39 -0
- package/dist/text-fields.js +134 -0
- package/dist/theme.d.ts +47 -0
- package/dist/theme.js +71 -0
- package/dist/transient.d.ts +80 -0
- package/dist/transient.js +273 -0
- package/package.json +50 -4
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, forwardRef, useContext, useEffect, useId, useImperativeHandle, useRef, useState, } from 'react';
|
|
3
|
+
import { Pressable, Text, View } from 'react-native';
|
|
4
|
+
import { Button } from './button.js';
|
|
5
|
+
import { DropZone } from './drag-drop.js';
|
|
6
|
+
import { FieldMessage, splitContent, TextContent } from './internal/content.js';
|
|
7
|
+
import { useControllableValue, useFieldValidation } from './internal/state.js';
|
|
8
|
+
import { Cluster } from './layout.js';
|
|
9
|
+
import { useTramaLocale, useTramaMessages } from './localization.js';
|
|
10
|
+
import { Image } from './media.js';
|
|
11
|
+
import { Dialog, DialogClose, DialogFooter } from './overlays.js';
|
|
12
|
+
import { Progress } from './progress.js';
|
|
13
|
+
import { useTramaAppearance, useTramaTheme } from './theme.js';
|
|
14
|
+
function isFile(value) {
|
|
15
|
+
return Boolean(value &&
|
|
16
|
+
typeof value === 'object' &&
|
|
17
|
+
'uri' in value &&
|
|
18
|
+
typeof value.uri === 'string' &&
|
|
19
|
+
'name' in value &&
|
|
20
|
+
typeof value.name === 'string');
|
|
21
|
+
}
|
|
22
|
+
function accepts(file, accept) {
|
|
23
|
+
const rules = accept
|
|
24
|
+
?.toLowerCase()
|
|
25
|
+
.split(',')
|
|
26
|
+
.map((rule) => rule.trim())
|
|
27
|
+
.filter(Boolean) ?? [];
|
|
28
|
+
return (!rules.length ||
|
|
29
|
+
rules.some((rule) => rule.startsWith('.')
|
|
30
|
+
? file.name.toLowerCase().endsWith(rule)
|
|
31
|
+
: rule.endsWith('/*')
|
|
32
|
+
? file.type?.toLowerCase().startsWith(rule.slice(0, -1))
|
|
33
|
+
: file.type?.toLowerCase() === rule));
|
|
34
|
+
}
|
|
35
|
+
function keyFor(file) {
|
|
36
|
+
return file.id ?? file.uri;
|
|
37
|
+
}
|
|
38
|
+
function imageUri(file) {
|
|
39
|
+
return (file.previewUri ??
|
|
40
|
+
(file.type?.startsWith('image/') || /\.(?:png|jpe?g|gif|webp|heic|heif)$/iu.test(file.name) ? file.uri : undefined));
|
|
41
|
+
}
|
|
42
|
+
const FileContext = createContext(null);
|
|
43
|
+
export const FileUpload = forwardRef(function FileUpload({ label, description, files, defaultFiles, onFilesChange, chooseFiles, onPickerError, systemDrop = false, onSystemDropError, onReject, onPreview, onRetry, onCancelUpload, renderItemMedia, name, required, disabled, readOnly, invalid, error, multiple, accept, maxFiles, maxFileSize, listStyle, children, style, ...props }, ref) {
|
|
44
|
+
const { theme, direction, resolve } = useTramaAppearance();
|
|
45
|
+
const messages = useTramaMessages();
|
|
46
|
+
const locale = useTramaLocale();
|
|
47
|
+
const [selected, setSelected] = useControllableValue(files, defaultFiles ?? []);
|
|
48
|
+
const [busy, setBusy] = useState(false);
|
|
49
|
+
const [pickerError, setPickerError] = useState(false);
|
|
50
|
+
const [dropError, setDropError] = useState(false);
|
|
51
|
+
const [rejected, setRejected] = useState([]);
|
|
52
|
+
const [preview, setPreview] = useState();
|
|
53
|
+
const locked = Boolean(disabled || readOnly);
|
|
54
|
+
const thumbnail = resolve(listStyle, 'list') === 'thumbnail';
|
|
55
|
+
const pending = useRef(null);
|
|
56
|
+
const alive = useRef(false);
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
alive.current = true;
|
|
59
|
+
return () => {
|
|
60
|
+
alive.current = false;
|
|
61
|
+
pending.current?.abort();
|
|
62
|
+
pending.current = null;
|
|
63
|
+
};
|
|
64
|
+
}, []);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (locked)
|
|
67
|
+
pending.current?.abort();
|
|
68
|
+
}, [locked]);
|
|
69
|
+
const limit = multiple ? Math.max(0, maxFiles ?? Infinity) : 1;
|
|
70
|
+
const { named, body } = splitContent(children, {
|
|
71
|
+
actions: FileUploadActions,
|
|
72
|
+
dropzone: FileUploadDropzone,
|
|
73
|
+
trigger: FileUploadTrigger,
|
|
74
|
+
media: FileUploadItemMedia,
|
|
75
|
+
});
|
|
76
|
+
const fieldConstraint = required && !selected.length ? messages.chooseFile : '';
|
|
77
|
+
const validation = useFieldValidation(error?.trim() || (invalid ? messages.reviewField : ''), fieldConstraint, locked);
|
|
78
|
+
const rejectionMessage = rejected.length ? messages.fileRejected(rejected.length) : '';
|
|
79
|
+
const visibleError = validation.message ||
|
|
80
|
+
(pickerError ? messages.filePickerError : dropError ? messages.fileDropError : rejectionMessage);
|
|
81
|
+
function update(next) {
|
|
82
|
+
if (!locked && onFilesChange?.(next) !== false)
|
|
83
|
+
setSelected(next);
|
|
84
|
+
}
|
|
85
|
+
function append(incoming) {
|
|
86
|
+
if (locked || !incoming.length)
|
|
87
|
+
return;
|
|
88
|
+
setDropError(false);
|
|
89
|
+
const next = multiple ? [...selected] : [];
|
|
90
|
+
const rejects = [];
|
|
91
|
+
let additions = 0;
|
|
92
|
+
for (const file of incoming) {
|
|
93
|
+
if (!accepts(file, accept))
|
|
94
|
+
rejects.push({ file, reason: 'type' });
|
|
95
|
+
else if (maxFileSize !== undefined && file.size !== undefined && file.size > maxFileSize)
|
|
96
|
+
rejects.push({ file, reason: 'size' });
|
|
97
|
+
else if (next.some((entry) => keyFor(entry) === keyFor(file)))
|
|
98
|
+
continue;
|
|
99
|
+
else if (next.length >= limit)
|
|
100
|
+
rejects.push({ file, reason: 'limit' });
|
|
101
|
+
else {
|
|
102
|
+
next.push(file);
|
|
103
|
+
additions++;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
setRejected(rejects);
|
|
107
|
+
if (rejects.length)
|
|
108
|
+
onReject?.(rejects);
|
|
109
|
+
if (additions)
|
|
110
|
+
update(next);
|
|
111
|
+
}
|
|
112
|
+
const latest = useRef({ append, onPickerError });
|
|
113
|
+
latest.current = { append, onPickerError };
|
|
114
|
+
async function browse() {
|
|
115
|
+
if (locked || !chooseFiles || pending.current)
|
|
116
|
+
return;
|
|
117
|
+
const controller = new AbortController();
|
|
118
|
+
pending.current = controller;
|
|
119
|
+
setBusy(true);
|
|
120
|
+
setRejected([]);
|
|
121
|
+
setPickerError(false);
|
|
122
|
+
setDropError(false);
|
|
123
|
+
try {
|
|
124
|
+
const next = await chooseFiles({ multiple: Boolean(multiple), accept, signal: controller.signal });
|
|
125
|
+
if (alive.current && !controller.signal.aborted && next)
|
|
126
|
+
latest.current.append(next);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (alive.current && !controller.signal.aborted) {
|
|
130
|
+
setPickerError(true);
|
|
131
|
+
latest.current.onPickerError?.(error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
finally {
|
|
135
|
+
if (pending.current === controller) {
|
|
136
|
+
pending.current = null;
|
|
137
|
+
if (alive.current)
|
|
138
|
+
setBusy(false);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function cancelBrowse() {
|
|
143
|
+
const controller = pending.current;
|
|
144
|
+
if (!controller || controller.signal.aborted)
|
|
145
|
+
return false;
|
|
146
|
+
controller.abort();
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
function clear() {
|
|
150
|
+
if (locked)
|
|
151
|
+
return;
|
|
152
|
+
cancelBrowse();
|
|
153
|
+
setRejected([]);
|
|
154
|
+
setPickerError(false);
|
|
155
|
+
setDropError(false);
|
|
156
|
+
if (selected.length)
|
|
157
|
+
update([]);
|
|
158
|
+
}
|
|
159
|
+
useImperativeHandle(ref, () => ({ ...validation, browse, cancelBrowse, append, clear }));
|
|
160
|
+
return (_jsx(FileContext.Provider, { value: {
|
|
161
|
+
browse: () => {
|
|
162
|
+
void browse();
|
|
163
|
+
},
|
|
164
|
+
clear,
|
|
165
|
+
disabled: locked,
|
|
166
|
+
browseDisabled: !chooseFiles,
|
|
167
|
+
busy,
|
|
168
|
+
acceptDrop: append,
|
|
169
|
+
systemDrop,
|
|
170
|
+
reportDropError(error) {
|
|
171
|
+
setPickerError(false);
|
|
172
|
+
setDropError(true);
|
|
173
|
+
onSystemDropError?.(error);
|
|
174
|
+
},
|
|
175
|
+
}, children: _jsxs(View, { ...props, style: [{ gap: theme.space * 2, direction }, style], children: [label && (_jsxs(Text, { style: { color: theme.colors.text, fontSize: theme.fontSize, fontWeight: '700' }, children: [label, required ? ' *' : ''] })), description && _jsx(Text, { style: { color: theme.colors.muted }, children: description }), named.dropzone ?? (_jsxs(FileUploadDropzone, { id: name, children: [named.trigger ?? _jsx(FileUploadTrigger, {}), body] })), _jsx(FieldMessage, { message: visibleError, invalid: Boolean(visibleError), theme: theme }), selected.map((file) => {
|
|
176
|
+
const uri = imageUri(file);
|
|
177
|
+
const canPreview = Boolean(onPreview || uri);
|
|
178
|
+
const media = renderItemMedia?.(file) ??
|
|
179
|
+
named.media ??
|
|
180
|
+
(thumbnail && uri ? (_jsx(Image, { source: { uri }, aspectRatio: 1, fit: "contain", alt: file.name, containerStyle: { width: 52 } })) : null);
|
|
181
|
+
const size = file.size === undefined
|
|
182
|
+
? undefined
|
|
183
|
+
: new Intl.NumberFormat(locale, {
|
|
184
|
+
style: 'unit',
|
|
185
|
+
unit: file.size >= 1000 ? 'kilobyte' : 'byte',
|
|
186
|
+
unitDisplay: 'short',
|
|
187
|
+
maximumFractionDigits: 1,
|
|
188
|
+
}).format(file.size >= 1000 ? file.size / 1000 : file.size);
|
|
189
|
+
const metadata = (_jsxs(_Fragment, { children: [media, _jsxs(View, { style: { flex: 1, minWidth: 0 }, children: [_jsx(Text, { style: { color: theme.colors.text, fontWeight: '600' }, children: file.name }), size && _jsx(Text, { style: { color: theme.colors.muted }, children: size })] })] }));
|
|
190
|
+
return (_jsxs(View, { style: {
|
|
191
|
+
borderColor: theme.colors.border,
|
|
192
|
+
borderWidth: 1,
|
|
193
|
+
borderRadius: theme.radius,
|
|
194
|
+
padding: theme.space * 2,
|
|
195
|
+
gap: theme.space,
|
|
196
|
+
}, children: [_jsxs(View, { style: { flexDirection: 'row', alignItems: 'center', gap: theme.space * 2 }, children: [canPreview ? (_jsx(Pressable, { accessibilityRole: "button", accessibilityLabel: messages.previewFile(file.name), onPress: () => (onPreview ? onPreview(file) : setPreview(file)), style: { flex: 1, minHeight: 44, flexDirection: 'row', gap: theme.space * 2, alignItems: 'center' }, children: metadata })) : (_jsx(View, { style: { flex: 1, flexDirection: 'row', gap: theme.space * 2, alignItems: 'center' }, children: metadata })), !locked && (_jsx(Button, { variant: "text", size: "sm", accessibilityLabel: messages.removeItem + ' ' + file.name, onPress: () => update(selected.filter((entry) => keyFor(entry) !== keyFor(file))), children: "\u00D7" }))] }), file.status === 'uploading' && _jsx(Progress, { value: file.progress, label: file.name }), file.error && _jsx(FieldMessage, { message: file.error, invalid: true, theme: theme }), !locked && file.status === 'error' && onRetry && (_jsx(Button, { variant: "text", onPress: () => onRetry(file), children: messages.retry })), !locked && file.status === 'uploading' && onCancelUpload && (_jsx(Button, { variant: "text", onPress: () => onCancelUpload(file), children: messages.cancel }))] }, keyFor(file)));
|
|
197
|
+
}), named.actions ??
|
|
198
|
+
(selected.length > 0 && !locked && (_jsx(FileUploadActions, { children: _jsx(ClearFiles, {}) }))), _jsxs(Dialog, { heading: preview?.name, open: Boolean(preview), onOpenChange: (open) => {
|
|
199
|
+
if (!open)
|
|
200
|
+
setPreview(undefined);
|
|
201
|
+
}, children: [preview && _jsx(Image, { source: { uri: imageUri(preview) }, fit: "contain", aspectRatio: 1, alt: preview.name }), _jsx(DialogFooter, { children: _jsx(DialogClose, {}) })] })] }) }));
|
|
202
|
+
});
|
|
203
|
+
export function FileUploadDropzone({ id, style, children, ...props }) {
|
|
204
|
+
const state = useContext(FileContext);
|
|
205
|
+
const generated = useId();
|
|
206
|
+
const theme = useTramaTheme();
|
|
207
|
+
const appearance = {
|
|
208
|
+
minHeight: 88,
|
|
209
|
+
justifyContent: 'center',
|
|
210
|
+
alignItems: 'center',
|
|
211
|
+
backgroundColor: theme.colors.subtle,
|
|
212
|
+
};
|
|
213
|
+
if (!id && !state?.systemDrop)
|
|
214
|
+
return (_jsx(View, { ...props, style: [
|
|
215
|
+
appearance,
|
|
216
|
+
{
|
|
217
|
+
borderWidth: 1,
|
|
218
|
+
borderStyle: 'dashed',
|
|
219
|
+
borderRadius: theme.radius,
|
|
220
|
+
borderColor: theme.colors.border,
|
|
221
|
+
padding: theme.space * 3,
|
|
222
|
+
gap: theme.space,
|
|
223
|
+
},
|
|
224
|
+
style,
|
|
225
|
+
], children: children }));
|
|
226
|
+
return (_jsx(DropZone, { ...props, id: id ?? generated, disabled: state?.disabled, style: [appearance, style], accepts: (item) => item.type === 'files' && Array.isArray(item.data) && item.data.some(isFile), onDrop: (item) => {
|
|
227
|
+
if (Array.isArray(item.data))
|
|
228
|
+
state?.acceptDrop(item.data.filter(isFile));
|
|
229
|
+
}, systemDrop: state?.systemDrop ? ['files'] : undefined, onSystemDrop: ({ items }) => {
|
|
230
|
+
state?.acceptDrop(items.flatMap((item) => item.kind === 'file' ? [{ uri: item.uri, name: item.name, type: item.type, size: item.size }] : []));
|
|
231
|
+
}, onSystemDropError: state?.reportDropError, children: children }));
|
|
232
|
+
}
|
|
233
|
+
export function FileUploadTrigger({ children, onPress, ...props }) {
|
|
234
|
+
const context = useContext(FileContext);
|
|
235
|
+
const messages = useTramaMessages();
|
|
236
|
+
return (_jsx(Button, { variant: "outlined", ...props, loading: context?.busy ?? props.loading, disabled: props.disabled || context?.disabled || (context?.browseDisabled && !onPress), onPress: (event) => {
|
|
237
|
+
onPress?.(event);
|
|
238
|
+
context?.browse();
|
|
239
|
+
}, children: children ?? messages.browseFiles }));
|
|
240
|
+
}
|
|
241
|
+
export function FileUploadActions(props) {
|
|
242
|
+
return _jsx(Cluster, { ...props });
|
|
243
|
+
}
|
|
244
|
+
export function FileUploadItemMedia(props) {
|
|
245
|
+
return _jsx(View, { ...props });
|
|
246
|
+
}
|
|
247
|
+
function ClearFiles() {
|
|
248
|
+
const context = useContext(FileContext);
|
|
249
|
+
const messages = useTramaMessages();
|
|
250
|
+
return (_jsx(Button, { variant: "text", size: "sm", onPress: context?.clear, children: _jsx(TextContent, { children: messages.clearFiles }) }));
|
|
251
|
+
}
|