@availity/mui-file-selector 0.1.3 → 0.2.1

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.
@@ -1,82 +0,0 @@
1
- import { ChangeEvent, RefObject, useState } from 'react';
2
- import { Input } from '@availity/mui-form-utils';
3
-
4
- const idCounter = () => {
5
- let id = 0;
6
- const increment = () => (id += 1);
7
- const generateId = () => {
8
- return `filepicker-${increment()}`;
9
- };
10
- return {
11
- generateId,
12
- };
13
- };
14
-
15
- const counter = idCounter();
16
-
17
- const inputSx = { display: 'none' };
18
-
19
- export type FilePickerInputProps = {
20
- /** Identifies the field and matches the validation schema. */
21
- name: string;
22
- /** The file types you want to restrict uploading to. eg: ['.jpeg', '.jpg']. */
23
- allowedFileTypes?: `.${string}`[];
24
- /** id passed to the input component. It is randomly generated if not passed */
25
- id?: string;
26
- /** ref passed to the input component */
27
- inputRef?: RefObject<HTMLInputElement>;
28
- /** The maximum file size (in bytes) for a file to be uploaded. */
29
- maxSize?: number;
30
- /** Indicates that the user will be allowed to select multiple files when selecting files from the OS prompt. */
31
- multiple?: boolean;
32
- /** Callback when the user has selected a file or multiple files. */
33
- onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
34
- };
35
-
36
- export const FilePickerInput = ({
37
- allowedFileTypes,
38
- id,
39
- inputRef,
40
- maxSize,
41
- multiple,
42
- name,
43
- onChange,
44
- }: FilePickerInputProps) => {
45
- const [stateId] = useState(counter.generateId());
46
-
47
- const handleOnChange = (event: ChangeEvent<HTMLInputElement>) => {
48
- const { files } = event.target;
49
-
50
- // TODO: get size of file and compare to maxSize
51
-
52
- const value: File[] = [];
53
- if (files) {
54
- // FileList is not iterable. Must use for loop for now
55
- for (let i = 0; i < files.length; i++) {
56
- if (maxSize) {
57
- console.log('file is too big:', files[i].size > maxSize);
58
- }
59
- value[i] = files[i];
60
- }
61
- }
62
-
63
- if (onChange) onChange(event);
64
- };
65
-
66
- const inputId = id || stateId;
67
-
68
- return (
69
- <Input
70
- inputRef={inputRef}
71
- sx={inputSx}
72
- value=""
73
- type="file"
74
- inputProps={{
75
- accept: Array.isArray(allowedFileTypes) && allowedFileTypes.length > 0 ? allowedFileTypes.join(',') : undefined,
76
- multiple,
77
- }}
78
- id={inputId}
79
- onChange={handleOnChange}
80
- />
81
- );
82
- };
@@ -1,70 +0,0 @@
1
- import { Button } from 'reactstrap';
2
- import Icon from '@availity/icon';
3
- import type Upload from '@availity/upload-core';
4
-
5
- import { UploadProgressBar } from './UploadProgressBar';
6
-
7
- const FILE_EXT_ICONS = {
8
- png: 'file-image',
9
- jpg: 'file-image',
10
- jpeg: 'file-image',
11
- gif: 'file-image',
12
- ppt: 'file-powerpoint',
13
- pptx: 'file-powerpoint',
14
- xls: 'file-excel',
15
- xlsx: 'file-excel',
16
- doc: 'file-word',
17
- docx: 'file-word',
18
- txt: 'doc-alt',
19
- text: 'doc-alt',
20
- zip: 'file-archive',
21
- '7zip': 'file-archive',
22
- xml: 'file-code',
23
- html: 'file-code',
24
- pdf: 'file-pdf',
25
- } as const;
26
-
27
- type FileExtensionKey = keyof typeof FILE_EXT_ICONS;
28
-
29
- const isValidKey = (key: string): key is FileExtensionKey => {
30
- return key ? key in FILE_EXT_ICONS : false;
31
- };
32
-
33
- export type FileRowProps = {
34
- upload: Upload;
35
- onRemoveFile: (id: string) => void;
36
- };
37
-
38
- export const FileRow = ({ upload, onRemoveFile }: FileRowProps) => {
39
- const remove = () => {
40
- onRemoveFile(upload.id);
41
- };
42
-
43
- const ext = upload.file.name.split('.').pop()?.toLowerCase() || '';
44
- const icon = isValidKey(ext) ? FILE_EXT_ICONS[ext] : 'doc';
45
- const extLabel = ext.toUpperCase();
46
-
47
- return (
48
- <tr>
49
- <td className="align-middle" style={{ width: '10%' }}>
50
- <Icon name={icon} title={`${extLabel} File Icon`}>
51
- <span className="sr-only">{extLabel} File Icon</span>
52
- </Icon>{' '}
53
- </td>
54
- <td className="align-middle" style={{ width: '35%' }}>
55
- <div className="text-truncate" title={upload.file.name}>
56
- {upload.file.name}
57
- </div>
58
- </td>
59
- <td className="align-middle" style={{ width: '45%' }}>
60
- <UploadProgressBar upload={upload} aria-label={`${upload.file.name} upload`} />
61
- </td>
62
- <td className="align-middle" style={{ width: '10%' }}>
63
- <Button data-testid="remove-file-btn" color="link" className="text-danger px-0" onClick={remove}>
64
- <Icon name="trash-empty" />
65
- <span className="sr-only">Remove {upload.file.name}</span>
66
- </Button>
67
- </td>
68
- </tr>
69
- );
70
- };