1- import { observable } from 'mobx' ;
1+ import { computed , observable } from 'mobx' ;
22import {
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 ) ;
0 commit comments