13.0.0 • Published 25 days ago

@mhernandez-easygo/ngx-input-file v13.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
25 days ago

ngx-input-file

ngx-input-file is a module to replace the html element input file with Material Design.

Try it with Stackblitz!

For the previous version with bootstrap: ngx-input-file@1.0.4.

Key features

  • Preview of the file
  • Drag and drop zone
  • Responsive

Installation

npm install ngx-input-file --save

Basic Configuration

import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { InputFileConfig, InputFileModule } from 'ngx-input-file';

const config: InputFileConfig = {};

@NgModule({
    imports: [
        ...
        BrowserAnimationsModule,
        InputFileModule.forRoot(config),
        ...
    ],
    ...
})

export class MyModule {}

Please include material-design-icons in your angular.json:

"styles": [
    "node_modules/material-design-icons/iconfont/material-icons.css",
    "src/styles.scss"
]

Component Attributes

These settings will overide the configuration defined with forRoot() method.

AttributeTypeDescription
fileAcceptInput - stringThe attribute accept of the html element input.
fileLimitInput - numberThe maximum files that the user can upload.
iconAddInput - stringThe icon for add.
iconDeleteInput - stringThe icon for delete.
iconFileInput - stringThe icon for file.
iconLinkInput - stringThe icon for link.
linkEnabledInput - booleanWhether adding is url is enabled.
placeholderLinkInput - stringThe placeholder for the link input.
sizeLimitInput - numberThe maximum size of the file (kB).
disabledInput - booleanWhether the component is disabled.
placeholderInput - stringThe placeholder of the component.
classAnimationInput - stringThe class of the image container which allow to animate the container when select or drop a file.
ngModel/formControlnameArrayTemplate driven or reactive form works.
acceptedFileOutput - InputFileTriggered when a file is accepted.
deletedFileOutput - InputFileTriggered when a file is deleted.
rejectedFileOutput - InputFileTriggered when a file is rejected.

Configuration Attributes

AttributeTypeDefaultDescription
classAnimationstring'bounce-in'The class of the image container which allow to animate the container when select or drop a file.
fileAcceptstring'*'The attribute accept of the html element input.
fileLimitnumber1The maximum files that the user can upload.
iconAddstring'add'The icon for add.
iconDeletestring'delete'The icon for add.
iconFilestring'insert_drive_file'The icon for file.
iconLinkstring'link'The icon for link.
linkEnabledbooleanfalseWhether adding is url is enabled.
placeholderLinkstring'Link'The placeholder for the link input.
sizeLimitnumbernullThe maximum size of the file (kB).

Example

<input-file
    placeholder="My files"
    [(ngModel)]="myModel">
</input-file>

<input-file
    placeholder="Pictures"
    formControlName="myField">
</input-file> 

Bonus

Here's an example to post a file:

import { HttpClient } from '@angular/common/http';

@Injectable()
export class MyRepository {

constructor(
    private http: HttpClient
) {}

public post(file: InputFile): Observable<YourClass> {
    const apiUrl = 'my-url';
    const formData = new FormData();
    formData.append('file', file.file, file.file.name);
    return this.http.post<YourClass>(apiUrl, formData).pipe(
        .catchError(...)
}

For developpers

You're welcome, please fork this repository to a make pull request.

Demonstration

Clone this repository and run npm start.