Skip to content

Commit d8079a3

Browse files
committed
[fix] CSS & Type of File components (fix #28)
1 parent c3b4900 commit d8079a3

File tree

5 files changed

+58
-24
lines changed

5 files changed

+58
-24
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "boot-cell",
3-
"version": "2.0.0-rc.11",
3+
"version": "2.0.0-rc.13",
44
"license": "LGPL-3.0",
55
"author": "[email protected]",
66
"description": "Web Components UI library based on WebCell v3, BootStrap v5, BootStrap Icon v1 & FontAwesome v6",

source/Form/FilePicker.tsx

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { observable } from 'mobx';
1+
import { computed, observable } from 'mobx';
22
import {
33
attribute,
44
component,
@@ -32,10 +32,26 @@ export class FilePicker
3232
@observable
3333
accessor multiple = false;
3434

35-
handleAdd = (event: Event) => {
36-
const file = (event.currentTarget as HTMLInputElement).files?.[0];
35+
@observable
36+
accessor file: File | undefined;
37+
38+
@computed
39+
get fileType() {
40+
const { accept, file } = this;
41+
42+
return file?.type || file?.name.match(/\.\w+$/)?.[0] || accept;
43+
}
44+
45+
connectedCallback() {
46+
this.classList.add('d-block');
47+
this.style.width = '10rem';
48+
this.style.height = '10rem';
49+
}
50+
51+
handleAdd = ({ currentTarget }: Event) => {
52+
const file = (currentTarget as HTMLInputElement).files?.[0];
3753

38-
this.value = file ? URL.createObjectURL(file) : '';
54+
this.value = (this.file = file) ? URL.createObjectURL(file) : '';
3955

4056
this.emit('change', { value: this.value, file });
4157
};
@@ -70,14 +86,14 @@ export class FilePicker
7086
}
7187

7288
renderContent() {
73-
const { value, accept } = this;
89+
const { value, fileType } = this;
7490

7591
return (
76-
<div className="form-control position-relative">
92+
<div className="d-inline-block w-100 h-100 border rounded position-relative">
7793
{value ? (
7894
<FilePreview
7995
className="w-100 h-100 object-fit-contain"
80-
type={accept}
96+
type={fileType}
8197
path={value + ''}
8298
/>
8399
) : (
@@ -104,6 +120,10 @@ export class FilePicker
104120
rel="stylesheet"
105121
href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css"
106122
/>
123+
<link
124+
rel="stylesheet"
125+
href="https://unpkg.com/[email protected]/font/bootstrap-icons.min.css"
126+
/>
107127
{this.renderContent()}
108128
</>
109129
);

source/Form/FileUploader.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,20 @@ export abstract class FileModel {
6464
}
6565
}
6666

67-
export interface FileUploaderProps extends WebCellProps<HTMLInputElement> {
67+
export interface WebFileFieldProps
68+
extends Omit<WebCellProps<HTMLInputElement>, 'defaultValue' | 'value'> {
69+
defaultValue?: string | string[];
70+
value?: string | string[];
71+
}
72+
73+
export interface FileUploaderProps extends WebFileFieldProps {
6874
store: FileModel;
6975
}
7076

7177
export interface WebFileField
72-
extends Omit<WebField<FileUploaderProps>, 'defaultValue' | 'value'> {
73-
defaultValue?: string | string[];
74-
value?: string | string[];
78+
extends Pick<FileUploaderProps, 'defaultValue' | 'value'>,
79+
Omit<WebField<FileUploaderProps>, 'defaultValue' | 'value' | 'props'> {
80+
props: FileUploaderProps;
7581
}
7682

7783
export interface FileUploader extends WebFileField {}
@@ -176,7 +182,6 @@ export class FileUploader extends HTMLElement implements WebFileField {
176182
>
177183
<FilePicker
178184
accept={accept}
179-
style={{ height: '10rem' }}
180185
value={file}
181186
onChange={this.handleChange(file)}
182187
/>

source/Media/FilePreview.tsx

+19-6
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,31 @@ export const FileTypeMap = {
2323
sheet: 'xlsx'
2424
};
2525

26-
export const FilePreview: FC<FilePreviewProps> = ({ type, path, ...props }) => {
26+
export const FilePreview: FC<FilePreviewProps> = ({
27+
className = '',
28+
type,
29+
path,
30+
...props
31+
}) => {
2732
const [category, ...kind] = type?.split(/\W+/) || [],
2833
fileName = new URL(path, 'http://localhost').pathname.split('/').at(-1);
29-
30-
const extension = FileTypeMap[kind.at(-1)] || fileName?.split('.').at(-1);
34+
const extension =
35+
FileTypeMap[kind.at(-1)] ||
36+
(fileName?.includes('.') ? fileName.split('.').at(-1) : kind.at(-1));
3137

3238
return category === 'image' ? (
33-
<ImagePreview fluid loading="lazy" src={path} {...props} />
39+
<ImagePreview
40+
className={className}
41+
fluid
42+
loading="lazy"
43+
src={path}
44+
{...props}
45+
/>
3446
) : category === 'audio' ? (
35-
<audio controls src={path} {...props} />
47+
<audio className={className} controls src={path} {...props} />
3648
) : category === 'video' ? (
3749
<video
50+
className={className}
3851
muted
3952
src={path}
4053
onMouseEnter={({ currentTarget }) =>
@@ -47,7 +60,7 @@ export const FilePreview: FC<FilePreviewProps> = ({ type, path, ...props }) => {
4760
/>
4861
) : (
4962
<a
50-
className="d-inline-block px-3 py-2"
63+
className={`d-inline-flex justify-content-center align-items-center ${className}`}
5164
download={fileName}
5265
href={path}
5366
{...props}

test/index.tsx

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ const Content = () => (
3939
<section>
4040
<h2>File Picker</h2>
4141

42-
<FilePicker
43-
name="file"
44-
accept="image/*"
45-
onChange={console.log}
46-
/>
42+
<FilePicker name="file" onChange={console.log} />
4743
</section>
4844

4945
<h2>File Uploader</h2>

0 commit comments

Comments
 (0)