Unfortunately, the design page you are looking for is not ready yet. But you might want to switch to the implementation tab. There you will find the corresponding Vue component and a description of the different use cases and what to consider when implementing it.
If you have any questions, suggestions or ideas for improvement right now, please feel free to contact us directly.
via Slack: #wescale-wui-public
via Mail: wescale-wui@wescale.com
Stay tuned!
import {
WuiFormCheckboxGroup,
WuiFormCheckbox,
} from "@wui/wui-vue/lib/form-checkbox";
Description
For cross browser consistency,
<wui-form-checkbox-group>and<wui-form-checkbox>use WUI's custom checkbox input to replace the browser default checkbox input. It is built on top of semantic and accessible markup, so it is a solid replacement for the default checkbox input.
Example 1: Single checkbox
<template>
<div>
<wui-form-checkbox
id="checkbox-1"
v-model="status"
name="checkbox-1"
value="accepted"
unchecked-value="not_accepted"
>
I accept the terms and use
</wui-form-checkbox>
<div>State: <strong>{ status }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
status: "not_accepted",
};
},
};
</script>
<!-- wui-form-checkbox.vue -->
Example 2: Multiple choice checkboxes
<template>
<div>
<wui-form-group label="Using options array:" v-slot="{ ariaDescribedby }">
<wui-form-checkbox-group
id="checkbox-group-1"
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
name="flavour-1"
></wui-form-checkbox-group>
</wui-form-group>
<wui-form-group label="Using sub-components:" v-slot="{ ariaDescribedby }">
<wui-form-checkbox-group
id="checkbox-group-10"
v-model="selected"
:aria-describedby="ariaDescribedby"
name="flavour-2"
>
<wui-form-checkbox value="orange">Orange</wui-form-checkbox>
<wui-form-checkbox value="apple">Apple</wui-form-checkbox>
<wui-form-checkbox value="pineapple">Pineapple</wui-form-checkbox>
<wui-form-checkbox value="grape">Grape</wui-form-checkbox>
</wui-form-checkbox-group>
</wui-form-group>
<div>Selected: <strong>{ selected }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: [], // Must be an array reference!
options: [
{ text: "Orange", value: "orange" },
{ text: "Apple", value: "apple" },
{ text: "Pineapple", value: "pineapple" },
{ text: "Grape", value: "grape" },
],
};
},
};
</script>
<!-- wui-form-checkbox-multiple.vue -->
Feel free to mix and match options prop and <wui-form-checkbox> in <wui-form-checkbox-group>. Manually placed <wui-form-checkbox> inputs will appear below any checkbox inputs generated by the options prop. To have them appear above the inputs generated by options, place them in the named slot first.
Checkbox group options array
options can be an array of strings or objects. Available fields:
valueThe selected value which will be set onv-modeldisabledDisables item for selectiontextDisplay text, orhtmlDisplay basic inline html
value can be a string, number, or simple object. Avoid using complex types in values.
If both html and text are provided, html will take precedence. Only basic/native HTML is supported in the html field (components will not work). Note that not all browsers will render inline html (i.e. <i>, <strong>, etc.) inside <option> elements of a <select>.
html field, as it may make you vulnerable to XSS attacks, if you do not first sanitize the user supplied string.const options = [
"A",
"B",
"C",
{ text: "D", value: { d: 1 }, disabled: true },
"E",
"F",
];
If an array entry is a string, it will be used for both the generated value and text fields.
You can mix using strings and objects in the array.
Internally, WuiVue will convert the above array to the following array (the array of objects) format:
const options = [
{ text: "A", value: "A", disabled: false },
{ text: "B", value: "B", disabled: false },
{ text: "C", value: "C", disabled: false },
{ text: "D", value: { d: 1 }, disabled: true },
{ text: "E", value: "E", disabled: false },
{ text: "F", value: "F", disabled: false },
];
Options as an array of objects
const options = [
{ text: "Item 1", value: "first" },
{ text: "Item 2", value: "second" },
{ html: "<b>Item</b> 3", value: "third", disabled: true },
{ text: "Item 4" },
{ text: "Item 5", value: { foo: "bar", baz: true } },
];
If value is missing, then text will be used as both the value and text fields. If you use the html property, you must supply a value property.
Internally, WuiVue will convert the above array to the following array (the array of objects) format:
const options = [
{ text: "Item 1", value: "first", disabled: false },
{ text: "Item 2", value: "second", disabled: false },
{ html: "<b>Item</b> 3", value: "third", disabled: true },
{ text: "Item 4", value: "Item 4", disabled: false },
{ text: "Item 5", value: "E", disabled: false },
];
Changing the option field names
If you want to customize the field property names (for example using name field for display text) you can easily change them by setting the text-field, html-field, value-field, and disabled-field props to a string that contains the property name you would like to use:
<template>
<div>
<wui-form-checkbox-group
v-model="selected"
:options="options"
class="mb-3"
value-field="item"
text-field="name"
disabled-field="notEnabled"
></wui-form-checkbox-group>
<div class="mt-3">Selected: <strong>{ selected }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: [],
options: [
{ item: "A", name: "Option A" },
{ item: "B", name: "Option B" },
{ item: "D", name: "Option C", notEnabled: true },
{ item: { d: 1 }, name: "Option D" },
],
};
},
};
</script>
<!-- wui-form-checkbox-group-options-fields.vue -->
Inline and stacked checkboxes
<wui-form-checkbox-group> components render inline checkboxes by default, while <wui-form-checkbox> renders block-level (stacked) checkboxes.
Set the prop stacked on <wui-form-checkbox-group> to place each form control one over the other, or if using individual checkboxes not inside a <wui-form-checkbox-group>, set the inline prop on <wui-form-checkbox>.
<template>
<div>
<wui-form-group
label="Form-checkbox-group inline checkboxes (default)"
v-slot="{ ariaDescribedby }"
>
<wui-form-checkbox-group
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
name="flavour-1a"
></wui-form-checkbox-group>
</wui-form-group>
<wui-form-group
label="Form-checkbox-group stacked checkboxes"
v-slot="{ ariaDescribedby }"
>
<wui-form-checkbox-group
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
name="flavour-2a"
stacked
></wui-form-checkbox-group>
</wui-form-group>
<wui-form-group
label="Individual stacked checkboxes (default)"
v-slot="{ ariaDescribedby }"
>
<wui-form-checkbox
v-for="option in options"
v-model="selected"
:key="option.value"
:value="option.value"
:aria-describedby="ariaDescribedby"
name="flavour-3a"
>
{ option.text }
</wui-form-checkbox>
</wui-form-group>
<wui-form-group
label="Individual inline checkboxes"
v-slot="{ ariaDescribedby }"
>
<wui-form-checkbox
v-for="option in options"
v-model="selected"
:key="option.value"
:value="option.value"
:aria-describedby="ariaDescribedby"
name="flavour-4a"
inline
>
{ option.text }
</wui-form-checkbox>
</wui-form-group>
</div>
</template>
<script>
export default {
data() {
return {
selected: [], // Must be an array reference!
options: [
{ text: "Orange", value: "orange" },
{ text: "Apple", value: "apple" },
{ text: "Pineapple", value: "pineapple" },
{ text: "Grape", value: "grape" },
],
};
},
};
</script>
<!-- wui-form-checkbox-stacked.vue -->
Checkbox values and v-model
By default, <wui-form-checkbox> value will be true when checked and false when unchecked. You can customize the checked and unchecked values by specifying the value and unchecked-value properties, respectively.
The v-model binds to the checked prop. When you have multiple checkboxes that bind to a single data state variable, you must provide an array reference ([]) to your v-model. Do not use the checked prop directly.
Note that when v-model is bound to multiple checkboxes (i.e an array ref), the unchecked-value is not used. Only the value(s) of the checked checkboxes will be returned in the v-model bound array. You should provide a unique value for each checkbox's value prop (the default of true will not work when bound to an array)
To pre-check any radios, set the v-model to the value(s) of the checks that you would like pre-selected.
When placing individual <wui-form-checkbox> components within a <wui-form-checkbox-group>, most props and the v-model are inherited from the <wui-form-checkbox-group>.
Note: the unchecked-value prop does not affect the native <input>'s value attribute, because browsers don't include unchecked boxes in form submissions. To guarantee that one of two values is submitted in a native <form> submit (e.g. 'yes' or 'no'), use radio inputs instead. This is the same limitation that Vue has with native checkbox inputs.
Multiple checkboxes and accessibility
When binding multiple checkboxes together, you must set the name prop to the same value for all <wui-form-checkbox>s in the group individually or via the name prop of <wui-form-checkbox-group>. This will inform users of assistive technologies that the checkboxes are related and enables native browser keyboard navigation.
Whenever using multiple checkboxes, it is recommended that the checkboxes be placed in a <wui-form-group> component to associate a label with the entire group of checkboxes. See examples above.
Non custom check inputs (plain)
You can have <wui-form-checkbox-group> or <wui-form-checkbox> render a browser native checkbox input by setting the plain prop.
<template>
<div>
<wui-form-group
label="Plain inline checkboxes"
v-slot="{ ariaDescribedby }"
>
<wui-form-checkbox-group
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
plain
></wui-form-checkbox-group>
</wui-form-group>
<wui-form-group
label="Plain stacked checkboxes"
v-slot="{ ariaDescribedby }"
>
<wui-form-checkbox-group
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
plain
stacked
></wui-form-checkbox-group>
</wui-form-group>
</div>
</template>
<script>
export default {
data() {
return {
selected: [], // Must be an array reference!
options: [
{ text: "Orange", value: "orange" },
{ text: "Apple", value: "apple" },
{ text: "Pineapple", value: "pineapple" },
{ text: "Grape", value: "grape" },
],
};
},
};
</script>
<!-- wui-form-checkbox-plain.vue -->
Note: The plain prop has no effect when button or buttons is set.
Contextual states
WUI includes validation styles for valid and invalid states on most form controls.
Generally speaking, you'll want to use a particular state for specific types of feedback:
falseorerror(denotes invalid state) is great for when there's a blocking or required field. A user must fill in this field properly to submit the form.trueorsuccess(denotes valid state) is ideal for situations when you have per-field validation throughoutwarning(denotes invalid state) is great for when there's a field with a value which is not an error but might be a risky value in the context of the submitted data a form and want to encourage a user through the rest of the fields.nullDisplays no validation state (neither valid nor invalid)
To apply one of the contextual state icons on <wui-form-input>, set the state prop to false or error (for invalid), true or success (for valid), or null (no validation state).
Contextual state and validation example
<template>
<div>
<wui-form-checkbox-group
v-model="value"
:options="options"
:state="state"
name="checkbox-validation"
>
<wui-form-invalid-feedback :state="state"
>Please select two</wui-form-invalid-feedback
>
<wui-form-valid-feedback :state="state"
>Thank you</wui-form-valid-feedback
>
</wui-form-checkbox-group>
</div>
</template>
<script>
export default {
data() {
return {
value: [],
options: [
{ text: "First Check", value: "first" },
{ text: "Second Check", value: "second" },
{ text: "Third Check", value: "third" },
],
};
},
computed: {
state() {
return this.value.length === 2;
},
},
};
</script>
<!-- form-checkbox-validation.vue -->
Required constraint
When using individual <wui-form-checkbox> components (not in a <wui-form-checkbox-group>), and you want the checkbox(es) to be required in your form, you must provide a name on each <wui-form-checkbox> in order for the required constraint to work. All <wui-form-checkbox> components tied to the same v-model must have the same name.
The name is required in order for Assistive Technologies (such as screen readers, and keyboard only users) to know which checkboxes belong to the same form variable (the name also automatically enables native browser keyboard navigation), hence required will only work if name is set. <wui-form-checkbox-group> will automatically generate a unique input name if one is not provided on the group.
Autofocus
When the autofocus prop is set on <wui-form-checkbox>, the input will be auto-focused when it is inserted (i.e. mounted) into the document, or re-activated when inside a Vue <keep-alive> component. Note that this prop does not set the autofocus attribute on the input, nor can it tell when the input becomes visible.
Indeterminate (tri-state) support
Normally a checkbox input can only have two states: checked or unchecked. They can have any value, but they either submit that value (checked) or don't (unchecked) with a form submission (although WuiVue allows a value for the unchecked state on a single checkbox)
Visually, there are actually three states a checkbox can be in: checked, unchecked, or indeterminate.
The indeterminate state is visual only. The checkbox is still either checked or unchecked as a value. That means the visual indeterminate state masks the real value of the checkbox, so that better make sense in your UI!
<wui-form-checkbox> supports setting this visual indeterminate state via the indeterminate prop (defaults to false). Clicking the checkbox will clear its indeterminate state. The indeterminate prop can be synced to the checkbox's state by v-binding the indeterminate prop with the .sync modifier.
Note: indeterminate styling is not supported in button or switch mode, nor is it supported in <wui-form-checkbox-group> (multiple checkboxes).
Single Indeterminate checkbox:
<template>
<div>
<wui-form-checkbox v-model="checked" :indeterminate.sync="indeterminate">
Click me to see what happens
</wui-form-checkbox>
<div class="mt-3">
Checked: <strong>{ checked }</strong><br />
Indeterminate: <strong>{ indeterminate }</strong>
</div>
<wui-button @click="toggleIndeterminate"
>Toggle Indeterminate State</wui-button
>
</div>
</template>
<script>
export default {
data() {
return {
checked: true,
indeterminate: true,
};
},
methods: {
toggleIndeterminate() {
this.indeterminate = !this.indeterminate;
},
},
};
</script>
<!-- wui-form-checkbox-indeterminate.vue -->
Indeterminate checkbox use-case example:
<template>
<div>
<wui-form-group>
<template #label>
<b>Choose your flavours:</b><br />
<wui-form-checkbox
v-model="allSelected"
:indeterminate="indeterminate"
aria-describedby="flavours"
aria-controls="flavours"
@change="toggleAll"
:value="true"
:unchecked-value="false"
>
{ allSelected ? 'Un-select All' : 'Select All' }
</wui-form-checkbox>
</template>
<template v-slot="{ ariaDescribedby }">
<wui-form-checkbox-group
id="flavors"
v-model="selected"
:options="flavours"
:aria-describedby="ariaDescribedby"
name="flavors"
class="ml-4"
aria-label="Individual flavours"
stacked
></wui-form-checkbox-group>
</wui-form-group>
<div>
Selected: <strong>{ selected }</strong><br />
All Selected: <strong>{ allSelected }</strong><br />
Indeterminate: <strong>{ indeterminate }</strong>
</div>
</template>
</div>
</template>
<script>
export default {
data() {
return {
flavours: ['Orange', 'Grape', 'Apple', 'Lime', 'Very Berry'],
selected: [],
allSelected: false,
indeterminate: false
}
},
methods: {
toggleAll() {
this.selected = this.allSelected ? this.flavours.slice() : []
}
},
watch: {
selected(newValue, oldValue) {
// Handle changes in individual flavour checkboxes
if (newValue.length === 0) {
this.indeterminate = false
this.allSelected = false
} else if (newValue.length === this.flavours.length) {
this.indeterminate = false
this.allSelected = true
} else {
this.indeterminate = true
this.allSelected = false
}
}
}
}
</script>
<!-- wui-form-checkbox-indeterminate-multiple.vue -->
Note: Pay attention that plain checkbox (i.e. with prop plain) also supports indeterminate state on Windows/Linux/Mac/Android, but not on iOS.
Indeterminate state and accessibility
Not all screen readers will convey the indeterminate state to screen reader users. So it is recommended to provide some form of textual feedback to the user (possibly by via the .sr-only class) if the indeterminate state has special contextual meaning in your application.
Component reference
<wui-form-checkbox-group>
Component aliases
<wui-form-checkbox-group> can also be used via the following aliases:
<wui-checkbox-group><wui-check-group>
Properties
| Property | Type | Default | Description |
|---|---|---|---|
aria-invalid | Boolean or String | false | Sets the 'aria-invalid' attribute value on the wrapper element. When not provided, the 'state' prop will control the attribute |
autofocus | Boolean | false | When set to true, attempts to auto-focus the control when it is mounted, or re-activated when in a keep-alive. Does not set the autofocus attribute on the control |
checkedv-model | Array | [] | The current value of the checked checkboxes in the group. Must be an array when there are multiple checkboxes |
disabled | Boolean | false | When set to true, disables the component's functionality and places it in a disabled state |
disabled-field | String | 'disabled' | Field name in the options array that should be used for the disabled state |
form | String | ID of the form that the form control belongs to. Sets the form attribute on the control | |
html-fieldUse with caution | String | 'html' | Field name in the options array that should be used for the html label instead of text field |
id | String | Used to set the id attribute on the rendered content, and used as the base to generate any additional element IDs as needed | |
name | String | Sets the value of the name attribute on the form control | |
options | Array or Object | [] | Array of items to render in the component |
plain | Boolean | false | Render the form control in plain mode, rather than custom styled mode |
required | Boolean | false | Adds the required attribute to the form control |
size | String | Set the size of the component's appearance. 'sm', 'md' (default), or 'lg' | |
stacked | Boolean | false | When set, renders the checkbox group in stacked mode |
state | Boolean | null | Controls the validation state appearance of the component. true for valid, false for invalid, or null for no validation state |
text-field | String | 'text' | Field name in the options array that should be used for the text label |
validated | Boolean | false | When set, adds the Bootstrap class 'was-validated' to the group wrapper |
value-field | String | 'value' | Field name in the options array that should be used for the value |
v-model
| Property | Event |
|---|---|
checked | input |
Slots
| Name | Description |
|---|---|
default | Content (form checkboxes) to place in the form checkbox group |
first | Slot to place wui-form-checks so that they appear before checks generated from options prop |
Events
| Event | Arguments | Description |
|---|---|---|
change | checked - Value of checkboxes. Value will be an array | Emitted when selected value(s) is changed due to user interaction |
input | checked - Value of checkboxes. Value will be an array | Emitted when the checked value is changed |
<wui-form-checkbox>
Component aliases
<wui-form-checkbox> can also be used via the following aliases:
<wui-checkbox><wui-check>
Properties
| Property | Type | Default | Description |
|---|---|---|---|
aria-label | String | Sets the value of aria-label attribute on the rendered element | |
aria-labelledby | String | The ID of the element that provides a label for this component. Used as the value for the aria-labelledby attribute | |
autofocus | Boolean | false | When set to true, attempts to auto-focus the control when it is mounted, or re-activated when in a keep-alive. Does not set the autofocus attribute on the control |
checkedv-model | Any | null | The current value of the checkbox(es). Must be an array when there are multiple checkboxes bound to the same v-model |
disabled | Boolean | false | When set to true, disables the component's functionality and places it in a disabled state |
form | String | ID of the form that the form control belongs to. Sets the form attribute on the control | |
id | String | Used to set the id attribute on the rendered content, and used as the base to generate any additional element IDs as needed | |
indeterminate | Boolean | false | Renders the checkbox in an indeterminate state. Syncable via the .sync modifier |
inline | Boolean | false | When set, renders the checkbox as an inline element rather than as a 100% width block |
name | String | Sets the value of the name attribute on the form control | |
plain | Boolean | false | Render the form control in plain mode, rather than custom styled mode |
required | Boolean | false | Adds the required attribute to the form control |
size | String | Set the size of the component's appearance. 'sm', 'md' (default), or 'lg' | |
state | Boolean | null | Controls the validation state appearance of the component. true for valid, false for invalid, or null for no validation state |
unchecked-value | Any | false | Value returned when this checkbox is unchecked. Note not applicable when multiple checkboxes bound to the same v-model array |
value | Any | true | Value returned when this checkbox is checked |
v-model
| Property | Event |
|---|---|
checked | input |
Slots
| Name | Description |
|---|---|
default | Content to place in the form checkbox |
Events
| Event | Arguments | Description |
|---|---|---|
change | checked - Value of checkbox(es). When bound to multiple checkboxes, value will be an array | Emitted when selected value(s) is changed due to user interaction |
input | checked - Value of checkbox(es). When bound to multiple checkboxes, value will be an array | Emitted when the selected value(s) is changed |
