Gathering detailed insights and metrics for @ngx-smart-forms/smart-file-upload
Gathering detailed insights and metrics for @ngx-smart-forms/smart-file-upload
npm install @ngx-smart-forms/smart-file-upload
Typescript
Module System
Node Version
NPM Version
69
Supply Chain
96.5
Quality
76.9
Maintenance
100
Vulnerability
98.9
License
TypeScript (77.28%)
HTML (14.49%)
SCSS (4.19%)
JavaScript (3.72%)
CSS (0.32%)
Total Downloads
277
Last Day
1
Last Week
6
Last Month
28
Last Year
277
2 Stars
26 Commits
3 Forks
2 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.1
Package Id
@ngx-smart-forms/smart-file-upload@1.0.1
Unpacked Size
166.69 kB
Size
36.58 kB
File Count
17
NPM Version
10.1.0
Node Version
20.8.0
Publised On
14 Oct 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
6
Compared to previous week
Last month
47.4%
28
Compared to previous month
Last year
0%
277
Compared to previous year
1
4
The SmartFileUpload
component is an Angular library that simplifies file uploads with advanced validation, theming, and customization capabilities. It supports multiple file uploads, drag-and-drop functionality, and image previews, while integrating seamlessly with Angular forms. This component is designed to be highly customizable and can handle various file types, sizes, and image dimensions, all while letting the form control handle error messaging just like native Angular form validators.
accept
attribute.debounceTime
property.Install the @ngx-smart-forms/smart-file-upload
library using npm or yarn:
1npm install @ngx-smart-forms/smart-file-upload
Or using Yarn:
1yarn add @ngx-smart-forms/smart-file-upload
Check out a live interactive demo of the @ngx-smart-forms/smart-file-upload
library on StackBlitz:
You can also click here to open the project in a new tab.
To start using the SmartFileUpload
library in your Angular project, follow these steps:
1import { SmartFileUpload } from '@ngx-smart-forms/smart-file-upload'; 2import { ReactiveFormsModule, FormGroup, FormBuilder } from '@angular/forms'; 3 4@Component({ 5 standalone: true, 6 imports: [ReactiveFormsModule, SmartFileUpload], 7 templateUrl: './your-component.html', 8}) 9export class YourComponent { 10 form: FormGroup; 11 12 constructor(private fb: FormBuilder) { 13 this.form = this.fb.group({ 14 fileUpload: [null, []], // Add custom validators if needed 15 }); 16 } 17}
Here’s how you can use the SmartFileUpload
component in a form:
1<form [formGroup]="form"> 2 <smart-file-upload formControlName="fileUpload"></smart-file-upload> 3 4 <!-- Display errors --> 5 <div *ngIf="form.get('fileUpload')?.errors as errors"> 6 <div *ngIf="errors['maxSize']">File size should not exceed {{ errors['maxSize'].maxSize / 1024 / 1024 }} MB.</div> 7 <div *ngIf="errors['invalidType']">Invalid file type. Allowed: {{ errors['invalidType'].allowedTypes }}.</div> 8 </div> 9</form>
1<form #uploadForm="ngForm"> 2 <smart-file-upload 3 name="fileUpload" 4 [(ngModel)]="uploadedFiles" 5 accept="image/*,application/pdf" 6 [multiple]="true" 7 [maxFileSize]="5 * 1024 * 1024" 8 [maxFiles]="5" 9 [showPreview]="true" 10 ></smart-file-upload> 11 12 <!-- Display errors --> 13 <div *ngIf="uploadForm.controls['fileUpload']?.errors as errors"> 14 <div *ngIf="errors['maxFiles']">You can upload up to {{ errors['maxFiles'].maxFiles }} files.</div> 15 <div *ngIf="errors['invalidType']">Invalid file type. Allowed: {{ errors['invalidType'].allowedTypes }}.</div> 16 <div *ngIf="errors['maxWidth']">Image width should not exceed {{ errors['maxWidth'].maxWidth }}px.</div> 17 <div *ngIf="errors['maxHeight']">Image height should not exceed {{ errors['maxHeight'].maxHeight }}px.</div> 18 </div> 19</form> 20
To allow multiple file uploads, enable the multiple
attribute:
1<smart-file-upload formControlName="fileUpload" [multiple]="true"></smart-file-upload>
You can restrict file types using the accept
attribute and limit file sizes using maxFileSize
:
1<smart-file-upload 2 formControlName="fileUpload" 3 accept="image/*,application/pdf" 4 [maxFileSize]="5 * 1024 * 1024" 5></smart-file-upload>
This restricts uploads to images and PDFs and enforces a maximum file size of 5 MB.
To enforce maximum image dimensions, use the maxWidth
and maxHeight
properties:
1<smart-file-upload 2 formControlName="fileUpload" 3 [maxWidth]="1920" 4 [maxHeight]="1080" 5></smart-file-upload>
To display image previews for uploaded files, enable the showPreview
option:
1<smart-file-upload formControlName="fileUpload" [showPreview]="true"></smart-file-upload>
To avoid performing validation checks too frequently, especially useful when uploading large files or multiple files, you can control the debounce time using the debounceTime
property.
1<smart-file-upload 2 formControlName="fileUpload" 3 [multiple]="true" 4 [debounceTime]="300" 5 [maxFileSize]="5 * 1024 * 1024" 6 [showPreview]="true" 7></smart-file-upload>
To improve performance for large images, the component supports lazy loading of image previews. Previews are only loaded when the image enters the viewport. This is enabled by default.
1<smart-file-upload 2 formControlName="fileUpload" 3 [multiple]="true" 4 accept="image/*" 5 [showPreview]="true" 6 [lazyLoadPreviews]="true" 7></smart-file-upload>
The SmartFileUpload
component supports light, dark, and custom themes using CSS variables for flexible styling. You can apply predefined themes or customize the component’s look by overriding CSS variables.
Example: Dark Theme
1<smart-file-upload formControlName="fileUpload" theme="dark"></smart-file-upload>
Custom Theme Example
You can override CSS variables to create a custom theme:
1<smart-file-upload formControlName="fileUpload" theme="custom"></smart-file-upload>
1smart-file-upload { 2 --file-upload-border-color: #007bff; 3 --file-upload-background-color: #e9f7fd; 4 --file-upload-text-color: #007bff; 5}
Customizable CSS Variables
--file-upload-border-color
--file-upload-border-hover-color
--file-upload-background-color
--file-preview-background-color
--file-preview-border-color
--file-upload-text-color
--file-thumbnail-width
--file-thumbnail-height
--file-upload-file-name-color
--file-upload-file-size-color
--remove-btn-color
--remove-btn-hover-color
The SmartFileUpload
component is designed with accessibility in mind:
To capture events when files are selected or dragged into the component, you can use Angular event bindings:
1<smart-file-upload
2 formControlName="fileUpload"
3 (fileSelected)="onFileSelected($event)"
4 (dragOver)="onDragOver($event)"
5 (fileDrop)="onDrop($event)"
6></smart-file-upload>
Here are the main properties available for configuration:
Property | Type | Default Value | Description |
---|---|---|---|
accept | string | 'image/*' | Defines the allowed MIME types for file uploads (e.g., image/*,application/pdf ). |
multiple | boolean | false | Allows multiple files to be selected. |
maxFileSize | number | 5 * 1024 * 1024 | Maximum file size (in bytes) for each uploaded file. |
maxFiles | number | 10 | Maximum number of files allowed for upload when multiple is enabled. |
maxWidth | number | null | Maximum width for image files (optional). |
maxHeight | number | null | Maximum height for image files (optional). |
showPreview | boolean | false | Enables image previews for uploaded files. |
debounceTime | number | 0 | Time in milliseconds to debounce the file input validation. |
lazyLoadPreviews | boolean | true | Lazy load image previews for large image files. |
theme | 'light' | 'dark' | 'custom' | 'light' | Theming support for light, dark, or custom themes. |
You can define custom validators in your form group and apply them to the fileUpload
control:
1this.form = this.fb.group({ 2 fileUpload: [null, [Validators.required]], // Require at least one file to be uploaded 3});
The SmartFileUpload
library passes validation errors to the form control, allowing developers to define their own error messages in the template. The following error types are available:
maxFiles
: When the number of selected files exceeds the maxFiles
limit.maxSize
: When a file exceeds the maxFileSize
limit.invalidType
: When a file type doesn't match the accept
MIME types.maxWidth
: When an image exceeds the maxWidth
limit.maxHeight
: When an image exceeds the maxHeight
limit.invalidImage
: When the image is invalid or cannot be loaded.When uploading large files or multiple files, consider the following performance tips:
debounceTime
property to avoid running validation too frequently.If you encounter any issues or have questions, feel free to open an issue on GitHub.
We welcome contributions from the community! Please see the CONTRIBUTING.md for guidelines on how to contribute to the project.
This project is licensed under the MIT License - see the LICENSE file for more information.
No vulnerabilities found.
No security vulnerabilities found.