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

@degjs/form-field-appender

v2.0.0

Published

A module for adding and saving an infinite number of form fields.

Downloads

1

Readme

formFieldAppender

Build Status

formFieldAppender is a Javascript module that allows you to add and remove an infinite number of similar form fields to a form.

Install

formFieldAppender is an ES6 module. Consequently, you'll need an ES6 transpiler (Babel is a nice one) as part of your Javascript workflow.

If you're already using NPM for your project, you can install formFieldAppender with the following command:

$ npm install @degjs/form-field-appender

Usage

Import formFieldAppender and create a new instance of it.

import formFieldAppender from '@degjs/form-field-appender';

let element = document.querySelector('div');

let instance = formFieldAppender(element);

By default, formFieldAppender will search the supplied element for an item that matches the itemClass parameter to use as its blueprint.

However, you can optionally provide either an HTML element or string representation of an HTML element as the item blueprint.

As an HTML element:

import formFieldAppender from '@degjs/form-field-appender';

let element = document.querySelector('div');
let itemBlueprint = document.createElement('div');

itemBlueprint.classList.add('phone-field__wrapper');
itemBlueprint.insertAdjacentHTML('afterbegin', `
    <label for="phone-field">Field</label>
    <input class="phone-field" name="phone-field" id="phone-field" type="tel">
`);

let instance = formFieldAppender(element, {
    blueprint: itemBlueprint
});

As a string representation of an HTML element:

import formFieldAppender from '@degjs/form-field-appender';

let element = document.querySelector('div');
let itemBlueprint = `
    <div class="phone-field__wrapper">
        <label for="phone-field">Field</label>
        <input class="phone-field" name="phone-field" id="phone-field" type="tel">
    </div>
`;

let instance = formFieldAppender(element, {
    blueprint: itemBlueprint
});

Parameters

blueprint

Type: HTMLElement or string
An HTML element or string representation of an HTML element that will be used as the blueprint for the repeating field.
Default: null

itemClass

Type: string
The class applied to each appended item.
Default: js-ffa-item

addTriggerSelector

Type: string
The selector for each item's add trigger.
Default: .js-ffa-add-trigger

removeTriggerSelector

Type: string
The selector for each item's remove trigger.
Default: .js-ffa-remove-trigger

idPatternAttr

Type: string
The attribute that contains the pattern that will be used to create each appended item's ID. A token of [[itemIndex]] in the pattern will be dynamically replaced by the item's current index, a token of [[itemElIndex]] will be replaced by the index of the element within the item, and a token of [[index]] will be replaced by a hyphenated combination of item and element index (i.e., ffa-id-3-2 for the third element inside the fourth item).
Default: data-ffa-id-pattern

namePatternAttr

Type: string
The attribute that contains the pattern that will be used to create each appended item's name attribute. See idPatternAttr description for available token values. Default: data-ffa-name-pattern

forPatternAttr

Type: string
The attribute that contains the pattern that will be used to create each appended item's for attribute. See idPatternAttr description for available token values.
Default: data-ffa-for-pattern

disabledClass

Type: string
The class applied to an item when it's hidden/disabled. This class can be used to visibly hide disabled items with CSS.
Default: is-disabled

initialReindex

Type: number
The first index value applied to added items.
Default: 1

firstItemIsRemovable

Type: boolean
Determines whether the first item can be removed.
Default: true

onlyLastItemIsRemovable

Type: boolean
Determines whether a "remove" trigger is added to all items, or only the last item.
Default: false

onItemAddCallback

Type: function or null
An optional callback that's fired whenever an item is added.
Default: null

onItemRemoveCallback

Type: function or null
An optional callback that's fired whenever an item is removed.
Default: null

resetNewItemFormVals

Type: boolean
Determines whether the fields in added items contain the values of the original item/template.
Default: true

Methods

.destroy()

Parameters: none
Destroys the formFieldAppender instance.

.getItems()

Parameters: none
Returns an array of the items currently added to the formFieldAppender instance.

Browser Support

formFieldAppender depends on the following browser APIs:

To support legacy browsers, you'll need to include polyfills for the above APIs.