Skip to content

Commit 0493875

Browse files
committed
♻️ Move fallback file-input to own modules
1 parent 4be1686 commit 0493875

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/react-chayns-file_input/component/FileInput.jsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import PropTypes from 'prop-types';
44
import classNames from 'classnames';
55
import { faUpload } from '@fortawesome/free-solid-svg-icons/faUpload';
66
import Icon from '../../react-chayns-icon/component/Icon';
7+
import supportsFileInput from '../utils/supportsFileInput';
8+
import fileInputCall from '../utils/fileInputCall';
79

810
export default class FileInput extends PureComponent {
911
static types = {
@@ -78,7 +80,7 @@ export default class FileInput extends PureComponent {
7880
super(props);
7981
this.itemRefs = [];
8082
this.fileInputRefs = [];
81-
this.needAppCall = (chayns.env.isApp || chayns.env.isMyChaynsApp) && chayns.env.isAndroid && chayns.env.appVersion < 6000;
83+
this.needAppCall = supportsFileInput();
8284
}
8385

8486
onDragEnter = (event, item, index) => {
@@ -123,15 +125,7 @@ export default class FileInput extends PureComponent {
123125
if (stopPropagation) event.stopPropagation();
124126
if (typeof item.onClick === 'function') item.onClick(event);
125127
if (this.needAppCall && item.onChange) {
126-
const uploadResult = await chayns.uploadCloudImage();
127-
const type = uploadResult.url.match(/(\.[a-z]+)/g)[0];
128-
const response = await fetch(uploadResult.url);
129-
const data = await response.blob();
130-
const metadata = {
131-
type: `image/${type}`,
132-
};
133-
const file = new File([data], `androidCompatibilityUpload${type}`, metadata);
134-
const compatibilityEvent = { target: { files: [file] } };
128+
const compatibilityEvent = await fileInputCall();
135129
this.onChange(compatibilityEvent, item, index);
136130
}
137131
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default async function fileInputCall() {
2+
const uploadResult = await chayns.uploadCloudImage();
3+
const type = uploadResult.url.match(/(\.[a-z]+)/g)[0];
4+
const response = await fetch(uploadResult.url);
5+
const data = await response.blob();
6+
const metadata = {
7+
type: `image/${type}`,
8+
};
9+
const file = new File([data], `androidCompatibilityUpload${type}`, metadata);
10+
11+
return { target: { files: [file] } }; // compatibility event
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function supportsFileInput() {
2+
return !((chayns.env.isApp || chayns.env.isMyChaynsApp) && chayns.env.isAndroid && chayns.env.appVersion < 6000);
3+
}

0 commit comments

Comments
 (0)