<vega-checkbox-group>
<div class="v-mb-xs">
<vega-checkbox identifier="apple">
Apple
</vega-checkbox>
</div>
<div class="v-mb-xs">
<vega-checkbox identifier="banana">
Banana
</vega-checkbox>
</div>
<div>
<vega-checkbox identifier="orange">
Orange
</vega-checkbox>
</div>
</vega-checkbox-group>
const checkboxes = document.querySelector('vega-checkbox-group');
checkboxes.vegaFlexProp = {
gap: 'size-8',
direction: 'col',
breakpoint: 'None',
alignItem: 'start',
justifyContent: 'start',
margin: '0',
};
checkboxes.value = ['apple', 'banana'];
Note: The above example uses Vanilla JS. If you would like to use React, Angular, or Vue, please consult Storybook.
Checkboxes are used for providing single or multiple selection fields on a page. They are used when a user may choose any number of choices (zero or many) from a selection of options. Checking one box does not uncheck any others.
Checkboxes are made up of two main elements: vega-checkbox-group
, which specifies the grouping of the options, and vega-checkbox
, which references the individual checkboxes.
vega-checkbox
) PropertiesName | Description | Default |
---|---|---|
identifier | The identifier of the checkbox, which represents the return value after checked. Note: the identifier should not be empty in the checkbox group. string | |
value | The final value of each selected checkbox. If the identifier is set, return identifier , or return "" when not checked. If identifier is not set, return true when checked, else return false . Note: Do not set initialization value for this property, this property will only reflect the final value. if you set it can not change current the value. Instead use the setValue method defined below. string boolean |
|
checked | Boolean value - indicates whether the checkbox is pre-checked. boolean | false |
size | Checkboxes have two sizes: default and small. They should be chosen to fit the context of the specific form | default |
disabled | Boolean value - indicating if the input field is disabled. boolean | false |
required | This is a Boolean operator which indicates whether the vega-input is required. boolean | false |
vega-checkbox-group
) PropertiesName | Description | Default |
---|---|---|
value | The final value of each selected checkbox. If the identifier is set, return identifier , or return "" when not checked. If identifier is not set, return true when checked, else return false . Note: Do not set initialization value for this property, this property will only reflect the final value. if you set it can not change current the value. Instead use the setValue method defined below. string boolean |
|
disabled | Boolean value - indicating if the input field is disabled. boolean | false |
label | The label of checkbox group. text | "" |
required | This is a Boolean operator which indicates whether the vega-input is required. boolean | false |
form-validation | Updating the value of this property triggers the validation process. number | 0 |
hint | A prompt message to supplement the expected value of the checkbox group. string | "" |
vega-flex-prop | All properties of vega-flex components, accessible as an object. You can find a full list here: https://heartlandpayments.github.io/Vega/?path=/docs/container-flex-v2—child-items-in-the-same-width#properties |
For vega-checkbox-group
, you may modify various flex attributes such as gap, direction, breakpoint, alignment, justification, and margin using the vegaFlexProp object. For instance, if you wish to change the display direction of the checkbox group, you can change the direction from row to col.
const checkboxes = document.querySelector('vega-checkbox-group');
checkboxes.vegaFlexProp = {
gap: 'size-8',
direction: 'col',
breakpoint: 'None',
alignItem: 'start',
justifyContent: 'start',
margin: '0',
};
vega-checkbox-group
should be set, and the checked property of vega-checkbox has no effect.const checkboxes = document.querySelector('vega-checkbox-group');
checkboxes.value = ['xxx', 'xxx'];
There are a few ways of obtaining the value of a selected checkbox.
Method 1: Obtain the value from the value
attribute of vega-checkbox-group
.
var group = document.getElementById('checkbox-group-id');
console.log(group.value);
Method 2: To obtain multiple values, you may loop through all selected checkboxes.
var values = [];
document.querySelectorAll('vega-checkbox-group input:checked').forEach(cbx => {
values.push(cbx.value);
});
Method 3: You may use the vega-checkbox
without using vega-checkbox-group
which behaves similarly to <input type="checkbox"/>
in standard HTML.
Method 4: If using checkboxes within vega-form
, ideally you will want to use its native getValue
method. The behavior is such that
vega-checkbox
has an identifier (e.g. <vega-checkbox identifier='testValue'></vega-checkbox>
), it will return testValue
when checked, and ''
when unchecked.vega-checkbox
does not have an identifier (e.g. <vega-checkbox></vega-checkbox>
), it will return true
if the vega-checkbox
is checked. Otherwise it will return false.<vega-checkbox identifier="xxx" checked="true|false" disabled="true|false"> Label </vega-checkbox>
Since the value
property is only set by the component, the value should not be set directly. Instead you can call setValue
method to set the value.
const checkbox = document.querySelector('vega-checkbox');
// While checkbox has no identifier attribute, set value to true or false to change checked status.
checkbox.setValue(true);
checkbox.setValue(false);
// While checkbox has an identifier attribute, set 'identifier value'/'' to change the checked status.
checkbox.setValue('${identifier value}');
checkbox.setValue('');
When a checkbox is checked or unchecked, it will dispatch the vegaChange
event. You may add an event listener to either vega-checkbox-group
or vega-checkbox
.
If the validation result is changed, it will dispatch the vegaValidate
event. The event listener can only be added to vega-checkbox-group
.
<!-- React -->
// On a group
<VegaCheckboxGroup onVegaChange={ (e) => { console.log(e.currentTarget.value) } } ... ></VegaCheckboxGroup>
// On a single checkbox
<VegaCheckbox onVegaChange={ (e) => { console.log(e.currentTarget.value) } } ... ></VegaCheckbox>
<!-- vue.js 3.X -->
// On a group
<vega-checkbox-group @vegachange="method" ...></vega-checkbox-group>
// On a single checkbox
<vega-checkbox @vegachange="method" ...></vega-checkbox>
<!-- Javascript -->
document.getElementById("checkbox_group_id").addEventListener("vegaChange", (e) => {
console.log(e.currentTarget.value) }) document.getElementById("checkbox_id").addEventListener("vegaChange",
(e) => { console.log(e.currentTarget.value) })
For more details, view in Storybook.
Do
Don't