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

form-input

v0.1.2

Published

Form input generators with error feedback support

Downloads

17

Readme

#Form Input

Renders form inputs and form input groups with feedback.

Can be used in the browser or with npm.

npm install form-input

var formInput = require('form-input')();
<script src="form-input.js"></script>
<script>
	var formInput = window.formInputGenerator();
</script>

##Input Set Attributes, Set Value to undefined for an attribute without a value.

formInput.input({ type:'text', name: 'foo', 'data-foo': undefined });
<input type="text" name="foo" data-foo/>

If given a value and "name" attribute, and the value is an object with a key of the same value as the "name" attribute, the "name" value will be used.

formInput.input({
	type:'text',
	name: 'foo',
	value: { foo: 'a', bar: 'b' }
});
<input type="text" name="foo" value="a"/>

If passed a value attribute with an array of values, the input will be repeated for each value.

formInput.input({
	type:'checkbox',
	name: 'foo[]',
	value: ['a', 'b']
});
<input type="checkbox" name="foo[]" value="a"/>
<input type="checkbox" name="foo[]" value="b"/>

If passed a array value attribute and a checked attribute, the matching checked values will be given a "checked" attribute.

formInput.input({
	type: 'radio',
	value: ['a', 'b'],
	checked: 'b'
});
<input type="radio" value="a"/>
<input type="radio" value="b" checked/>

If passed a array value attribute and an array checked attribute, the matching checked values will be given a "checked" attribute.

formInput.input({
	type: 'checkbox',
	value: ['a', 'b', 'c'],
	checked: ['a', 'b']
});
<input type="checkbox" value="a" checked/>
<input type="checkbox" value="b" checked/>
<input type="checkbox" value="c"/>

If passed a value attribute that is an array of objects, the input will be wrapped in a label attribute which is given a class name of "[type]-inline".

formInput.input({
	type:'checkbox',
	name: 'foo[]',
	value: ['a', 'b']
});
<label class="checkbox-inline">
	foo
	<input type="checkbox" name="foo[]" value="a"/>
</label>
<label class="checkbox-inline">
	bar
	<input type="checkbox" name="foo[]" value="b"/>
</label>

##Select select follows simmilar logic to input regarding the interaction of name and value attributes.

formInput.select({
	name: 'foo',
	options: [
		{ label: 'bar', value: 'b' },
		{ label: 'foo', value: 'a' }
	],
	value: { foo: 'a' }
})
<select name="foo">
	<option value="b">bar</option>
	<option value="a" selected>foo</option>
</select>

##Textarea textarea follows simmilar logic to input regarding the interaction of name and value attributes.

formInput.textarea({
	name: 'foo',
	value: { foo: 'bar' }
})
<textarea name="foo">bar</textarea>

##Feedback feedback may be used to render error or success messages next to form inputs.

formInput.feedback('foo')
<span>foo</span>

Feedback may be passed an array of strings that will be rendered with an unordered list.

formInput.feedback(['foo', 'bar'])
<span>
	<ul>
		<li>foo</li>
		<li>bar</li>
	</ul>
</span>

If Feedback is passed an object and name parameter it will pluck the appropriate name from the value object.

formInput.feedback({ foo: 'baz' }, 'foo')
<span>baz</span>

If the object does contain the named value, feedback will return an empty string.

//returns empty string
formInput.feedback({ foo: 'baz' }, 'bar')

##Group Groups labels, inputs and feedback in an html structure.

Group can take input, select, textarea, and feedback properties which will behave the same as their counterpart methods documented above.

The name attribute can be pulled to the top level of the configuration, and will be used by input and feedback as shown below.

formInput.group({
	label: 'bar',
	name: 'foo',
	input: {
		value: { foo: 'b' }
	},
	feedback: { foo: 'c' }
})
<div>
	<label>bar</label>
	<div>
		<input name="foo" value="b"/>
		<span>c</span>
	</div>
</div>

An example using a select input

formInput.group({
	label: 'bar',
	name: 'foo',
	select: {
		options: [
			{ value: 'a', label: 'A' },
			{ value: 'b', label: 'B' }
		]
		value: { foo: 'a' }
	}
})
<div>
	<label>bar</label>
	<div>
		<select name="foo">
			<option value="a" selected>A</option>
			<option value="b">B</option>
		</select>
	</div>
</div>

##Custom open and close tags Most of the closing an opening tags can be overriden either globally

var formInput = require('form-input')({
	groupOpen: '<group>',
	groupClose: '</group>'
});

Or when calling the feedback or group methods

formInput.feedback('foo', undefined, {
	feedbackOpen: '<feedback>',
	feedbackClose: '</feedback>'
});

Here is an example that overrides all available open and close tags.

formInput.group({
	feedback: 'bar',
	feedbackOpen: '<feedback>',
	feedbackClose: '</feedback>',
	labelOpen: '<foo-label>',
	labelClose: '</foo-label>',
	groupOpen: '<group>',
	groupClose: '</group>',
	groupControlOpen: '<group-control>',
	groupControlClose: '</group-control>',

	label: 'foo',
	input: { a: 'b' }
});
<group>
	<foo-label>foo</foo-label>
	<group-control>
		<input a="b"/>
		<feedback>bar</feedback>
	</group-control>
</group>