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 { WuiFormRadioGroup, WuiFormRadio } from "@wui/wui-vue/lib/form-radio";
Description
For cross browser consistency,
<wui-form-radio-group>
and<wui-form-radio>
uses WUI's custom radio input to replace the browser default radio input. It is built on top of semantic and accessible markup, so it is a solid replacement for the default radio input.
Individual radios
<template>
<div>
<wui-form-group label="Individual radios" v-slot="{ ariaDescribedby }">
<wui-form-radio
v-model="selected"
:aria-describedby="ariaDescribedby"
name="some-radios"
value="A"
>Option A</wui-form-radio
>
<wui-form-radio
v-model="selected"
:aria-describedby="ariaDescribedby"
name="some-radios"
value="B"
>Option B</wui-form-radio
>
</wui-form-group>
<div class="mt-3">Selected: <strong>{ selected }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: "",
};
},
};
</script>
<!-- wui-form-radio.vue -->
Grouped radios
The individual radio inputs in <wui-form-radio-group>
can be specified via the options
prop, or via manual placement of the <wui-form-radio>
sub component. When using manually placed <wui-form-radio>
components within a <wui-form-radio-group>
, they will inherit most props and the v-model from the <wui-form-radio-group>
.
<template>
<div>
<wui-form-group label="Radios using options" v-slot="{ ariaDescribedby }">
<wui-form-radio-group
id="radio-group-1"
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
name="radio-options"
></wui-form-radio-group>
</wui-form-group>
<wui-form-group
label="Radios using sub-components"
v-slot="{ ariaDescribedby }"
>
<wui-form-radio-group
id="radio-group-10"
v-model="selected"
:aria-describedby="ariaDescribedby"
name="radio-sub-component"
>
<wui-form-radio value="first">Toggle this custom radio</wui-form-radio>
<wui-form-radio value="second"
>Or toggle this other custom radio</wui-form-radio
>
<wui-form-radio value="third" disabled
>This one is Disabled</wui-form-radio
>
<wui-form-radio :value="{ fourth: 4 }"
>This is the 4th radio</wui-form-radio
>
</wui-form-radio-group>
</wui-form-group>
<div class="mt-3">Selected: <strong>{ selected }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: "first",
options: [
{ text: "Toggle this custom radio", value: "first" },
{ text: "Or toggle this other custom radio", value: "second" },
{ text: "This one is Disabled", value: "third", disabled: true },
{ text: "This is the 4th radio", value: { fourth: 4 } },
],
};
},
};
</script>
<!-- wui-form-radio-group.vue -->
Feel free to mix and match options
prop and <wui-form-radio>
in <wui-form-radio-group>
. Manually placed <wui-form-radio>
inputs will appear below any radio inputs generated by the options
prop. To have them appear above the inputs generated by options
, place them in the named slot first
.
<template>
<div>
<wui-form-group
label="Radios using options and slots"
v-slot="{ ariaDescribedby }"
>
<wui-form-radio-group
id="radio-slots"
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
name="radio-options-slots"
>
<!-- Radios in this slot will appear first -->
<template #first>
<wui-form-radio value="first"
>Toggle this custom radio from slot first</wui-form-radio
>
</template>
<!-- Radios in the default slot will appear after any option generated radios -->
<wui-form-radio :value="{ fourth: 4 }"
>This is the 4th radio</wui-form-radio
>
<wui-form-radio value="fifth">This is the 5th radio</wui-form-radio>
</wui-form-radio-group>
</wui-form-group>
<div class="mt-3">Selected: <strong>{ selected }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: "",
options: [
{ text: "Or toggle this other custom radio", value: "second" },
{ text: "Third radio", value: "third" },
],
};
},
};
</script>
<!-- wui-form-radio-group-slot.vue -->
Radio group options array
options
can be an array of strings or objects. Available fields:
value
The selected value which will be set onv-model
disabled
Disables item for selectiontext
Display text, orhtml
Display 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-radio-group
v-model="selected"
:options="options"
class="mb-3"
value-field="item"
text-field="name"
disabled-field="notEnabled"
></wui-form-radio-group>
<div class="mt-3">Selected: <strong>{ selected }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: "A",
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-radio-group-options-fields.vue -->
Radio value and v-model
<wui-form-radio>
components do not have a value by default. You must explicitly supply a value via the value
prop on <wui-form-radio>
. This value will be sent to the v-model
when the radio is checked.
The v-model
of both <wui-form-radio>
and <wui-form-radio-group>
binds to the checked
prop. To pre-check a radio, you must set the v-model
value to the one of the radio's value (i.e. must match the value of specified on one of the the radio's value
prop). Do not use the checked
prop directly. Each radio in a radio group must have a unique value.
Radios support values of many types, such as a string
, boolean
, number
, or a plain object
.
Inline or stacked radios
By default <wui-form-radio-group>
generates inline radio inputs, while <wui-form-radio>
generates stacked radios. Set the prop stacked
on <wui-form-radio-group>
to make the radios appear one over the other, or when using radios not in a group, set the inline
prop on wui-form-radio
to true to render them inline.
<template>
<div>
<wui-form-group
label="Inline radios (default)"
v-slot="{ ariaDescribedby }"
>
<wui-form-radio-group
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
name="radio-inline"
></wui-form-radio-group>
</wui-form-group>
<wui-form-group label="Stacked radios" v-slot="{ ariaDescribedby }">
<wui-form-radio-group
v-model="selected"
:options="options"
:aria-describedby="ariaDescribedby"
name="radios-stacked"
stacked
></wui-form-radio-group>
</wui-form-group>
<div class="mt-3">Selected: <strong>{ selected }</strong></div>
</div>
</template>
<script>
export default {
data() {
return {
selected: "first",
options: [
{ text: "First radio", value: "first" },
{ text: "Second radio", value: "second" },
{ text: "Third radio", value: "third" },
],
};
},
};
</script>
<!-- wui-form-radio-stacked.vue -->
Required constraint
When using individual <wui-form-radio>
components (not in a <wui-form-radio-group>
), and you want the radio(s) to be required
in your form, you must provide a name
on each <wui-form-radio>
in order for the required constraint to work. All <wui-form-radio>
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 radios 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-radio-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-radio>
, 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.
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:
false
(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.true
(denotes valid state) is ideal for situations when you have per-field validation throughout a form and want to encourage a user through the rest of the fields.null
Displays no validation state (neither valid nor invalid)
To apply one of the contextual state icons on <wui-form-radio>
, set the state
prop to false
(for invalid), true
(for valid), or null
(no validation state).
Note: Contextual state is not supported for radios rendered in buttons mode.
Contextual state with feedback example
<template>
<div>
<wui-form-radio-group
v-model="value"
:options="options"
:state="state"
name="radio-validation"
>
<wui-form-invalid-feedback :state="state"
>Please select one</wui-form-invalid-feedback
>
<wui-form-valid-feedback :state="state"
>Thank you</wui-form-valid-feedback
>
</wui-form-radio-group>
</div>
</template>
<script>
export default {
data() {
return {
value: null,
options: [
{ text: "First radio", value: "first" },
{ text: "Second radio", value: "second" },
{ text: "Third radio", value: "third" },
],
};
},
computed: {
state() {
return Boolean(this.value);
},
},
};
</script>
<!-- wui-form-radio-validation.vue -->
Conveying contextual validation state to assistive technologies and colorblind users
Using these contextual states to denote the state of a form control only provides a visual, color-based indication, which will not be conveyed to users of assistive technologies - such as screen readers - or to colorblind users.
Ensure that an alternative indication of state is also provided. For instance, you could include a hint about state in the form control's <label>
text itself, or by providing an additional help text block (i.e. <wui-form-invalid-feedback>
). Specifically for assistive technologies, invalid form controls can also be assigned an aria-invalid="true"
attribute (see below).
ARIA aria-invalid
attribute
When <wui-form-radio-group>
has an invalid contextual state (i.e. state = false
) you may also want to set the <wui-form-radio-group>
prop aria-invalid
to true
.
Supported aria-invalid
values are:
false
(default) No errors detectedtrue
The value has failed validation.
aria-invalid
is automatically set to true
if the state
prop is false
.
Component reference
<wui-form-radio-group>
Component aliases
<wui-form-radio-group>
can also be used via the following aliases:
<wui-radio-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 |
checked v-model | Any | The current value of the checked radio in the group | |
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-field Use 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 radio 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 radios) to place in the form radio group |
first | Slot to place b-form-radio's so that they appear before radios generated from options prop |
Events
Event | Arguments | Description |
---|---|---|
change | checked - Current selected Value of radio group | Emitted when selected value is changed due to user interaction |
input | checked - Current selected Value of radio group | Emitted when the selected value is changed |
<wui-form-radio>
Component aliases
<wui-form-radio>
can also be used via the following aliases:
<wui-radio>
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 |
checked v-model | Any | null | The current value of the radio(s) |
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 | |
inline | Boolean | false | When set, renders the radio 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 |
value | Any | Value returned when this radio is checked |
v-model
Property | Event |
---|---|
checked | input |
Slots
Name | Description |
---|---|
default | Content to place in the form radio |
Events
Event | Arguments | Description |
---|---|---|
change | checked - Current selected Value of radio group | Emitted when selected value is changed due to user interaction |
input | checked - Current selected Value of radio group | Emitted when the selected value is changed |