@octane-xplat/files 0.9.0 → 0.11.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/README.md +73 -20
- package/package.json +11 -4
- package/src/FileInput.linux.tsrx +1 -0
- package/src/FileInput.macos.tsrx +167 -0
- package/src/FileInput.tsrx +168 -0
- package/src/FileInput.web.test.tsx +99 -0
- package/src/FileInput.web.tsrx +240 -0
- package/src/file-bytes.test.ts +53 -0
- package/src/file-bytes.ts +68 -0
- package/src/file-input-utils.test.ts +22 -0
- package/src/file-input-utils.ts +113 -0
- package/src/files.bridge.test.ts +342 -0
- package/src/files.macos.ts +69 -7
- package/src/files.ts +350 -19
- package/src/files.web.test.ts +117 -0
- package/src/files.web.ts +156 -15
- package/src/index.macos.ts +9 -0
- package/src/index.ts +9 -0
- package/src/props.ts +52 -0
- package/src/types.ts +65 -1
- package/types/index.d.ts +22 -0
- package/types/index.macos.d.ts +21 -0
package/README.md
CHANGED
|
@@ -1,32 +1,85 @@
|
|
|
1
1
|
# `@octane-xplat/files`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
Cross-platform file selection and file entry for Octane apps.
|
|
4
|
+
|
|
5
|
+
Install `@octane-xplat/files` and `@octane-xplat/ui`. `FileInput` uses the
|
|
6
|
+
platform picker by default: the browser file input on web, the NativeScript
|
|
7
|
+
document picker on iOS/Android, and AppKit `NSOpenPanel` on macOS. No
|
|
8
|
+
app-registered picker is needed. The optional `pick` prop can replace the
|
|
9
|
+
default for a custom source such as cloud storage.
|
|
10
|
+
|
|
11
|
+
```tsrx
|
|
12
|
+
import { useState } from 'octane'
|
|
13
|
+
import { FileInput } from '@octane-xplat/files'
|
|
14
|
+
import type { FileInputFile } from '@octane-xplat/files'
|
|
15
|
+
|
|
16
|
+
export function Attachments() @{
|
|
17
|
+
const [files, setFiles] = useState<FileInputFile[] | null>([])
|
|
18
|
+
|
|
19
|
+
<FileInput
|
|
20
|
+
label="Attachments"
|
|
21
|
+
isMultiple
|
|
22
|
+
value={files}
|
|
23
|
+
onChange={(next) => setFiles(Array.isArray(next) ? next : next ? [next] : [])}
|
|
24
|
+
/>
|
|
25
|
+
}
|
|
12
26
|
```
|
|
13
27
|
|
|
28
|
+
The portable value is `{ name, uri, size?, mimeType? }`. On web, the selected
|
|
29
|
+
browser `File` is also available as `file`; its `uri` is a temporary object
|
|
30
|
+
URL. Native platforms return a local path or content URI, which apps should
|
|
31
|
+
retain as an opaque reference and pass to `files.readText` where supported.
|
|
32
|
+
AppKit provides native picking, but its current `readText` and `writeText`
|
|
33
|
+
methods are unsupported. Web file references should be released with
|
|
34
|
+
`files.release` when no longer needed.
|
|
35
|
+
|
|
14
36
|
```ts
|
|
15
37
|
import { files } from '@octane-xplat/files'
|
|
16
38
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
39
|
+
// Run in a .web.ts file: AppKit currently does not support readText.
|
|
40
|
+
const file = await files.pick('text/plain')
|
|
41
|
+
if (file) {
|
|
42
|
+
try {
|
|
43
|
+
console.log(await files.readText(file))
|
|
44
|
+
} finally {
|
|
45
|
+
files.release(file)
|
|
46
|
+
}
|
|
21
47
|
}
|
|
48
|
+
```
|
|
22
49
|
|
|
23
|
-
|
|
24
|
-
|
|
50
|
+
## Binary reads, writes, and export
|
|
51
|
+
|
|
52
|
+
`files.readBytes(ref, { maxBytes?, signal? })` reads the full contents as a
|
|
53
|
+
`Uint8Array`. It works on `content://` picks (read through Android's
|
|
54
|
+
`ContentResolver`), iOS picker copies, sandbox files, and web object URLs.
|
|
55
|
+
`maxBytes` rejects oversized files mid-read instead of loading them, and
|
|
56
|
+
`signal` aborts the read with an `AbortError`.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const bytes = await files.readBytes(file, { maxBytes: 10 * 1024 * 1024 })
|
|
25
60
|
```
|
|
26
61
|
|
|
27
|
-
`
|
|
28
|
-
|
|
29
|
-
|
|
62
|
+
`files.writeBytes(name, bytes)` mirrors `writeText`: on native it writes into
|
|
63
|
+
the app sandbox (Documents) and returns a `FileRef`; on web it starts a
|
|
64
|
+
browser download. `name` must be a bare file name — paths like `../x` or
|
|
65
|
+
`a/b` are rejected, and `writeText` enforces the same rule.
|
|
66
|
+
|
|
67
|
+
`files.export(name, bytes, { mimeType? })` asks the user where to save the
|
|
68
|
+
data and resolves `saved`, `cancelled`, or `unavailable`. `saved` means the
|
|
69
|
+
platform committed the bytes — Android flushes them through
|
|
70
|
+
`ContentResolver` to the picked document, iOS completes the export-as-copy
|
|
71
|
+
flow, and web closes a File System Access writable. Browsers without
|
|
72
|
+
`showSaveFilePicker` and the AppKit leaf report `unavailable`; a share-sheet
|
|
73
|
+
presentation never counts as `saved`. Only one export can run at a time — a
|
|
74
|
+
concurrent call rejects with an `ExportBusyError` rather than stealing the
|
|
75
|
+
open picker's result.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const result = await files.export('report.pdf', pdfBytes, { mimeType: 'application/pdf' })
|
|
79
|
+
if (result === 'cancelled') console.log('user dismissed')
|
|
80
|
+
```
|
|
30
81
|
|
|
31
|
-
|
|
32
|
-
|
|
82
|
+
Picked refs are only guaranteed readable within the picking session. Android
|
|
83
|
+
`content://` grants are not persisted by the picker, and iOS import-mode
|
|
84
|
+
copies live in a temp folder — copy what you need with `writeBytes`, and
|
|
85
|
+
call `files.release(ref)` when done.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octane-xplat/files",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Native + web file pick and read/write for Octane xplat — matching web + NativeScript implementations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"android",
|
|
@@ -22,12 +22,17 @@
|
|
|
22
22
|
"directory": "packages/files"
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
|
-
"src"
|
|
25
|
+
"src",
|
|
26
|
+
"types"
|
|
26
27
|
],
|
|
27
28
|
"type": "module",
|
|
28
29
|
"exports": {
|
|
29
30
|
".": {
|
|
30
|
-
"macos":
|
|
31
|
+
"macos": {
|
|
32
|
+
"types": "./types/index.macos.d.ts",
|
|
33
|
+
"default": "./src/index.macos.ts"
|
|
34
|
+
},
|
|
35
|
+
"types": "./types/index.d.ts",
|
|
31
36
|
"default": "./src/index.ts"
|
|
32
37
|
},
|
|
33
38
|
"./*": "./src/*"
|
|
@@ -37,17 +42,19 @@
|
|
|
37
42
|
},
|
|
38
43
|
"dependencies": {
|
|
39
44
|
"@nativescript-community/ui-document-picker": "1.1.29",
|
|
40
|
-
"@octane-xplat/platform": "0.
|
|
45
|
+
"@octane-xplat/platform": "0.11.0"
|
|
41
46
|
},
|
|
42
47
|
"devDependencies": {
|
|
43
48
|
"@nativescript-community/octane": "0.2.4",
|
|
44
49
|
"@nativescript/core": "9.1.2",
|
|
45
50
|
"@nativescript/types": "9.0.0",
|
|
51
|
+
"@octane-xplat/ui": "0.11.0",
|
|
46
52
|
"octane": "0.6.3"
|
|
47
53
|
},
|
|
48
54
|
"peerDependencies": {
|
|
49
55
|
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
50
56
|
"@nativescript/core": ">=9.1.0 <10",
|
|
57
|
+
"@octane-xplat/ui": ">=0.6.0 <1",
|
|
51
58
|
"octane": ">=0.6.3 <1"
|
|
52
59
|
},
|
|
53
60
|
"peerDependenciesMeta": {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FileInput } from './FileInput.web.tsrx'
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/** @jsxImportSource @octane-xplat/macos-renderer */
|
|
2
|
+
import { useRef, useImperativeHandle, useId, useState } from 'octane'
|
|
3
|
+
import { Field as FormField, Icon, Pressable, useFieldControl } from '@octane-xplat/ui'
|
|
4
|
+
import type { FileInputFile, FileInputProps } from './props'
|
|
5
|
+
import { files as fileService } from './files'
|
|
6
|
+
import { cx, formatFileSize, normalizePicked, validateFiles } from './file-input-utils'
|
|
7
|
+
|
|
8
|
+
const STATUS_TEXT = { error: '⚠', warning: '⚠', success: '✓' } as const
|
|
9
|
+
|
|
10
|
+
function FileInputControl(inputProps: FileInputProps) @{
|
|
11
|
+
const hostRef = useRef<any>(null);
|
|
12
|
+
const { props } = useFieldControl(inputProps)
|
|
13
|
+
const generatedId = useId()
|
|
14
|
+
const inputId = props.id ?? generatedId
|
|
15
|
+
|
|
16
|
+
const [busy, setBusy] = useState(false)
|
|
17
|
+
const [validationErrors, setValidationErrors] = useState<string[]>([])
|
|
18
|
+
|
|
19
|
+
const files: FileInputFile[] = props.value == null ? [] : Array.isArray(props.value) ? props.value : [props.value]
|
|
20
|
+
const isDisabled = !!props.isDisabled || !!props.isReadOnly
|
|
21
|
+
const isBusy = !!props.isLoading || busy
|
|
22
|
+
const statusType = validationErrors.length ? 'error' : props.status?.type
|
|
23
|
+
const statusMessage = validationErrors.length ? validationErrors.join(' ') : props.status?.message
|
|
24
|
+
|
|
25
|
+
// `onChange` mirrors `value`'s shape: array when `isMultiple`, single ref
|
|
26
|
+
// or null otherwise.
|
|
27
|
+
const emitValue = (next: FileInputFile[]): FileInputFile | FileInputFile[] | null =>
|
|
28
|
+
props.isMultiple ? next : (next[0] ?? null)
|
|
29
|
+
|
|
30
|
+
const fireChange = (next: FileInputFile[]) => {
|
|
31
|
+
const emitted = emitValue(next)
|
|
32
|
+
props.onChange?.(emitted)
|
|
33
|
+
if (props.changeAction) {
|
|
34
|
+
setBusy(true)
|
|
35
|
+
Promise.resolve(props.changeAction(emitted)).finally(() => setBusy(false))
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const acceptRefs = (picked: FileInputFile[]) => {
|
|
40
|
+
if (isDisabled || isBusy || !picked.length) return
|
|
41
|
+
const merged = props.isMultiple ? [...files, ...picked] : picked
|
|
42
|
+
const { valid, errors } = validateFiles(merged, {
|
|
43
|
+
accept: props.accept,
|
|
44
|
+
maxSize: props.maxSize,
|
|
45
|
+
maxFiles: props.maxFiles,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
setValidationErrors(errors)
|
|
49
|
+
const next = !props.isMultiple && errors.length ? files : valid
|
|
50
|
+
if (next.length !== files.length || next.some((file, index) => file.uri !== files[index]?.uri)) {
|
|
51
|
+
fireChange(next)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const removeAt = (index: number) => {
|
|
56
|
+
if (isDisabled || isBusy) return
|
|
57
|
+
setValidationErrors([])
|
|
58
|
+
fireChange(files.filter((_, i) => i !== index))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const pick = () => {
|
|
62
|
+
if (isDisabled || isBusy) return
|
|
63
|
+
const options = { accept: props.accept, multiple: props.isMultiple }
|
|
64
|
+
const picked = props.pick
|
|
65
|
+
? props.pick(options)
|
|
66
|
+
: props.isMultiple
|
|
67
|
+
? fileService.pickMultiple(props.accept)
|
|
68
|
+
: fileService.pick(props.accept)
|
|
69
|
+
|
|
70
|
+
Promise.resolve(picked)
|
|
71
|
+
.then((result) => acceptRefs(normalizePicked(result)))
|
|
72
|
+
.catch((error) => setValidationErrors([String(error?.message ?? error)]))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const dropzone = props.mode === 'dropzone'
|
|
76
|
+
const statusText = statusType ? STATUS_TEXT[statusType] : null
|
|
77
|
+
|
|
78
|
+
useImperativeHandle(props.ref, () => ({ open: pick, native: hostRef.current }), undefined);
|
|
79
|
+
|
|
80
|
+
<flexboxlayout
|
|
81
|
+
flexDirection="column"
|
|
82
|
+
className={cx(
|
|
83
|
+
'vx-fileinput',
|
|
84
|
+
dropzone && 'vx-fileinput--dropzone',
|
|
85
|
+
isDisabled && 'vx-fileinput--disabled',
|
|
86
|
+
isBusy && 'vx-fileinput--loading',
|
|
87
|
+
statusType && `vx-fileinput--status-${statusType}`,
|
|
88
|
+
props.size && `vx-fileinput--size-${props.size}`,
|
|
89
|
+
props.className,
|
|
90
|
+
)}
|
|
91
|
+
style={props.style}
|
|
92
|
+
>
|
|
93
|
+
{files.length ? (
|
|
94
|
+
<flexboxlayout flexDirection="column" className="vx-fileinput-list">
|
|
95
|
+
{files.map((f, i) => (
|
|
96
|
+
<flexboxlayout key={`${f.uri}-${i}`} flexDirection="row" alignItems="center" className="vx-fileinput-item">
|
|
97
|
+
<Icon name="xplat-upload" size={14} />
|
|
98
|
+
<label className="vx-fileinput-itemname" text={f.name} />
|
|
99
|
+
{f.size != null ? <label className="vx-fileinput-itemsize" text={formatFileSize(f.size)} /> : null}
|
|
100
|
+
{!isDisabled && !isBusy
|
|
101
|
+
? <Pressable className="vx-fileinput-itemremove" accessibilityLabel={`Remove ${f.name}`} onPress={() => removeAt(i)}><label text="×" /></Pressable>
|
|
102
|
+
: null}
|
|
103
|
+
</flexboxlayout>
|
|
104
|
+
))}
|
|
105
|
+
</flexboxlayout>
|
|
106
|
+
) : null}
|
|
107
|
+
<Pressable
|
|
108
|
+
ref={(v: any) => { hostRef.current = v; }}
|
|
109
|
+
id={inputId}
|
|
110
|
+
className="vx-fileinput-trigger"
|
|
111
|
+
disabled={isDisabled || isBusy}
|
|
112
|
+
accessibilityLabel={props.accessibilityLabel ?? props.ariaLabel ?? props.fieldLabel ?? props.label}
|
|
113
|
+
accessibilityHint={props.accessibilityHint}
|
|
114
|
+
accessibilityState={{ disabled: isDisabled, busy: isBusy } as any}
|
|
115
|
+
onPress={pick}
|
|
116
|
+
>
|
|
117
|
+
<flexboxlayout flexDirection="row" alignItems="center">
|
|
118
|
+
<Icon name="xplat-upload" size={14} />
|
|
119
|
+
<label text={files.length ? 'Choose more files' : 'Choose files'} />
|
|
120
|
+
</flexboxlayout>
|
|
121
|
+
</Pressable>
|
|
122
|
+
{files.length > 0 && !isDisabled && !isBusy
|
|
123
|
+
? <Pressable className="vx-fileinput-clear" accessibilityLabel={`Clear ${props.fieldLabel ?? props.label ?? 'files'}`} onPress={() => { setValidationErrors([]); fireChange([]) }}><label text="×" /></Pressable>
|
|
124
|
+
: null}
|
|
125
|
+
{statusText != null
|
|
126
|
+
? <label className={cx('vx-field-icon', `vx-field-icon--${statusType}`)} text={statusText} accessibilityLabel={props.statusVariant === 'tooltip' ? statusMessage : undefined} />
|
|
127
|
+
: null}
|
|
128
|
+
{statusMessage && props.statusVariant !== 'tooltip'
|
|
129
|
+
? <label className={cx('vx-field-status', `vx-field-status--${statusType}`)} text={statusMessage} />
|
|
130
|
+
: null}
|
|
131
|
+
</flexboxlayout>
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* File entry — delegates selection to this package's AppKit files service.
|
|
136
|
+
* Values are portable `{name, uri, size?, mimeType?}` refs. Drag/drop is not
|
|
137
|
+
* implemented on this leaf.
|
|
138
|
+
*/
|
|
139
|
+
export function FileInput(props: FileInputProps) @{
|
|
140
|
+
const { inField } = useFieldControl(props)
|
|
141
|
+
const cid = useId()
|
|
142
|
+
const control = <FileInputControl {...props} />
|
|
143
|
+
|
|
144
|
+
<>
|
|
145
|
+
{inField ? control : (
|
|
146
|
+
<FormField
|
|
147
|
+
inputID={props.id ?? cid}
|
|
148
|
+
label={props.label}
|
|
149
|
+
isLabelHidden={props.isLabelHidden}
|
|
150
|
+
description={props.description}
|
|
151
|
+
isDisabled={props.isDisabled}
|
|
152
|
+
disabledMessage={props.disabledMessage}
|
|
153
|
+
isReadOnly={props.isReadOnly}
|
|
154
|
+
isLoading={props.isLoading}
|
|
155
|
+
isRequired={props.isRequired}
|
|
156
|
+
isOptional={props.isOptional}
|
|
157
|
+
size={props.size}
|
|
158
|
+
status={props.status}
|
|
159
|
+
statusVariant={props.statusVariant}
|
|
160
|
+
labelTooltip={props.labelTooltip}
|
|
161
|
+
width={props.width}
|
|
162
|
+
>
|
|
163
|
+
{control}
|
|
164
|
+
</FormField>
|
|
165
|
+
)}
|
|
166
|
+
</>
|
|
167
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/** @jsxImportSource @nativescript-community/octane */
|
|
2
|
+
import { useRef, useImperativeHandle, useId, useState } from 'octane'
|
|
3
|
+
import { Field as FormField, Icon, Pressable, useFieldControl } from '@octane-xplat/ui'
|
|
4
|
+
import type { FileInputFile, FileInputProps } from './props'
|
|
5
|
+
import { files as fileService } from './files'
|
|
6
|
+
import { cx, formatFileSize, normalizePicked, validateFiles } from './file-input-utils'
|
|
7
|
+
|
|
8
|
+
const STATUS_TEXT = { error: '⚠', warning: '⚠', success: '✓' } as const
|
|
9
|
+
|
|
10
|
+
function FileInputControl(inputProps: FileInputProps) @{
|
|
11
|
+
const hostRef = useRef<any>(null);
|
|
12
|
+
const { props } = useFieldControl(inputProps)
|
|
13
|
+
const generatedId = useId()
|
|
14
|
+
const inputId = props.id ?? generatedId
|
|
15
|
+
|
|
16
|
+
const [busy, setBusy] = useState(false)
|
|
17
|
+
const [validationErrors, setValidationErrors] = useState<string[]>([])
|
|
18
|
+
|
|
19
|
+
const files: FileInputFile[] = props.value == null ? [] : Array.isArray(props.value) ? props.value : [props.value]
|
|
20
|
+
const isDisabled = !!props.isDisabled || !!props.isReadOnly
|
|
21
|
+
const isBusy = !!props.isLoading || busy
|
|
22
|
+
const statusType = validationErrors.length ? 'error' : props.status?.type
|
|
23
|
+
const statusMessage = validationErrors.length ? validationErrors.join(' ') : props.status?.message
|
|
24
|
+
|
|
25
|
+
// `onChange` mirrors `value`'s shape: array when `isMultiple`, single ref
|
|
26
|
+
// or null otherwise.
|
|
27
|
+
const emitValue = (next: FileInputFile[]): FileInputFile | FileInputFile[] | null =>
|
|
28
|
+
props.isMultiple ? next : (next[0] ?? null)
|
|
29
|
+
|
|
30
|
+
const fireChange = (next: FileInputFile[]) => {
|
|
31
|
+
const emitted = emitValue(next)
|
|
32
|
+
props.onChange?.(emitted)
|
|
33
|
+
if (props.changeAction) {
|
|
34
|
+
setBusy(true)
|
|
35
|
+
Promise.resolve(props.changeAction(emitted)).finally(() => setBusy(false))
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const acceptRefs = (picked: FileInputFile[]) => {
|
|
40
|
+
if (isDisabled || isBusy || !picked.length) return
|
|
41
|
+
const merged = props.isMultiple ? [...files, ...picked] : picked
|
|
42
|
+
const { valid, errors } = validateFiles(merged, {
|
|
43
|
+
accept: props.accept,
|
|
44
|
+
maxSize: props.maxSize,
|
|
45
|
+
maxFiles: props.maxFiles,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
setValidationErrors(errors)
|
|
49
|
+
const next = !props.isMultiple && errors.length ? files : valid
|
|
50
|
+
if (next.length !== files.length || next.some((file, index) => file.uri !== files[index]?.uri)) {
|
|
51
|
+
fireChange(next)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const removeAt = (index: number) => {
|
|
56
|
+
if (isDisabled || isBusy) return
|
|
57
|
+
setValidationErrors([])
|
|
58
|
+
fireChange(files.filter((_, i) => i !== index))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const pick = () => {
|
|
62
|
+
if (isDisabled || isBusy) return
|
|
63
|
+
const options = { accept: props.accept, multiple: props.isMultiple }
|
|
64
|
+
const picked = props.pick
|
|
65
|
+
? props.pick(options)
|
|
66
|
+
: props.isMultiple
|
|
67
|
+
? fileService.pickMultiple(props.accept)
|
|
68
|
+
: fileService.pick(props.accept)
|
|
69
|
+
|
|
70
|
+
Promise.resolve(picked)
|
|
71
|
+
.then((result) => acceptRefs(normalizePicked(result)))
|
|
72
|
+
.catch((error) => setValidationErrors([String(error?.message ?? error)]))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const dropzone = props.mode === 'dropzone'
|
|
76
|
+
const statusText = statusType ? STATUS_TEXT[statusType] : null
|
|
77
|
+
|
|
78
|
+
useImperativeHandle(props.ref, () => ({ open: pick, native: hostRef.current }), undefined);
|
|
79
|
+
|
|
80
|
+
<flexboxlayout
|
|
81
|
+
flexDirection="column"
|
|
82
|
+
className={cx(
|
|
83
|
+
'vx-fileinput',
|
|
84
|
+
dropzone && 'vx-fileinput--dropzone',
|
|
85
|
+
isDisabled && 'vx-fileinput--disabled',
|
|
86
|
+
isBusy && 'vx-fileinput--loading',
|
|
87
|
+
statusType && `vx-fileinput--status-${statusType}`,
|
|
88
|
+
props.size && `vx-fileinput--size-${props.size}`,
|
|
89
|
+
props.className,
|
|
90
|
+
)}
|
|
91
|
+
style={props.style}
|
|
92
|
+
>
|
|
93
|
+
{files.length ? (
|
|
94
|
+
<flexboxlayout flexDirection="column" className="vx-fileinput-list">
|
|
95
|
+
{files.map((f, i) => (
|
|
96
|
+
<flexboxlayout key={`${f.uri}-${i}`} flexDirection="row" alignItems="center" className="vx-fileinput-item">
|
|
97
|
+
<Icon name="xplat-upload" size={14} />
|
|
98
|
+
<label className="vx-fileinput-itemname" text={f.name} />
|
|
99
|
+
{f.size != null ? <label className="vx-fileinput-itemsize" text={formatFileSize(f.size)} /> : null}
|
|
100
|
+
{!isDisabled && !isBusy
|
|
101
|
+
? <Pressable className="vx-fileinput-itemremove" accessibilityLabel={`Remove ${f.name}`} onPress={() => removeAt(i)}><label text="×" /></Pressable>
|
|
102
|
+
: null}
|
|
103
|
+
</flexboxlayout>
|
|
104
|
+
))}
|
|
105
|
+
</flexboxlayout>
|
|
106
|
+
) : null}
|
|
107
|
+
<Pressable
|
|
108
|
+
ref={(v: any) => { hostRef.current = v; }}
|
|
109
|
+
id={inputId}
|
|
110
|
+
className="vx-fileinput-trigger"
|
|
111
|
+
disabled={isDisabled || isBusy}
|
|
112
|
+
accessibilityLabel={props.accessibilityLabel ?? props.ariaLabel ?? props.fieldLabel ?? props.label}
|
|
113
|
+
accessibilityHint={props.accessibilityHint}
|
|
114
|
+
accessibilityState={{ disabled: isDisabled, busy: isBusy } as any}
|
|
115
|
+
onPress={pick}
|
|
116
|
+
>
|
|
117
|
+
<flexboxlayout flexDirection="row" alignItems="center">
|
|
118
|
+
<Icon name="xplat-upload" size={14} />
|
|
119
|
+
<label text={files.length ? 'Choose more files' : 'Choose files'} />
|
|
120
|
+
</flexboxlayout>
|
|
121
|
+
</Pressable>
|
|
122
|
+
{files.length > 0 && !isDisabled && !isBusy
|
|
123
|
+
? <Pressable className="vx-fileinput-clear" accessibilityLabel={`Clear ${props.fieldLabel ?? props.label ?? 'files'}`} onPress={() => { setValidationErrors([]); fireChange([]) }}><label text="×" /></Pressable>
|
|
124
|
+
: null}
|
|
125
|
+
{statusText != null
|
|
126
|
+
? <label className={cx('vx-field-icon', `vx-field-icon--${statusType}`)} text={statusText} accessibilityLabel={props.statusVariant === 'tooltip' ? statusMessage : undefined} />
|
|
127
|
+
: null}
|
|
128
|
+
{statusMessage && props.statusVariant !== 'tooltip'
|
|
129
|
+
? <label className={cx('vx-field-status', `vx-field-status--${statusType}`)} text={statusMessage} />
|
|
130
|
+
: null}
|
|
131
|
+
</flexboxlayout>
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* File entry — `compact`/`dropzone` modes render the same tappable surface
|
|
136
|
+
* on iOS/Android (drag/drop has no touch equivalent). Values are
|
|
137
|
+
* `FileInputFile` `{name, uri, size?, mimeType?}`; native picking uses the
|
|
138
|
+
* platform file service exported by this package.
|
|
139
|
+
*/
|
|
140
|
+
export function FileInput(props: FileInputProps) @{
|
|
141
|
+
const { inField } = useFieldControl(props)
|
|
142
|
+
const cid = useId()
|
|
143
|
+
const control = <FileInputControl {...props} />
|
|
144
|
+
|
|
145
|
+
<>
|
|
146
|
+
{inField ? control : (
|
|
147
|
+
<FormField
|
|
148
|
+
inputID={props.id ?? cid}
|
|
149
|
+
label={props.label}
|
|
150
|
+
isLabelHidden={props.isLabelHidden}
|
|
151
|
+
description={props.description}
|
|
152
|
+
isDisabled={props.isDisabled}
|
|
153
|
+
disabledMessage={props.disabledMessage}
|
|
154
|
+
isReadOnly={props.isReadOnly}
|
|
155
|
+
isLoading={props.isLoading}
|
|
156
|
+
isRequired={props.isRequired}
|
|
157
|
+
isOptional={props.isOptional}
|
|
158
|
+
size={props.size}
|
|
159
|
+
status={props.status}
|
|
160
|
+
statusVariant={props.statusVariant}
|
|
161
|
+
labelTooltip={props.labelTooltip}
|
|
162
|
+
width={props.width}
|
|
163
|
+
>
|
|
164
|
+
{control}
|
|
165
|
+
</FormField>
|
|
166
|
+
)}
|
|
167
|
+
</>
|
|
168
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
3
|
+
import { act, createRoot } from 'octane'
|
|
4
|
+
import { FileInput } from './FileInput.web.tsrx'
|
|
5
|
+
|
|
6
|
+
vi.mock('@octane-xplat/ui', () => ({
|
|
7
|
+
Field: ({ children }: any) => children,
|
|
8
|
+
Icon: () => null,
|
|
9
|
+
Spinner: () => null,
|
|
10
|
+
Tooltip: ({ trigger }: any) => trigger,
|
|
11
|
+
useFieldControl: (props: any) => ({ props, inField: false }),
|
|
12
|
+
}))
|
|
13
|
+
|
|
14
|
+
const roots: any[] = []
|
|
15
|
+
function mount(jsx: any) {
|
|
16
|
+
const el = document.createElement('div')
|
|
17
|
+
document.body.append(el)
|
|
18
|
+
const root = createRoot(el)
|
|
19
|
+
roots.push(root)
|
|
20
|
+
act(() => root.render(jsx))
|
|
21
|
+
return el
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
for (const root of roots.splice(0)) {
|
|
26
|
+
act(() => root.unmount())
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
document.body.innerHTML = ''
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe('FileInput (web)', () => {
|
|
33
|
+
it('validates and emits portable refs from a custom source', async () => {
|
|
34
|
+
const change = vi.fn()
|
|
35
|
+
const el = mount(
|
|
36
|
+
<FileInput
|
|
37
|
+
value={null}
|
|
38
|
+
onChange={change}
|
|
39
|
+
isMultiple={true}
|
|
40
|
+
accept=".txt"
|
|
41
|
+
pick={() =>
|
|
42
|
+
Promise.resolve([
|
|
43
|
+
{ name: 'ok.txt', uri: 'blob:1', size: 10 },
|
|
44
|
+
{ name: 'no.exe', uri: 'blob:2', size: 10 },
|
|
45
|
+
])
|
|
46
|
+
}
|
|
47
|
+
/>,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
await act(async () => {
|
|
51
|
+
el.querySelector('.vx-fileinput-trigger')!.dispatchEvent(
|
|
52
|
+
new MouseEvent('click', { bubbles: true }),
|
|
53
|
+
)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
expect(change).toHaveBeenCalledWith([{ name: 'ok.txt', uri: 'blob:1', size: 10 }])
|
|
57
|
+
expect(el.querySelector('.vx-field-status')?.textContent).toContain('no.exe')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('replaces a single value even when the list length stays one', async () => {
|
|
61
|
+
const change = vi.fn()
|
|
62
|
+
const el = mount(
|
|
63
|
+
<FileInput
|
|
64
|
+
value={{ name: 'old.txt', uri: 'blob:old' }}
|
|
65
|
+
onChange={change}
|
|
66
|
+
pick={() => Promise.resolve({ name: 'new.txt', uri: 'blob:new' })}
|
|
67
|
+
/>,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
await act(async () => {
|
|
71
|
+
el.querySelector('.vx-fileinput-trigger')!.dispatchEvent(
|
|
72
|
+
new MouseEvent('click', { bubbles: true }),
|
|
73
|
+
)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
expect(change).toHaveBeenCalledWith({ name: 'new.txt', uri: 'blob:new' })
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('retains the current file when a replacement fails validation', async () => {
|
|
80
|
+
const change = vi.fn()
|
|
81
|
+
const el = mount(
|
|
82
|
+
<FileInput
|
|
83
|
+
value={{ name: 'old.txt', uri: 'blob:old' }}
|
|
84
|
+
onChange={change}
|
|
85
|
+
accept=".txt"
|
|
86
|
+
pick={() => Promise.resolve({ name: 'new.exe', uri: 'blob:new' })}
|
|
87
|
+
/>,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
await act(async () => {
|
|
91
|
+
el.querySelector('.vx-fileinput-trigger')!.dispatchEvent(
|
|
92
|
+
new MouseEvent('click', { bubbles: true }),
|
|
93
|
+
)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
expect(change).not.toHaveBeenCalled()
|
|
97
|
+
expect(el.querySelector('.vx-field-status')?.textContent).toContain('new.exe')
|
|
98
|
+
})
|
|
99
|
+
})
|