🔥 Meet Our New Project: t0ggles - Your Ultimate Project Management Tool! 🔥

Checkbox Vue Component

Checkbox Components

There are following components included:

  • Checkbox

Checkbox Props

NameTypeDefaultDescription
checkedbooleanfalse

Defines whether the checkbox input is checked or not

colorsobject

Object with Tailwind CSS colors classes

colors.bgCheckedIosstring'bg-primary'
colors.bgCheckedMaterialstring'bg-md-light-primary dark:bg-md-dark-primary'
colors.borderCheckedIosstring'border-primary'
colors.borderCheckedMaterialstring'border-md-light-primary dark:border-md-dark-primary'
colors.borderIosstring'border-black border-opacity-30 dark:border-white dark:border-opacity-30'
colors.borderMaterialstring'border-md-light-outline dark:border-md-dark-outline'
componentstring'label'

Component's HTML Element

defaultCheckedbooleanfalse

Defines whether the checkbox input is checked or not, for the case if it is uncontrolled component

disabledbooleanfalse

Defines whether the checkbox input is disabled

indeterminatebooleanfalse

Defines whether the checkbox input is in indeterminate state or not

namestring

Checkbox input name

readonlybooleanfalse

Defines whether the checkbox input is readonly

touchRipplebooleantrue

Enables touch ripple effect in Material theme

valueany

Checkbox input value

Checkbox Events

NameTypeDescription
changefunction(e)

Event will be triggered when checkbox state changed

Checkboxes List

Checkboxes List is not a separate component, but just a particular case of using List and ListItem components.

<k-list>
  <!-- Additional "label" prop on ListItem -->
  <k-list-item
    label
    title="Books"
  >
    <template #media>
      <!-- Pass Checkbox to list item media -->
      <k-checkbox component="div" name="my-checkbox" />
    </template>
  </k-list-item>
  <k-list-item
    label
    title="Movies"
  >
    <template #media>
      <!-- Pass Checkbox to list item media -->
      <k-checkbox component="div" name="my-checkbox" />
    </template>
  </k-list-item>
</List>

Examples

Checkbox.vue
<template>
<k-page>
<k-navbar title="Checkbox" />
<k-block-title>Inline</k-block-title>
<k-block strong-ios outline-ios>
<p>
Lorem{{ ' ' }}
<k-checkbox
name="checkbox-1"
:checked="checked1"
@change="(e) => (checked1 = e.target.checked)"
/>{{ ' ' }} ipsum dolor sit amet, consectetur adipisicing elit. Alias
beatae illo nihil aut eius commodi sint eveniet aliquid eligendi
{{ ' ' }}
<k-checkbox
name="checkbox-2"
:checked="checked2"
@change="(e) => (checked2 = e.target.checked)"
/>{{ ' ' }}
ad delectus impedit tempore nemo, enim vel praesentium consequatur nulla
mollitia!
</p>
</k-block>
<k-block-title>Checkbox Group</k-block-title>
<k-list strong-ios outline-ios>
<k-list-item label title="Books">
<template #media>
<k-checkbox
component="div"
name="demo-checkbox"
:checked="group.includes('Books')"
@change="() => toggleGroupValue('Books')"
/>
</template>
</k-list-item>
<k-list-item label title="Movies">
<template #media>
<k-checkbox
component="div"
name="demo-checkbox"
:checked="group.includes('Movies')"
@change="() => toggleGroupValue('Movies')"
/>
</template>
</k-list-item>
<k-list-item label title="Food">
<template #media>
<k-checkbox
component="div"
name="demo-checkbox"
:checked="group.includes('Food')"
@change="() => toggleGroupValue('Food')"
/>
</template>
</k-list-item>
<k-list-item label title="Drinks">
<template #media>
<k-checkbox
component="div"
name="demo-checkbox"
:checked="group.includes('Drinks')"
@change="() => toggleGroupValue('Drinks')"
/>
</template>
</k-list-item>
</k-list>
<k-block-title>Indeterminate State</k-block-title>
<k-list strong-ios outline-ios>
<k-list-item label title="Movies" name="demo-checkbox">
<template #media>
<k-checkbox
:checked="movies.length === 2"
:indeterminate="movies.length === 1"
@change="onMoviesChange"
/>
</template>
<ul class="ps-12">
<k-list-item label title="Movie 1">
<template #media>
<k-checkbox
name="demo-checkbox"
value="Movie 1"
:checked="movies.indexOf('Movie 1') >= 0"
@change="onMovieChange"
/>
</template>
</k-list-item>
<k-list-item label title="Movie 2">
<template #media>
<k-checkbox
name="demo-checkbox"
value="Movie 2"
:checked="movies.indexOf('Movie 2') >= 0"
@change="onMovieChange"
/>
</template>
</k-list-item>
</ul>
</k-list-item>
</k-list>
<k-block-title>With Media Lists</k-block-title>
<k-list strong-ios outline-ios>
<k-list-item
label
title="Facebook"
after="17:14"
subtitle="New messages from John Doe"
text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis tellus ut turpis condimentum, ut dignissim lacus tincidunt. Cras dolor metus, ultrices condimentum sodales sit amet, pharetra sodales eros. Phasellus vel felis tellus. Mauris rutrum ligula nec dapibus feugiat. In vel dui laoreet, commodo augue id, pulvinar lacus."
>
<template #media>
<k-checkbox
component="div"
name="demo-media-checkbox"
:checked="media.includes('Item 1')"
@change="() => toggleMediaValue('Item 1')"
/>
</template>
</k-list-item>
<k-list-item
label
title="John Doe (via Twitter)"
after="17:11"
subtitle="John Doe (@_johndoe) mentioned you on Twitter!"
text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis tellus ut turpis condimentum, ut dignissim lacus tincidunt. Cras dolor metus, ultrices condimentum sodales sit amet, pharetra sodales eros. Phasellus vel felis tellus. Mauris rutrum ligula nec dapibus feugiat. In vel dui laoreet, commodo augue id, pulvinar lacus."
>
<template #media>
<k-checkbox
component="div"
name="demo-media-checkbox"
:checked="media.includes('Item 2')"
@change="() => toggleMediaValue('Item 2')"
/>
</template>
</k-list-item>
</k-list>
</k-page>
</template>
<script>
import { ref } from 'vue';
import {
kPage,
kNavbar,
kNavbarBackLink,
kCheckbox,
kBlockTitle,
kBlock,
kList,
kListItem,
} from 'konsta/vue';
export default {
components: {
kPage,
kNavbar,
kNavbarBackLink,
kCheckbox,
kBlockTitle,
kBlock,
kList,
kListItem,
},
setup() {
const checked1 = ref(false);
const checked2 = ref(true);
// Group
const group = ref(['Books']);
const toggleGroupValue = (value) => {
if (group.value.includes(value))
group.value.splice(group.value.indexOf(value), 1);
else group.value.push(value);
group.value = [...group.value];
};
// Indeterminate
const movies = ref(['Movie 1']);
const onMovieChange = (e) => {
const value = e.target.value;
if (e.target.checked) {
movies.value.push(value);
} else {
movies.value.splice(movies.value.indexOf(value), 1);
}
movies.value = [...movies.value];
};
const onMoviesChange = () => {
if (movies.value.length === 1 || movies.value.length === 0) {
movies.value = ['Movie 1', 'Movie 2'];
} else if (movies.value.length === 2) {
movies.value = [];
}
};
// Media
const media = ref(['Item 1']);
const toggleMediaValue = (value) => {
if (media.value.includes(value))
media.value.splice(media.value.indexOf(value), 1);
else media.value.push(value);
media.value = [...media.value];
};
return {
checked1,
checked2,
group,
toggleGroupValue,
movies,
onMovieChange,
onMoviesChange,
media,
toggleMediaValue,
};
},
};
</script>
Code licensed under MIT.
2022 © Konsta UI by nolimits4web.