@octane-xplat/files 0.0.1 → 0.10.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 +47 -2
- package/package.json +68 -10
- 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-input-utils.test.ts +22 -0
- package/src/file-input-utils.ts +113 -0
- package/src/files.linux.ts +2 -0
- package/src/files.macos.ts +59 -0
- package/src/files.ts +141 -0
- package/src/files.web.ts +75 -0
- package/src/index.macos.ts +11 -0
- package/src/index.ts +11 -0
- package/src/props.ts +52 -0
- package/src/types.ts +9 -0
- package/types/index.d.ts +15 -0
- package/types/index.macos.d.ts +14 -0
- package/index.d.ts +0 -1
- package/index.js +0 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alec Larson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,48 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@octane-xplat/files`
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
}
|
|
26
|
+
```
|
|
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
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { files } from '@octane-xplat/files'
|
|
38
|
+
|
|
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
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
package/package.json
CHANGED
|
@@ -1,19 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octane-xplat/files",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Native + web file pick and read/write for Octane xplat — matching web + NativeScript implementations",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"android",
|
|
7
|
+
"cross-platform",
|
|
8
|
+
"document-picker",
|
|
9
|
+
"file-picker",
|
|
10
|
+
"files",
|
|
11
|
+
"ios",
|
|
12
|
+
"nativescript",
|
|
13
|
+
"octane",
|
|
14
|
+
"web",
|
|
15
|
+
"xplat"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/files",
|
|
5
18
|
"license": "MIT",
|
|
6
19
|
"repository": {
|
|
7
20
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/octane-xplat/octane-xplat
|
|
21
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
9
22
|
"directory": "packages/files"
|
|
10
23
|
},
|
|
11
|
-
"homepage": "https://github.com/octane-xplat/octane-xplat/tree/main/packages/files",
|
|
12
|
-
"main": "./index.js",
|
|
13
|
-
"types": "./index.d.ts",
|
|
14
24
|
"files": [
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
"src",
|
|
26
|
+
"types"
|
|
27
|
+
],
|
|
28
|
+
"type": "module",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"macos": {
|
|
32
|
+
"types": "./types/index.macos.d.ts",
|
|
33
|
+
"default": "./src/index.macos.ts"
|
|
34
|
+
},
|
|
35
|
+
"types": "./types/index.d.ts",
|
|
36
|
+
"default": "./src/index.ts"
|
|
37
|
+
},
|
|
38
|
+
"./*": "./src/*"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@nativescript-community/ui-document-picker": "1.1.29",
|
|
45
|
+
"@octane-xplat/platform": "0.10.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@nativescript-community/octane": "0.2.4",
|
|
49
|
+
"@nativescript/core": "9.1.2",
|
|
50
|
+
"@nativescript/types": "9.0.0",
|
|
51
|
+
"@octane-xplat/ui": "0.10.0",
|
|
52
|
+
"octane": "0.6.3"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@nativescript-community/octane": ">=0.2.1 <1",
|
|
56
|
+
"@nativescript/core": ">=9.1.0 <10",
|
|
57
|
+
"@octane-xplat/ui": ">=0.6.0 <1",
|
|
58
|
+
"octane": ">=0.6.3 <1"
|
|
59
|
+
},
|
|
60
|
+
"peerDependenciesMeta": {
|
|
61
|
+
"@nativescript-community/octane": {
|
|
62
|
+
"optional": true
|
|
63
|
+
},
|
|
64
|
+
"@nativescript/core": {
|
|
65
|
+
"optional": true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"nativescript": {
|
|
69
|
+
"platforms": {
|
|
70
|
+
"android": "9.1.0",
|
|
71
|
+
"ios": "9.1.0"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"scripts": {
|
|
75
|
+
"test:packed": "node tests/packed-consumer.mjs"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -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
|
+
})
|