npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-formly-material-file

v1.0.0

Published

material design file upload for ngx-formly

Downloads

34

Readme

NgxFormlyMaterialFile

This library provides a material design file upload for ngx-formly. ngx-formly-material-file supports drag and drop and ships with various validators.

ngx-formly-material-file demo

Installation

Install NPM Dependencies

Follow the quick-start for ngx-formly and use @ngx-formly/material as the UI library. Finally run npm i ngx-formly-material-file.

Icons

ngx-formly-material-file uses material icons as a default. Add the following entry to the assets array in your angular.json to serve the icons:

{ 
   "glob":"**/*",
   "input":"./node_modules/ngx-formly-material-file/assets/svgs",
   "output":"/assets/svgs/"
}

Configure the App Module

...
import { LOCALE_ID, NgModule } from '@angular/core';
import { FileTypeComponent, FileTypeModule, FileTypeValidationMessages } from 'ngx-formly-material-file';
...

export const APP_LOCALE_ID = 'en-US';

@NgModule({
  imports: [
    ...
    FileTypeModule.forRoot(),
    FormlyModule.forRoot({
      validationMessages: new FileTypeValidationMessages(APP_LOCALE_ID).validationMessages,
      types: [
        { name: 'file', component: FileTypeComponent },
      ],
    })
    ...
  ],
  ...
  providers: [{ provide: LOCALE_ID, useValue: APP_LOCALE_ID }, ...],
})
export class AppModule {}

Use FileTypeComponent

ngx-formly-material-file adds an array of SelectedFile to the form model.

export interface SelectedFile {

  file: File;

  location?: string;

}

If you specify a uploadUrl in the templateOptions, the files will be uploaded using a FormData POST request. The default parameter name is file but it can be changed by setting paramName in the templateOptions. If the server returns a Location header, it will be present in the SelectedFile.

import { Component } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig, FormlyFormOptions } from '@ngx-formly/core';
import { FileListValidators, FileValidators } from 'ngx-formly-material-file';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {

  form = new FormGroup({});

  model: any = {};

  options: FormlyFormOptions = {};

  fields: FormlyFieldConfig[] = [
    {
      key: 'pictures',
      type: 'file',
      templateOptions: {
        label: 'Pictures',
        description: 'Upload some nice pictures',
        required: true,
        uploadUrl: '/upload'
      },
      validators: {
        validation: [
          FileListValidators.minFiles(2),
          FileListValidators.maxFiles(4),
          FileListValidators.totalFilesize(400 * 1000)
        ]
      },
      fieldArray: {
        validators: {
          validation: [
            FileValidators.minFilenameLength(1),
            FileValidators.maxFilenameLength(50),
            FileValidators.fileExtension(['pdf', 'txt', 'png']),
            FileValidators.filesize(1000 * 1000),
            FileValidators.filenameForbiddenCharacters(['/'])
          ]
        }
      }
    },
  ];

}

Customization

Icons

You can replace the default material icons by using the MatIconRegistry. Here is an example:

...
export class AppModule {
  constructor(matIconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
    matIconRegistry.addSvgIconInNamespace('fileType', 'fileDrop', sanitizer.bypassSecurityTrustResourceUrl('assets/svgs/solid/file-import.svg'));
    matIconRegistry.addSvgIconInNamespace('fileType', 'file', sanitizer.bypassSecurityTrustResourceUrl('assets/svgs/solid/file.svg'));
    matIconRegistry.addSvgIconInNamespace('fileType', 'fileUpload', sanitizer.bypassSecurityTrustResourceUrl('assets/svgs/solid/file-upload.svg'));
    matIconRegistry.addSvgIconInNamespace('fileType', 'fileRemove', sanitizer.bypassSecurityTrustResourceUrl('assets/svgs/solid/times.svg'));
  }
}

Validation messages

The default validation messages can be overwritten as shown in this example:

...
import { LOCALE_ID, NgModule } from '@angular/core';
import { FileTypeComponent, FileTypeModule, FileTypeValidationMessages } from 'ngx-formly-material-file';
...

export const APP_LOCALE_ID = 'en-US';

@NgModule({
  imports: [
    ...
    FileTypeModule.forRoot(),
    FormlyModule.forRoot({
      validationMessages: new FileTypeValidationMessages(APP_LOCALE_ID).validationMessages.concat([
        { name: 'maxFilenameLength', message: 'my custom message' },
        { name: 'minFilenameLength', message: 'my custom message' },
        { name: 'fileExtension', message: 'my custom message' },
        { name: 'filesize', message: '2do' },
        { name: 'filenameForbiddenCharacters', message: 'my custom message' },
        { name: 'minFiles', message: 'my custom message' },
        { name: 'maxFiles', message: '2do' },
        { name: 'totalFilesize', message: 'my custom message' },
        { name: 'uploadError', message: 'my custom message' }
      ]),
      types: [
        { name: 'file', component: FileTypeComponent },
      ],
    })
    ...
  ],
  ...
  providers: [{ provide: LOCALE_ID, useValue: APP_LOCALE_ID }, ...],
})
export class AppModule {}

Labels

The labels of the dropzone and the button as well as the tooltip of the remove-file-icon can be changed:

FileTypeModule.forRoot({
  dropzoneText: 'my custom label',
  browseFilesButtonText: 'my custom label',
  removeFileTooltip: 'my custom label'
})