Skip to content
On this page

Checkbox

A Native <input type="checkbox" /> to check items or multiple items at once, once again batteries included! This wil also cover Checkbox groups!

Preview & Playground 🖼️

Here you may find a preview of the component, with error & possible variants.

🏄 Click to expand the code
vue
<script setup lang="ts">
import { Checkbox } from '@flavorly/vanilla-components'
import { ref } from 'vue'

const value = ref('accepted')
const value2 = ref(true)
const valueMultiple = ref([])
</script>

<template>
  <PreviewWrapper>
    <div class="flex items-center justify-center mx-auto text-center mt-2">
      <!-- Regular -->
      <Checkbox
        v-model="value"
        value="accepted"
        checked-value="accepted"
        unchecked-value="notAccepted"
        feedback="Im useful helper out here, choose wisely"
        align="center-center"
      />
    </div>

    <div class="flex items-center justify-center mx-auto text-center mt-10">
      <Checkbox
        v-model="value"
        value="accepted"
        checked-value="accepted"
        unchecked-value="notAccepted"
        feedback="Im disabled"
        align="center-center"
        :disabled="true"
      />
    </div>

    <div class="flex items-center justify-center mx-auto text-center mt-2">
      <pre class="text-xs">{{ JSON.stringify(value) }}</pre>
    </div>

    <div class="h-1 my-5" />

    <div class="flex items-center justify-center space-x-2">
      <!-- Multiple -->
      <Checkbox
        v-model="valueMultiple"
        value="accepted"
        checked-value="accepted"
        unchecked-value="notAccepted"
        class="inline-flex"
      />

      <Checkbox
        v-model="valueMultiple"
        value="accepted2"
        checked-value="accepted2"
        unchecked-value="notAccepted2"
        class="inline-flex"
      />
    </div>

    <div class="flex items-center justify-center mx-auto text-center mt-2">
      <pre class="text-xs">{{ JSON.stringify(valueMultiple) }}</pre>
    </div>

    <div class="h-1 my-5" />

    <!-- Errors -->
    <div class="flex items-center justify-center mx-auto text-center mt-2">
      <Checkbox
        v-model="value2"
        errors="Sorry for this name is already taken"
        feedback="I only show when i dont have errors"
        align="center-center"
      />
    </div>

    <div class="flex items-center justify-center mx-auto text-center mt-2">
      <pre class="text-xs">{{ JSON.stringify(value2) }}</pre>
    </div>
  </PreviewWrapper>
</template>

Props 📥

Props available for this component extending the default variant & global props.

PropDescriptionAccepted ValuesDefault
modelValueThe value for the elementAnyundefined
checkedDefault state for checkboxBooleanfalse
valueInitial value[Boolean,Any]false'
checkedValueChecked Value[Boolean,Any]true'
uncheckedValueUn-checked Value[Boolean,Any]false'
alignPosition of the checkbox['left-top', 'left-center', 'left-bottom','right-bottom', ..]left-top'

Slots 🧬

Current slots available for this component are the following:

Slot default

The default slot is used to display the label of the checkbox, but you can be creative to do whatever you want.

AttributeDescriptionType
errorsThe errors being injected[String, Array, Undefined]
feedbackThe feedback message[String, Undefined]
hasErrorsIf it has errorsBoolean

Slot errors

Slot that holds the error messages when the component errors prop is defined.

AttributeDescriptionType
errorsThe errors being injected[String, Array, Undefined]
feedbackThe feedback message[String, Undefined]
hasErrorsIf it has errorsBoolean

Slot feedback

Slot that holds the feedback messages when the component feedback prop is defined. When showing errors the feedback will be hidden by default, and vice-versa.

AttributeDescriptionType
errorsThe errors being injected[String, Array, Undefined]
feedbackThe feedback message[String, Undefined]
hasErrorsIf it has errorsBoolean

Events 🚇

Here you may find the events emitted by this component.

EventDescriptionValue
update:modelValueWhen the value changesany

| click | When element is clicked | ClickEvent |

Checkbox Groups

A Checkbox group is a group of checkboxes, it's a wrapper for multiple checkboxes, it's a way to group checkboxes together. A nice use-case for this, is whenever you want to select multiple items at once, like a list of items.

🏄 Click to expand the code
vue
<script setup lang="ts">
import { CheckboxGroup } from '@flavorly/vanilla-components'
import { ref } from 'vue'
const value = ref([])
const value2 = ref([])
</script>

<template>
  <PreviewWrapper>
    <div>
      <CheckboxGroup
        v-model="value"
        name="skills"
        :options="[
          { text: 'Vue', value: 'vue', description: 'Is Vue your stack?' },
          { text: 'React', value: 'react', description: 'React just sucks right?' },
          { text: 'Angular', value: 'angular', description: 'Lets not talk about angular' },
          { text: 'Svelte', value: 'svelte', description: 'Maybe we can try some day' },
          { text: 'Ember', value: 'ember', description: 'Good stuff, but... vue' },
        ]"
      />
    </div>
    <div class="flex items-center justify-center mx-auto text-center mt-2">
      <pre class="text-xs">{{ JSON.stringify(value) }}</pre>
    </div>

    <div class="flex items-center justify-center mx-auto text-center mt-2">
      Everything disabled
    </div>

    <div>
      <CheckboxGroup
        v-model="value2"
        :disabled="true"
        name="skills"
        :options="[
          { text: 'Cant', value: 'cat', description: 'Try it?' },
          { text: 'Touch', value: 'touch', description: 'Try it again?' },
          { text: 'This', value: 'this', description: 'You shouldnt be allowed' },
          { text: 'tun', value: 'tun', description: 'Hum, not now' },
          { text: 'tunnene', value: 'tunnene', description: 'Ok, this is awkeredddd' },
          { text: 'yeah', value: 'yeah', description: 'Ok', disabled: true },
        ]"
      />
    </div>

    <div class="flex items-center justify-center mx-auto text-center mt-2">
      Only certain options disabled
    </div>

    <div>
      <CheckboxGroup
        v-model="value2"
        name="skills"
        :options="[
          { text: 'Im allowed', value: 'allowed1' },
          { text: 'Im also allowed', value: 'allowed2' },
          { text: 'Im Forbidden', value: 'forbidden1', disabled: true },
          { text: 'Im allowed again', value: 'allowed3' },
          { text: 'Im Forbidden again', value: 'forbidden2', disabled: true },
        ]"
      />
    </div>
  </PreviewWrapper>
</template>

Checkbox Group Props 📥

Props available for this component extending the default variant & global props.

PropDescriptionAccepted ValuesDefault
modelValueA v-model for the elementsArrayundefined
optionsArray of options for the selectArrayundefined
normalizeOptionsIf we should normalize the optionsArrayundefined
valueAttributeValue Attribute key to search on optionsStringundefined
textAttributeLabel Attribute key to search on optionsStringundefined

Checkbox Group Options

The options for the checkbox group, works exactly the same way as the options for RichSelect. Passing options without any structure defined, will require you to fill the valueAttribute and textAttribute props so we can grab them from your options

Here is a small example of normalized options:

js
[
    {
    value: 'batman',
    text: 'Batman',
    description: 'The Dark Knight',
    },
    {
        value: 'robin',
        text: 'Robin',
        description: 'The Dark Knight\'s sidekick',
    },
]

Released under the MIT License.