Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
ampersand-select-view
Advanced tools
A view module for intelligently rendering and validating selectbox input. Works well with ampersand-form-view.
Lead Maintainer: Christopher Dieringer (@cdaringe)
A view module for intelligently rendering and validating selectbox input. Works well with ampersand-form-view.
npm install ampersand-select-view
Part of the Ampersand.js toolkit for building clientside applications.
this
Alias to calling setValue(null, true)
. Sets the selected option to either the unselectedText option or a user defined option whose value is null
. Be mindful that if no unselectedText or null
option exists, the view will error.
this
Sets the selected option and view value to the original option value provided during construction.
this
Sets the selected option to that which matches the provided value. Updates the view's .value
accordingly. SelectView will error if no matching option exists. null
, undefined
, and ''
values will preferentially select unselectedText if defined.
new SelectView([options])
name
: the <select>
's name
attribute's value. Used when reporting to parent formparent
: parent form referenceoptions
: array/collection of options to render into the select box[groupOptions]
: use instead of options
to generate <optgroup>
elements within your <select>
. If this is set, any values passed in options
will be ignored and replaced with values coming from groupOptions
.[el]
: element if you want to render the view into[template]
: a custom template to use (see 'template' section, below, for more)[required]
: [default: false
] field required[eagerValidate]
: [default: false
] validate and show messages immediately. Note: field will be validated immediately to provide a true .valid
value, but messages by default are hidden.[unselectedText]
: text to display if unselected[value]
: initial value for the <select>
. value
must be a member of the options
set[tabindex]
: [default: 0
] Specify the tab index number for your field (integer).[label]
: [default: name
value] text to annotate your select control[invalidClass]
: [default: 'select-invalid'
] class to apply to root element if invalid[validClass]
: [default: 'select-valid'
] class to apply to root element if valid[requiredMessage]
: [default: 'Selection required'
] message to display if invalid and requiredIf using a collection to produce <select>
<option>
s, the following may also be specified:
[disabledAttribute]
: boolean model attribute to flag disabling of the option node[idAttribute]
: model attribute to use as the id for the option node. This will be returned by SelectView.prototype.value
[textAttribute]
: model attribute to use as the text of the option node in the select box[yieldModel]
: [default: true
] if options is a collection, yields the full model rather than just its idAttribute
to .value
When the collection changes, the view will try and maintain its currently .value
. If the corresponding model is removed, the <select>
control will default to the 0th index <option>
and update its value accordingly.
You may override the default template by providing your own template string to the constructor options hash. Technically, all you must provided is a <select>
element. However, your template may include the following under a single root element:
data-hook="label"
to annotate your select control<select>
element to hold your options
data-hook="message-container"
to contain validation messagesdata-hook="message-text"
nested beneath the data-hook="message-container"
element to show validation messagesHere's the default template for reference:
<label class="select">
<span data-hook="label"></span>
<select></select>
<span data-hook="message-container" class="message message-below message-error">
<p data-hook="message-text"></p>
</span>
</label>
You may SelectView.extend({ template: ...})
to create a View definition with a more permanent default template of your liking as well.
var FormView = require('ampersand-form-view');
var SelectView = require('ampersand-select-view');
module.exports = FormView.extend({
fields: function () {
return [
new SelectView({
label: 'Pick a color!',
// actual field name
name: 'color',
parent: this,
// you can pass simple string options
options: ['blue', 'orange', 'red'],
// if included this will add option for an unselected state
unselectedText: 'please choose one',
// you can specify that they have to pick one
required: true,
// specify tab index for usability
tabindex: 1,
}),
new SelectView({
name: 'option',
parent: this,
// you can also pass array, first is the value, second is used for the label
// and an optional third value can used to disable the option
options: [ ['a', 'Option A'], ['b', 'Option B'], ['c', 'Option C', true] ],
tabindex: 2,
}),
new SelectView({
name: 'option',
parent: this,
// define groupOptions to generate <optgroup> elements. Pass it an array of
// Objects, each object will become an <optgroup> with groupName being the
// <optgroup>'s name and options being an array (either of strings or array, see
// previous two examples) that will become the <option>s under that <optgroup>
groupOptions: [
{
groupName: "Options 1",
options: [ ['1', 'Option 1'], ['2', 'Option 2'], ['3', 'Option 3', true] ]
},
{
groupName: "Options 2",
options: [ ['a', 'Option A'], ['b', 'Option B'], ['c', 'Option C', true] ]
}
],
tabindex: 3,
}),
new SelectView({
name: 'model',
parent: this,
// you can pass in a collection here too
options: collection,
// and pick an item from the collection as the selected one
value: collection1.at(2),
// here you specify which attribute on the objects in the collection
// to use for the value returned.
idAttribute: 'id',
// you can also specify which model attribute to use as the title
textAttribute: 'title',
// you can also specify a boolean model attribute to render items as disabled
disabledAttribute: 'disabled',
// here you can specify if it should return the selected model from the
// collection, or just the id attribute. defaults `true`
yieldModel: false,
tabindex: 4,
})
];
}
});
Numeric option values are generally stringified by the browser. Be mindful doing comparisons. You'll generally desire to inspect selectView.value
(the value of your selected options' input) over selectView.select.value
(the value returned from the browser).
==
one another. E.g., do not use options whose values are "2" (string) and 2 (number). Browsers cannot distinguish between them in the select control context, thus nor can ampersand-select-view.null
, undefined
, or ''
option values are not considered valid
when the field is required. This does not apply when options are from a collection and yieldModel
is enabled.
unselectedText
option will always be preferred in updating the control to an empty-ish value.7.0.0
6.2.1
tabindex
option to allow custom tab-ordering of fields for usability6.2.0
template
6.1.0
<optgroup>
elements by passing the new options.groupOptions
parameter6.0.0
name
attr5.0.0
4.0.0
autoRender
, where previously this view would autoRender unconditionally3.0.0
skipValidation
to skipValidationMessage
, permit immediate validation, but conditionally display messages.Error
when trying to setValue(value)
and an option matching the requested value
does not exist. The exception to this is when the provided value is null
, undefined
, or ''
, and a null
option value exists. Because the DOM can only recognize a single empty value for any <option>
, which is the empty string ''
, only a single empty-ish option can only be supported by the view.0
value options, both in Model id's and array values.eagerValidate
.Originally designed & written by @philip_roberts.
MIT
FAQs
A view module for intelligently rendering and validating selectbox input. Works well with ampersand-form-view.
We found that ampersand-select-view demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.