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

@hdsydsvenskan/forms-utils

v2.1.1

Published

A set of useful utilities for the forms module

Downloads

1,595

Readme

@hdsydsvenskan/forms-utils

A collection of fields, widgets and other useful utils for the forms module.

js-semistandard-style

Usage

const { customFields: fields } = require('@hdsydsvenskan/forms-utils');
const { fields, widgets } = require('forms');

const formDefinition = {
  description: customFields.html({
    label: 'Description',
    html: '<p>This is a HTML description</p>'
  }),
  items: customFields.multiObject({
    label: 'Items',
    rowField: {
      itemName: fields.string({ label: 'Name' }),
      itemValuee: fields.string({ label: 'Value' })
    }
  })
}

API

.fields

  • .basic – a basic field which can eg. be extended to create more complex fields
  • .html – a HTML field, which doesn't contain any actual form widget, but inserts a piece of HTML in its place instead (specified through a html property). Does not interact with any value of the field, neither gets or sets it.
  • .multiField – a multi row field for creating complex forms. Repeats a sub-form for every item in an array value + allows adding new items. Needs a piece of client side JS to delete rows and to make adding new rows easier.

.widgets

  • Basic – a basic widget which can eg. be extended to create more complex widgets. In itself it just prints the escaped value of the field.
  • Image – an image widget which can show a preview thumbnail, linking to the full image, along with a file upload field and a "remove" button

.utils

  • getUserAttrs() – mimics the one from forms itself
  • htmlEscape() – mimics the one from forms itself
  • tag() – mimics the one from forms itself

Promised Forms

  • promisedFormHandle(form, body) – takes a form definition and a body and handles the form through a Promise – resolving to { success: true, form }, { error: true, form } or { empty: true, form }
  • promisedFormHandles(forms, body) – same as promisedFormHandle, but with multiple form definitions. If any of those definitions return error: true, then it will return { error: true, forms }. If any forms is empty, then it will return { empty: true, forms }. Else it will return { success: true, forms }.

Browser JS

Multi Field

Example

import { multiField } from '@hdsydsvenskan/forms-utils/browser/multi-field';

const multiFieldSetup = multiFieldSetup({ dragula });

multiFieldSetup.init();

multiField(options)

  • [options.activateInContext(newRow)] – if provided, then this will be called for every new row added. This enables other pieces of javascript to run on those new rows – adding eg. autocomplete functionality or other interesting things
  • [options.dragula] – if drag and drop capabilities are wanted, then provide a version of Dragula 3.x
  • [options.textRemove] – the text to add to the remove buttons. Defaults to Remove.

Returns an object with two methods: initField(elem) and init([context]).

initField(elem) initiates the javascript for a specific multi field setup.

init([context]) initiates the javascript for aöö multi field setups within the context or document.