Options
Context
nakedFormSelect requirements one thing of you and thats to pass in an argument which should be the selector for the select you want to initialize the library on.
For example, if the dom element you are targeting is .my-selector, you initialize it with nakedFormSelector('.my-selector').
Behind the scenes, the library queries [...document.querySelectorAll('.my-selector')].
But what if you don't want to query your selector at document? Thats where the context.value option comes into play. This can be particularly useful if you're getting some ajax data back (such is the case in lets say Drupal).
Lets continue to pretend you want to initialize the library on .my-selector but only in the context of some data you're back from an object called newData:
nakedFormSelect('.my-selector', {
context: {
value: newData
}
})
This will result in myData.querySelector('.my-selector').
One more example–I mentioned Drupal earlier and admittedly, I built this feature mainly to service my needs in Drupal projects I work on. I can use this in Drupal Behaviors like:
Drupal.behaviors.myModule = {
attach: function(context, settings) {
nakedFormSelect('.my-selector', {
context: {
value: context
}
})
}
}
...and this will query context.querySelectorAll('.my-selector').
Events
The events option lets you pass in callbacks for certain events during nakedFormSelect
Currently, you can perform callbacks during:
events.close: this happens when the dropdown is closedevents.open: this happens when the dropdown is opened
Usage
Lets pretend your callback that you want to use when the nakedFormSelect closes is called closeDropdown() and when it opens, you have a callback function called closeDropdown(). If the callback does not require any arguments to be passed to it, you'd use it like the following:
nakedFormSelect('select'), {
events: {
close: openDropdown,
open: closeDropdown
}
});
Now, in the scenario in which you do have arguments you want to pass to your callback, you'd pass your callback into an anonymous function first like the following:
nakedFormSelect('select'), {
events: {
close: () => closeDropdown(arg),
open: () => closeDropdown(arg)
}
});
Note: Of course, there are many ways you can write an anonymous function, we're being as brief as possible like proper mad lads.
Why?
In the spirit of being as verbose as possible, in case if you're not seeing why you would need callbacks, I'll raise an example'.
The first thing that comes to mind is that I built nakedFormSelect to be as barebones in terms of subjective aesthetics as possible (hence, like, 5 lines of css thats included in the dist folder).
So, I don't provide a means of animating the open/close action of the dropdown. Therefore, you may want to add callbacks to handle that. The team I'm on likes to use gsap so I've used this option to animate the dropdowns with gsap.
Here's an example of how you might use gsap to animate the open/close of nakedFormSelect
const toggleAnimation = (selector, heightSize) =>
gsap.to(`[data-naked-select-id="${selector}"] .options-wrap`, { duration: .2, height: heightSize });
nakedFormSelect('select', {
events: {
open: () => toggleAnimation('select', 'auto'),
close: () => toggleAnimation('select', 0)
}
});
Events example
Keyword Search
Keyword search adds an input field to the dropdown in which you can type characters that will filter the list of dropdown items. It has the following options:
keywordSearch.on: accepts a boolean value. It is set tofalseby defaultkeywordSearch.placeholder: accepts a string. This will set theplaceholdervalue on the keyword search input field.
Settings
Currently, there is only one setting option available:
settings.dropupThreshold: accepts an integer. Default set to 50
This feature allows you to determines how many pixels from the bottom of the window height until the dropdown drops up (versus dropping down as usual).
Settings demo.Submit Button
This option will add a button inside the dropdown.
There are multiple options avaialble in the submit button option:
submitBtn.on: accepts a boolean. Is false by default.submitBtn.text: accepts a string. Is undefined by default. This will set the text value on thebutton. IfsubmitBtn.on: truebut you leave this undefined, it defaults to the text Submit
data-label
In some unique cases, you may want to use the first option in a select as a label as opposed to a selectable option. In addition, you may not want this label to show up in the drop down at all.
You can use data-label="true" on the select to opt into this option.
data-multiple-keyword/data-pluralize
When using the library on a <select multiple></select>, by default, after you select more than 1 option, the label of the select takes on the verbiage of x Items selected (x = item count).
However, if you want to change the keyword Item to something else, you can with the data-multiple-keyword data attribute.
Usage
Lets say that you want your select to say 'x Hamburgers selected' instead of the default:
<select data-multiple-keyword="Hamburger"></select>
This option will automatically pluralize the value you pass in. So in this case, if you have 2 options selected, it will say '2 Hamburgers selected'.
If you do not want the library to automatically pluralize your keyword, simply add data-pluralize="false" to your select element as well. By default, data-pluralization is set to true
So, an example select utilizing data-multiple-keyword with data-pluralization turned off would be:
<select data-multiple-keyword="specialties" data-pluralize="false"></select>
data-multiple-keyword/data-pluralize demo.