All Projects → ngneat → Bind Query Params

ngneat / Bind Query Params

Licence: mit
Sync URL Query Params with Angular Form Controls

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Bind Query Params

Boltforms
Bolt Forms extension - Symfony interface and API for Bolt
Stars: ✭ 53 (-27.4%)
Mutual labels:  forms
Customui
Library to create custom UI's in MCPE 1.2+
Stars: ✭ 60 (-17.81%)
Mutual labels:  forms
Syncchanges
Synchronize/Replicate database changes using SQL Server Change Tracking
Stars: ✭ 66 (-9.59%)
Mutual labels:  synchronization
Poosh
🌎 Publish local files to virtually any remote endpoint (e.g. AWS S3)
Stars: ✭ 55 (-24.66%)
Mutual labels:  synchronization
Syncthing Macos
Frugal and native macOS Syncthing application bundle
Stars: ✭ 1,096 (+1401.37%)
Mutual labels:  synchronization
Ciruela
A peer-to-peer synchronization software for servers in datacenters.
Stars: ✭ 61 (-16.44%)
Mutual labels:  synchronization
Express Fileupload
Simple express file upload middleware that wraps around busboy
Stars: ✭ 1,069 (+1364.38%)
Mutual labels:  forms
Formst
Model-driven Form library for React
Stars: ✭ 69 (-5.48%)
Mutual labels:  forms
React Router Form
<Form> is to <form> as <Link> is to <a>
Stars: ✭ 58 (-20.55%)
Mutual labels:  forms
Mform
Spielend einfach umfangreiche Modul-Input-Formulare erzeugen.
Stars: ✭ 65 (-10.96%)
Mutual labels:  forms
Laravel Casts
Form builder for Laravel.
Stars: ✭ 56 (-23.29%)
Mutual labels:  forms
Awesomevalidation
Android validation library which helps developer boil down the tedious work to three easy steps.
Stars: ✭ 1,093 (+1397.26%)
Mutual labels:  forms
Zaloha.sh
Small and simple directory synchronizer (a BASH script)
Stars: ✭ 63 (-13.7%)
Mutual labels:  synchronization
Rff Cli Example
An example of how to use 🏁 React Final Form in a CLI application with Ink
Stars: ✭ 55 (-24.66%)
Mutual labels:  forms
Ng Dynamic Forms
Rapid form development library for Angular
Stars: ✭ 1,146 (+1469.86%)
Mutual labels:  forms
Vynchronize
Watch videos with friends online with the new real time video synchronization platform
Stars: ✭ 1,072 (+1368.49%)
Mutual labels:  synchronization
Outlookgooglecalendarsync
Sync your Outlook and Google calendars
Stars: ✭ 1,113 (+1424.66%)
Mutual labels:  synchronization
Form Object
Form object to use with Vue components for sending data to a Laravel application using axios.
Stars: ✭ 73 (+0%)
Mutual labels:  forms
Sync
Synchronization primitives for PHP based on Amp.
Stars: ✭ 67 (-8.22%)
Mutual labels:  synchronization
Autosize Input
🎈 Effortless, dynamic-width text boxes in vanilla JavaScript
Stars: ✭ 64 (-12.33%)
Mutual labels:  forms


MIT commitizen PRs styled with prettier All Contributors ngneat spectator CI

Sync URL Query Params with Angular Form Controls

The library provides a simple and reusable solution for binding URL query params to Angular Forms

Demo

Installation

npm install @ngneat/bind-query-params

Usage

Inject the BindQueryParamsFactory provider, pass an array of definitions and connect it to your form:

import { BindQueryParamsFactory } from '@ngneat/bind-query-params';

interface Filters {
  searchTerm: string;
  someBoolean: boolean;
}

@Component({
  template: `Your normal form setup`,
})
export class MyComponent {
  filters = new FormGroup({
    searchTerm: new FormControl(),
    someBoolean: new FormControl(false),
  });

  bindQueryParamsManager = this.factory
    .create<Filters>([
      { queryKey: 'searchTerm' },
      { queryKey: 'someBoolean', type: 'boolean' }
     ]).connect(this.filters);

  constructor(private factory: BindQueryParamsFactory) {}

  ngOnDestroy() {
    this.bindQueryParamsManager.destroy();
  }
}

With this setup, the manager will take care of two things:

  1. Update the control's value when the page is loaded for the first time
  2. Update the URL query parameter when the corresponding control value changes

QueryParam Definition

queryKey

The query parameter key

path

The form control path. If it is not specified, the manager assumes that the path is the queryKey. We can also pass nested keys, for example, person.name:

{ queryKey: 'name', path: 'person.name' }

type

Specify the control value type. Available options are: boolean, array, number, string and object. Before updating the control with the value, the manager will parse it based on the provided type.

parser

Provide a custom parser. For example, the default array parser converts the value to an array of strings. If we need it to be an array of numbers, we can pass the following parser:

const def = { parser: (value) => value.split(',').map((v) => +v) };

serializer

Provide a custom serializer. For example, supposing that we have a FormControl that carries a Date and we want to persist, in the query params, a custom value, such as a string Date, we can do something like the following serializer:

const def = { serializer: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : (value as string)) };

strategy

When working with async control values, for example, a dropdown list that its options come from the server, we cannot immediately update the control.

In this cases, we can provide the modelToUrl strategy, that will not update the control value when the page loads. When the data is available we can call the manager.syncDefs() method that'll update the controls based on the current query params:

@Component()
export class MyComponent {
  filters = new FormGroup({
    searchTerm: new FormControl(),
    users: new FormControl([]),
    someBoolean: new FormControl(false),
  });

  bindQueryParamsManager = this.factory
    .create<Filters>([
      { queryKey: 'searchTerm' },
      { queryKey: 'someBoolean', type: 'boolean' },
      { queryKey: 'users', type: 'array', strategy: 'modelToUrl' },
    ])
    .connect(this.filters);

  constructor(private factory: BindQueryParamsFactory) {}

  ngOnInit() {
    service.getUsers().subscribe((users) => {
      // Initalize the dropdown
      this.users = users;
      this.manager.syncDefs('users');
    });
  }

  ngOnDestroy() {
    this.bindQueryParamsManager.destroy();
  }
}

Note that syncDefs will always be called once under the hood.

Browser Support

The library uses the URLSearchParams API, which supported in any browser except IE.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Netanel Basal

💻 🖋 📖 🤔 🚇

This project follows the all-contributors specification. Contributions of any kind welcome!

Icons made by Freepik from www.flaticon.com
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].