
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
form-elements
Advanced tools
A collection of form elements.
const els = require('form-elements')
const html = require('nanohtml')
const scratch = {
test: '',
test2: '',
pushed: 0
}
const errOpts = {
classes: ['error-note'],
animate: true,
hiddenState: {
height: function () {
let height = this.$errorContainer.clientHeight
if (this.$errorContainer.children.length === 0) {
height = '0'
}
return `${height}px`
},
overflow: 'hidden',
transition: 'height 0.8s'
},
visibleState: {
height: function () {
let height = this.$errorContainer.clientHeight
if (this.$errorContainer.children.length === 0) {
height = '0'
}
return `${height}px`
}
}
}
function main (state, emit) {
const formErrors = els.error(errOpts)
const inputOpts = {
classes: [],
validate: true,
errorDisplay: formErrors
}
const input2Opts = {
classes: [],
required: true,
validate: true,
errorDisplay: formErrors
}
const $input = els.labeledInput('test email', 'email', oninput, scratch.test, inputOpts)
const $input2 = els.labeledInput('test required', 'text', oninput2, scratch.test2, input2Opts)
return html`<div class="">
<div class="">
${formErrors.render()}<br>
<b>Button pushed:</b> ${scratch.pushed}<br>
<b>Input value:</b> ${scratch.test}<br>
${$input}<br>
${$input2}<br>
${els.button('i am a button', (evt) => ++scratch.pushed && emit('render'))}
</div>
</div>`
function oninput (evt) {
scratch.test = evt.currentTarget.value
}
function oninput2 (evt) {
scratch.test2 = evt.currentTarget.value
}
}
Results in:
<form>
<div></div>
<label>Email<br><input type="email" value=""></label>
<button>submit!</button>
</form>
In this example, the <input type='email'>
field will be validated using the
built-in browser's validation framework for email addresses. When the user
blur
s from the field, it'll attempt to validate the data inside, and if
it isn't valid, it'll display the error in the <div>
located at the top
of the form.
elementOptions:object(key:string,value:any)
All the elements accept an elementOptions
hash as the final argument which controls any additional element attributes you may wish to set, in addition to the following:
classes:array(string)
- an array of class names to apply, in addition to the default ones.style:string
- a string containing inline style informationIf you wish to override all the classes attached to the input, you can do so by providing the class
key on this options list.
Event handlers should be provided using the on[event]
key, e.g. oninput
or onblur
.
Each element type will have specific attributes that can be set in addition to the default attributes provided by the DOM spec.
errorOptions:object(key:string,value:any)
This is a superset of elementOptions
containing the following extra values:
animate:boolean
: whether or not it should attach style information for CSS animations to show/hide the error elementvisibleState:object(key:string,value:string|function)
: what styles to apply to the object when it's visiblehiddenState:object(key:string,value:string|function)
: what styles to apply to the object when it's visibleThe valid keys for visibleState
and hiddenState
are all valid HTML Style rules. You can either set them to a string or a function which returns a sting and runs in the context of the Error element.
{
animate: true,
hiddenState: {
opacity: 0,
transition: 'opacity 0.8s ease-in'
},
visibleState: {
opacity: 1,
height: function () {
const height = this.$el.clientHeight + 15
return `${height}px`
}
}
}
The initial state of the object is hiddenState
-- these rules will supercede any styles or classes on the object. When errors are presented to the user, it will apply the styles found in visibleState
. These will be applyed everytime a new error is presented, allowing you to grow/shrink or otherwise alter the box. When all of the errors are gone, it will apply
the hiddenState
styles again.
error(opts:errorOptions):ErrorDisplay
Create a new error display object
ErrorDisplay
:
#render():HTMLDivElement
- return a <div>
element, using the opts
provided by the constructor#displayError(key:string, text:string):undefined
- Add a new error to the display#removeError(key:string):undefined
- Remove an error from the display#clear():undefined
- Remove all errorsinput(label:string, type:string, inputHandler:function(evt:HTMLInputEvent, value:(string|number), opts:elementOptions):HTMLInputElement
Create a form input with various options. opts
accepts any of the HTMLInputElement attributes, the usual default attributes, in addition to several custom fields:
validate:boolean
- should validation be attempted when the blur
event has been firedvalidator:function($el:HTMLInputElement):boolean
- a custom validation method that returns true/false. If you need to set a custom error message, you should use $el.setCustomValidity
, but don't forget to set to an empty string when the error has been resolved!errorDisplay:ErrorElement
- the error element that validation errors should be placed into. Must match the API provided by the included error
object<input type=[type] value=[value] oninput=[inputHandler]>
button(text:string, clickHandler:function(evt:HTMLClickEvent), opts:elementOptions):HTMLButtonElement
Create a button with a click handler.
<button onclick=[clickHandler]>[text]</button>
label(text:string, opts:elementOptions):HTMLLabellement
Create a label
<label>[text]</label>
labeledInput(label:string, type:string, inputHandler:function(evt:HTMLInputEvent, value:(string|number), labelOpts:elementOptions, inputOpts:elementOptions)
Create a form input element wrapped by a label
<label>[text]<br><input type=[type] value=[value] oninput=[inputHandler]></label>
Copyright © 2018 Scripto, LLC. Licensed under Apache-2.0
FAQs
Form elements for HTML-element based systems
The npm package form-elements receives a total of 0 weekly downloads. As such, form-elements popularity was classified as not popular.
We found that form-elements demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.