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

@financial-times/ip-forms-hbs

v0.0.1

Published

create forms from json using handlebars

Downloads

3

Readme

ip-forms-hbs

npm version CircleCI

This module provides handlebar partials that will allow you to create a form with different question types such as radio buttons, checkboxes, text fields, select boxes etc based on a json or yaml file that you provide.

Setup


  npm install -S @financial-times/ip-forms-hbs

Usage

In order to start using the handlebars partials provided by the module, you need to register them as partials first. This can be done in different ways depending on the configuration that you prefer. Below you can find two ways of registering the modules's partials - one is using handlebars and the other is using express-handlebars.

N.B: The partials in the module have an extension of html. So specify this while registering your partials.


  //using handlebars
  const handlebars = require('handlebars');
  const viewsPath = join(__dirname, 'node_modules/@financial-times/ip-forms-hbs/views/partials');
  const viewsPattern = join(viewsPath, '**', '*.html');
  const partials = glob(viewsPattern)
    .map(file => ({
      name: join(relative(viewsPath, parse(file).dir), parse(file).name),
      file: read(file).toString(),
    }))
    .reduce((partial, { name, file }) => ({ ...partial, [name]: file }), {});
  handlebars.registerPartial(partials);

  //using express-handlebars
  const exphbs = require('express-handlebars');
  const handlebars = exphbs.create({ extname: '.html', partialsDir:['views/partials/',
                    'node_modules/@financial-times/ip-forms-hbs/views/partials']});

You can provide data to the partials in two different ways:

  // Option 1 : provide a json data that is formated in a way that the module expects 
  // - example is provided at Sample Data section below
  const data = require('./exampleData.json');
  
  // Option 2 : use the 'formatData' method of the module with yaml data, example given below, 
  // which will convert it to a json data that is in a format that the module expects
  const {formatData} = require('@financial-times/ip-forms-hbs');
  const yamlData = fs.readFileSync('pathToYourYamlFile', 'utf8');
  const data = formatData(yamlData);  

All the behaviours needed for the form field to function properly is provided as a single script file, main.js, and you need to include it in your script file as it is or require it if you are using a build tool to bundle your scripts as:

require('@financial-times/ip-forms-hbs/public/main.js');

Once all the setup is done, use the form partial in your app's handlebar template as:

...
  <div>
  {{> form }} 
  </div>
...

Question Types

  • Binary - for creating radio input fields with the given options as their value
  • long-text - for creating a textarea input field
  • text - for creating text input fields
  • checkbox - for creating checkbox input fields with the given options as their value
  • select-box - for creating a drop-down list with the given options as available options in the list.
  • upload - for creating input field with a type of file for uploading files

Sample Data

To properly construct your form it is advisable that you construct the questions in the following yaml format and use formatData(yamlFile) method to convert it to a json format and pass that to the template. Additionally, if you have values that you want to include as part of the form but want it to be hidden from the user, you can include these values as shown below and the module will include them to the form as input fields with type hidden.

YAML File

name: abc
method: POST
title: ip-forms-hbs
enctype: multipart/formdata
action: ""
description: |
 We believe in happy customers.
hidden:
 - fieldName: surveyId
   value: cust-sat-001
 - fieldName: region
   value: West-EU
sections:
 - title: Customer Satisifaction
   form:
   - question: Are you satisfied with our service?
     type: binary
     options:
        - Yes
        - No
   - question: What service of ours do you use most?
     type: checkbox
     options: 
        - Email Service
        - Data Service
        - Chatbot services
        - Sign Up
        - Others
     when:
      question: Are you satisfied with our service?
      is : Yes
   - question: How well do our products meet your needs?
     type: text
   - question: How long have you been a customer of our company?
     type: select-box
     options: 
      - less than 6 months
      - 6 months to one year
      - 1-2 years
      - 3 years or more
   - question: Please upload a copy of your contract with us.
     type: upload
   - question: Do you have any other comments, questions or concerns?
     type: long-text
      

JSON Data

{
  "name": "abc",
  "method": "POST",
  "title": "ip-forms-hbs",
  "enctype": "multipart/formdata",
  "action": "",
  "description": "We believe in happy customers.",
  "hidden": [
        {
            "fieldName": "surveyId",
            "value": "cust-sat-001"
        },
        {
            "fieldName": "region",
            "value": "West-EU"
        }
    ],
  "sections": [
    {
      "title": "Customer Satisifaction",
      "form": [
        {
          "question": "Are you satisfied with our service?",
          "type": "binary",
          "options": [
            "Yes",
            "No"
          ],
          "id": 0
        },
        {
          "question": "What service of ours do you use most?",
          "type": "checkbox",
          "options": [
            "Email Service",
            "Data Service",
            "Chatbot services",
            "Sign Up",
            "Others"
          ],
          "when": {
            "question": "Are you satisfied with our service?",
            "is": "Yes"
          },
          "id": 1,
          "parent-id": 0
        },
        {
          "question": "How well do our products meet your needs?",
          "type": "text",
          "id": 2
        },
        {
          "question": "How long have you been a customer of our company?",
          "type": "select-box",
          "options": [
            "less than 6 months",
            "6 months to one year",
            "1-2 years",
            "3 years or more"
          ],
          "id": 3
        },
        {
          "question": "Please upload a copy of your contract with us.",
          "type": "upload",
          "id": 4
        },
        {
          "question": "Do you have any other comments, questions or concerns?",
          "type": "long-text",
          "id": 5
        }
      ]
    }
  ]
}

Styling

Setup your bowerrc file as described at https://registry.origami.ft.com/components and add the following origami components to your project.

  • o-forms
  • o-typography
  • o-buttons

Finally, you also need to include the following styling to your app's stylesheet, *.scss, file to make the partials work properly.

.o-forms__label--child-questions {
  padding: 10px 0;
  display: none;
}
.form-question {
  position: relative;
  padding: 10px;
  background: oColorsGetPaletteColor('black-5');
  margin: 10px 0;
}
.salvation {
  @include oTypographySans(-2);
  opacity: 0;
  transition: opacity 0.3s linear;
  position: absolute;
  left: 90%;
  margin-left: 10px;
}

Licence

MIT