Skip to content
On this page

Checkbox Group

It's used for multiple checkboxes which are bound in one group, and indicates whether one option is selected by checking if it is checked.

Basic Usage

Bind v-model to a Boolean typed variable.

<template>
  <HcCheckboxGroup
    v-model="checkedValues"
    label="Label"
    :items="options"
  />
</template>

<script lang="ts" setup>
import { CheckboxItemType, HcCheckboxGroup } from '@deeepvision/hope-component-kit';
import { ref } from 'vue';

const checkedValues = ref<string[]>();

const options = ref<CheckboxItemType[]>([
  {
    value: 'option_1',
    label: 'Option 1',
  },
  {
    value: 'option_2',
    label: 'Option 2',
    disabled: true,
  },
  {
    value: 'option_3',
    label: 'Option 3',
  },
  {
    value: 'option_4',
    label: 'Option 4',
  },
  {
    value: 'option_5',
    label: 'Option 5',
  },
  {
    value: 'option_6',
    label: 'Option 6',
  },
]);
</script>

Column direction

Change prop direction to determinate checkbox group align

<template>
  <HcCheckboxGroup
    v-model="checkedValues"
    label="Label"
    :items="options"
    direction="col"
  />
</template>

<script lang="ts" setup>
import { CheckboxItemType, HcCheckboxGroup } from '@deeepvision/hope-component-kit';
import { ref } from 'vue';

const checkedValues = ref<string[]>();

const options = ref<CheckboxItemType[]>([
  {
    value: 'option_1',
    label: 'Option 1',
  },
  {
    value: 'option_2',
    label: 'Option 2',
  },
  {
    value: 'option_3',
    label: 'Option 3',
  },
  {
    value: 'option_4',
    label: 'Option 4',
  },
  {
    value: 'option_5',
    label: 'Option 5',
  },
  {
    value: 'option_6',
    label: 'Option 6',
  },
]);
</script>

Max checked count

Use prop max to determinate max count of selected checkboxes

<template>
  <HcCheckboxGroup
    v-model="checkedValues"
    label="Label"
    :items="options"
    :max="3"
  />
</template>

<script lang="ts" setup>
import { CheckboxItemType, HcCheckboxGroup } from '@deeepvision/hope-component-kit';
import { ref } from 'vue';

const checkedValues = ref<string[]>();

const options = ref<CheckboxItemType[]>([
  {
    value: 'option_1',
    label: 'Option 1',
  },
  {
    value: 'option_2',
    label: 'Option 2',
  },
  {
    value: 'option_3',
    label: 'Option 3',
  },
  {
    value: 'option_4',
    label: 'Option 4',
  },
  {
    value: 'option_5',
    label: 'Option 5',
  },
  {
    value: 'option_6',
    label: 'Option 6',
  },
]);
</script>

Indeterminate

Indeterminate usage

<template>
  <div>
    <HcCheckbox
      v-model="isIndeterminate"
      label="Select all"
      :indeterminate="Boolean(checkedValues?.length && !isCheckAll)"
      @change="checkAllChange"
    />
    <HcCheckboxGroup
      v-model="checkedValues"
      :items="options"
      class="flex flex-col !items-start mt-5"
      custom-class="mb-5"
      @change="onChange"
    />
  </div>
</template>

<script lang="ts" setup>
import { CheckboxItemType, HcCheckboxGroup } from '@deeepvision/hope-component-kit';
import { ref } from 'vue';

const isIndeterminate = ref(false);
const isCheckAll = ref(false);

const onChange = (value: string[]) => {
  const checkedCount = value.length;
  isIndeterminate.value = checkedCount > 0;
  isCheckAll.value = Boolean(options.value?.filter((o) => !o.disabled)?.length === checkedValues.value?.length);
};

const checkAllChange = () => {
  checkedValues.value = !isCheckAll.value ? options.value?.filter((o) => !o.disabled).map((o) => o.value) : [];
  isCheckAll.value = Boolean(options.value?.filter((o) => !o.disabled)?.length === checkedValues.value?.length);
  isIndeterminate.value = isCheckAll.value;
};

const checkedValues = ref<string[]>();

const options = ref<CheckboxItemType[]>([
  {
    value: 'option_1',
    label: 'Option 1',
  },
  {
    value: 'option_2',
    label: 'Option 2',
  },
  {
    value: 'option_3',
    label: 'Option 3',
  },
  {
    value: 'option_4',
    label: 'Option 4',
  },
  {
    value: 'option_5',
    label: 'Option 5',
  },
  {
    value: 'option_6',
    label: 'Option 6',
  },
]);
</script>

Disabled & Readonly

Use prop disabled or readonly to change state

<template>
  <div>
    <HcCheckboxGroup
      v-model="checkedValues"
      label="Label"
      :items="options"
      disabled
    />

    <HcCheckboxGroup
      v-model="checkedValues"
      label="Label"
      :items="options"
      class="mt-10"
      readonly
    />
  </div>
</template>

<script lang="ts" setup>
import { CheckboxItemType, HcCheckboxGroup } from '@deeepvision/hope-component-kit';
import { ref } from 'vue';

const checkedValues = ref<string[]>();

const options = ref<CheckboxItemType[]>([
  {
    value: 'option_1',
    label: 'Option 1',
  },
  {
    value: 'option_2',
    label: 'Option 2',
  },
  {
    value: 'option_3',
    label: 'Option 3',
  },
  {
    value: 'option_4',
    label: 'Option 4',
  },
]);
</script>

Error message

Add prop errorMsg for show error

<template>
  <HcCheckboxGroup
    v-model="checkedValues"
    label="Label"
    :items="options"
    error-msg="This field is required"
  />
</template>

<script lang="ts" setup>
import { CheckboxItemType, HcCheckboxGroup } from '@deeepvision/hope-component-kit';
import { ref } from 'vue';

const checkedValues = ref<string[]>();

const options = ref<CheckboxItemType[]>([
  {
    value: 'option_1',
    label: 'Option 1',
  },
  {
    value: 'option_2',
    label: 'Option 2',
  },
  {
    value: 'option_3',
    label: 'Option 3',
  },
  {
    value: 'option_4',
    label: 'Option 4',
  },
  {
    value: 'option_5',
    label: 'Option 5',
  },
  {
    value: 'option_6',
    label: 'Option 6',
  },
]);

</script>

Props

PropertyType Values Description Default
modelValueString[]String[]binding value[]
labelStringStringlabel text
itemsCheckboxItemType[]CheckboxItemType[]items of checkbox group[]
maxNumberNumbermax count of the selected itemsundefined
customClassStringStringcustom class for each of Checkbox
errorMsgStringStringerror message
directionstringrowcoldirection of the checkboxesrow
disabledBooleanBooleanwhether component is disabledfalse
readonlyBooleanBooleanwhether component is read onlyfalse

Slots

PropertyType Values Description Default
label-suffix-customize label suffix content
default-customize default content

Events

PropertyType Values Description Default
update:model-valueFunctionvalue - string[]triggers when the binding value changes
changeFunctionvalue - string[]triggers when the binding value changes

Created by DeepVision Software.