Custom Radios Form Group Component
Overview
Here is an overview of the Custom_radios_form_group class:
class Custom_radios_form_group {
constructor (opts = false) { ... }
get_class_defaults () { ... }
get_generate_options (options) { ... }
generate (options = false) { ... }
}
Default Settings
The following object properties and values are all of the defaults for this class:
{
classes : {
form_groups : 'form-group mb-4',
bordered_label_wrappers : 'd-flex justify-content-between align-items-center border-bottom mb-2',
labels : 'mb-1',
label_buttons : 'btn btn-sm btn-link pb-1 pl-3 pr-0',
label_button_icons : 'fas fa-question-circle text-primary',
radio_parents : 'custom-control custom-radio',
radios : 'custom-control-input',
radio_labels : 'custom-control-label',
form_text_parents : 'text-left pr-2',
form_help_texts : 'form-text text-muted',
form_error_texts : 'd-none form-text text-danger',
form_success_texts : 'd-none form-text text-success'
},
aria_describedby_suffix : '-help',
error_text_suffix : '-error',
success_text_suffix : '-success',
group_id : 'default-radios',
group_name : 'custom-radios',
label : 'Default Custom Radios',
form_text : {
help : ['Custom Radios help text'],
error : ['Custom Radios error text'],
success : ['Custom Radios success text']
},
form_modal_text : {
heading: 'Custom Radios',
body: [{
type: 'paragraphs',
content: [ 'Custom Radios are Bootstrap 4 custom form elements. Custom Radios leverage brand colors and CSS to replace the browser-defined styling of radios.', 'Using Custom Radios lets a web app provide users with radio elements that look/feel the same across different browsers.' ]
}]
},
radios : [
{
label: 'Default Radio 1',
attributes:
{
id : 'radio-1',
value : 'value1',
checked : ''
}
}
]
}
Methods
The following class methods allow you to interact with the Custom_radios_form_group class object.
constructor(opts)
Class constructor with an optional argument expecting an object with class-specific properties/values. Any matching properties passed will override the default values. The constructor function exposes a .defaults object containing various default settings for the component.
<script type="module">
// import module
import { Custom_radios_form_group } from './js/modules/form_components.js';
// create an object with your overrides specified
let radio_default_settings = {
group_id : 'main-course',
group_name : 'main-course',
label : 'Main Course:',
radios : [
{
label: 'Chicken',
attributes:
{
id : 'meal-main-course',
value : 'chicken',
checked : ''
}
},
{
label: 'Fish',
attributes:
{
id : 'meal-main-course',
value : 'fish'
}
}
]
};
// initialize the class and pass your settings as an argument
let radios = new Custom_radios_form_group(radio_default_settings);
// confirm your changes
console.log( radios.defaults.attributes.class );
</script>
get_class_defaults()
Returns an object with all of the default properties of the Custom_radios_form_group class.
<script type="module">
// import module
import { Custom_radios_form_group } from './js/modules/form_components.js';
// initialize the class and call the method
console.log( new Custom_radios_form_group().get_class_defaults() );
</script>
Conversely, you can also access the Class defaults directly after initializing the class, and without the get_class_defaults() method:
<script type="module">
// import module
import { Custom_radios_form_group } from './js/modules/form_components.js';
// initialize the class and call the method
console.log( new Custom_radios_form_group().defaults );
</script>
get_generate_options()
Requires an object to be passed as an argument. The argument will override the default settings of the class, but ONLY in the scope of this method. This method is mainly provided for debugging purposes.
<script type="module">
// import module
import { Custom_radios_form_group } from './js/modules/form_components.js';
// initialize the class and pass your settings as an argument
let radios = new Custom_radios_form_group();
let my_test_settings = {
group_id : 'main-course',
group_name : 'main-course',
label : 'Main Course:',
radios : [
{
label: 'Chicken',
attributes:
{
id : 'meal-main-course',
value : 'chicken',
checked : ''
}
},
{
label: 'Fish',
attributes:
{
id : 'meal-main-course',
value : 'fish'
}
}
]
};
// initialize the class and call the method
console.log( radios.get_generate_options( my_test_settings ) );
</script>
generate()
Takes an optional argument expecting an object with class-specific properties/values. Any matching properties passed will override the default values. This method returns a node list of DOM element(s), configured according to the class defaults and any overridden settings.
<script type="module">
// import module
import { Custom_radios_form_group } from './js/modules/form_components.js';
// initialize the class and pass your settings as an argument
let radios = new Custom_radios_form_group();
// set your desired settings for the component elements you're about to generate
let radio_settings = {
group_id : 'main-course',
group_name : 'main-course',
label : 'Main Course:',
radios : [
{
label: 'Chicken',
attributes:
{
id : 'meal-main-course',
value : 'chicken',
checked : ''
}
},
{
label: 'Fish',
attributes:
{
id : 'meal-main-course',
value : 'fish'
}
}
]
};
// generate the component nodes to spec by passing your settings in
let radio = radios.generate( radio_settings );
</script>