The IRadioButtons component allows you to control multiple selected values using a single binding.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('apple');
const options = ref([
{ id: 'apple', label: 'Apple' },
{ id: 'banana', label: 'Banana' },
{ id: 'strawberry', label: 'Strawberry' },
{ id: 'mango', label: 'Mango' }
]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" />
</template>
You can disable an entire radio buttons group using the disabled property.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('apple');
const options = ref([
{ id: 'apple', label: 'Apple' },
{ id: 'banana', label: 'Banana' },
{ id: 'strawberry', label: 'Strawberry' },
{ id: 'mango', label: 'Mango' }
]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" disabled />
</template>
You can disable an entire radio buttons group using the readonly property.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('strawberry');
const options = ref([
{ id: 'apple', label: 'Apple' },
{ id: 'banana', label: 'Banana' },
{ id: 'strawberry', label: 'Strawberry' },
{ id: 'mango', label: 'Mango' }
]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" readonly />
</template>
You can use the color property to set a light or dark color for your radioes.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('apple');
const options = ref([
{ id: 'apple', label: 'Apple' },
{ id: 'banana', label: 'Banana' },
{ id: 'strawberry', label: 'Strawberry' },
{ id: 'mango', label: 'Mango' }
]);
</script>
<template>
<IRadioButtons v-model="checked" color="light" :options="options" />
<IRadioButtons v-model="checked" color="dark" :options="options" />
</template>
You're able to use the size property to control the size of your radio buttons group, using one of the available sizes: sm, md, and lg. The default size is set to md.
The chosen size will be applied to all of its child inputs.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('strawberry');
const options = ref([
{ id: 'apple', label: 'Apple' },
{ id: 'banana', label: 'Banana' },
{ id: 'strawberry', label: 'Strawberry' },
{ id: 'mango', label: 'Mango' }
]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" size="sm" />
<IRadioButtons v-model="checked" :options="options" size="md" />
<IRadioButtons v-model="checked" :options="options" size="lg" />
</template>
You're able to use the variant property to control the aspect of your radio buttons group, using one of the available variants: default, and button-group. The default variant is set to default.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('apple');
const options = ref([
{ id: 'apple', label: 'Apple' },
{ id: 'banana', label: 'Banana' },
{ id: 'strawberry', label: 'Strawberry' },
{ id: 'mango', label: 'Mango' }
]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" variant="default" />
<IRadioButtons v-model="checked" :options="options" variant="group" />
</template>
The IRadioButtons component accepts an array of objects as its options prop.
- each item must contain a
id property - the
modelValue property will be an array of the selected id values - the
label property is a Renderable will be used as the radio label.
export interface RadioButtonsOption {
id: string | number;
label?: Renderable;
disabled?: boolean;
readonly?: boolean;
buttonProps?: Record<string, unknown>;
[key: string]: any;
}
Read more about Renderable props.
Expressions are strings that can be interpolated using the {{ }} syntax.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('apple');
const options = ref([
{ id: 'apple', fruit: { name: 'Apple' } },
{ id: 'banana', fruit: { name: 'Banana' } },
{ id: 'strawberry', fruit: { name: 'Strawberry' } },
{ id: 'mango', fruit: { name: 'Mango' } }
]);
const labelRenderString = '{{fruit.name}}';
</script>
<template>
<IRadioButtons v-model="checked" :options="options" :label="labelRenderString" />
</template>
Render functions are functions that return a string or VNode. They receive the option being rendered as an argument.
<script lang="ts" setup>
import { h, ref } from 'vue';
import type { LabelRenderFunction, RadioButtonOption } from '@inkline/inkline';
const checked = ref('apple');
const options = ref<RadioButtonOption[]>([
{ id: 'apple' },
{ id: 'banana' },
{ id: 'strawberry' },
{ id: 'mango' }
]);
const labelRenderFunction: LabelRenderFunction<RadioButtonOption> = (option) =>
h('strong', option.id);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" :label="labelRenderFunction" />
</template>
You can also use a component to render each option. The component will receive the option being rendered as a prop named ctx (context).
<script lang="ts" setup>
import { defineComponent, h, markRaw, ref } from 'vue';
import type { RadioButtonOption } from '@inkline/inkline';
const LabelRenderComponent = markRaw(
defineComponent({
props: {
ctx: {
type: Object,
default: () => ({})
}
},
setup(props) {
return () => h('strong', props.ctx.id);
}
})
);
const checked = ref('apple');
const options = ref<RadioButtonOption[]>([
{ id: 'apple' },
{ id: 'banana' },
{ id: 'strawberry' },
{ id: 'mango' }
]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" :label="LabelRenderComponent" />
</template>
Each option's label field is a Renderable property. This means you can also use a render function to render each option's label.
<script lang="ts" setup>
import { h, ref } from 'vue';
const checked = ref('apple');
const options = ref([
{ id: 'apple', label: () => h('strong', { class: '_color:green' }, 'Apple') },
{ id: 'banana', label: () => h('strong', { class: '_color:yellow' }, 'Banana') },
{ id: 'strawberry', label: () => h('strong', { class: '_color:red' }, 'Strawberry') },
{ id: 'mango', label: () => h('strong', { class: '_color:orange' }, 'Mango') }
]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options" />
</template>
The option scoped slot can be used to render each option. It receives the option being rendered as a prop named option.
<script lang="ts" setup>
import { ref } from 'vue';
const checked = ref('apple');
const options = ref([{ id: 'apple' }, { id: 'banana' }, { id: 'strawberry' }, { id: 'mango' }]);
</script>
<template>
<IRadioButtons v-model="checked" :options="options">
<template #option="{ option }">
{{ option.id }}
</template>
</IRadioButtons>
</template>