Skip to content

EpCheckbox

EpCheckbox is my finest work. It can exist in one of two ways: checked or unchecked. Think about that.

Usage

vue
<template>
  <ep-checkbox
    v-for="checkbox in checkboxes"
    :key="checkbox.id"
    v-bind="checkbox"
    v-model="checkbox.checked"
    @update:modelValue="updateChecked($event, checkbox.label, checkbox.id)"
  />
</template>

<script setup>
  import { ref } from 'vue'
  import { EpCheckbox } from '@epicenter/vue-components'

  const checkboxes = ref([
      {
        id: 'checkbox1',
        label: 'Checked',
        name: 'checkboxes',
        value: 'checked',
        checked: true,
      },
      {
        id: 'checkbox2',
        label: 'Indeterminate',
        name: 'checkboxes',
        value: 'indeterminate',
        checked: false,
        indeterminate: true,
      },
      {
        id: 'checkbox3',
        label: 'Disabled',
        name: 'checkboxes',
        value: 'disabled',
        checked: false,
        disabled: true,
      },
    ])

    const updateChecked = (event, label, id) => {
      const checkbox = checkboxes.value.find(checkbox => checkbox.id === id)
      checkbox.checked = event
      checkbox.indeterminate = false
    }
</script>

Props

NameDescriptionTypeDefault
idThe ID of the checkbox.string-
labelThe label for the checkbox.string-
nameThe name of the checkbox.string-
valueThe value of the checkbox.string-
checkedWhether the checkbox is checked.booleanfalse
disabledWhether the checkbox is disabled.booleanfalse
indeterminateWhether the checkbox is indeterminate.booleanfalse
requiredWhether the checkbox is required.booleanfalse

INFO

This component does not use events, slots.

Component Code

vue
<template>
  <label
    :for="id"
    :class="['ep-checkbox', classes]"
  >
    <input
      :id="id"
      v-model="modelValue"
      type="checkbox"
      :name
      :value
      :checked
      :disabled
      :indeterminate
      :required
    >
    {{ label }}
  </label>
</template>

<script setup>
  import { computed } from 'vue'

  defineOptions({
    name: 'EpCheckbox'
  })

  const modelValue = defineModel({
    type: Boolean,
    required: true
  })

  const props = defineProps({
    /**
     * The ID of the checkbox.
     */
    id: {
      type: String,
      required: true
    },
    /**
     * The label for the checkbox.
     */
    label: {
      type: String,
      required: true
    },
    /**
     * The name of the checkbox.
     */
    name: {
      type: String,
      required: true
    },
    /**
     * The value of the checkbox.
     */
    value: {
      type: String,
      required: true
    },
    /**
     * Whether the checkbox is checked.
     */
    checked: {
      type: Boolean,
      default: false
    },
    /**
     * Whether the checkbox is disabled.
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * Whether the checkbox is indeterminate.
     */
    indeterminate: {
      type: Boolean,
      default: false
    },
    /**
     * Whether the checkbox is required.
     */
    required: {
      type: Boolean,
      default: false
    },
  })

  const classes = computed(() => {
    return {
      'ep-checkbox--checked': modelValue.value,
      'ep-checkbox--disabled': props.disabled,
      'ep-checkbox--indeterminate': props.indeterminate,
    }
  })
</script>

Styles (SCSS)

scss
.ep-checkbox {
  display: inline-flex;
  width: fit-content;
  align-items: center;
  gap: 1rem;
  cursor: pointer;
  user-select: none;

  &--checked,
  &--indeterminate {
    color: var(--text-color--loud);
  }

  &--disabled {
    color: var(--text-color--disabled);
    cursor: default;
  }

  input {
    appearance: auto;
    cursor: inherit;
  }

  // label {
  //   color: inherit;
  //   cursor: inherit;
  // }
}