On this page

Checkboxes field

The checkboxes field stores an array of strings representing multiple choices.

Checkboxes field

Examples

Here's an example of using the checkboxes field in a block:

# blocks/IconBox.vue

<template>
  <div>
    <div v-if="tags?.length" class="flex gap-2">
      <span v-for="tag in tags">{{ choices[tag] }}</span>
    </div>
    ...
  </div>
</template>

<script lang="ts" setup>
import { checkboxesField } from '#pruvious'

const choices = {
  'new-arrivals': 'New arrivals',
  'best-sellers': 'Best sellers',
  'on-sale': 'On sale',
}

defineProps({
  tags: checkboxesField({
    choices,
    default: ['new-arrivals'],
  }),
})
</script>

Here is another example of how to use it in a collection definition:

# collections/products.ts

import { defineCollection } from '#pruvious'

export default defineCollection({
  name: 'products',
  mode: 'multi',
  fields: {
    tags: {
      type: 'checkboxes',
      options: {
        choices: {
          'new-arrivals': 'New arrivals',
          'best-sellers': 'Best sellers',
          'on-sale': 'On sale',
        },
        default: ['new-arrivals'],
      },
    },
    // ...
  },
})

Options

You can specify the following options in the checkboxes field (required fields are marked with the symbol R):

OptionDescription

choices R

A key-value object containing permissible choices, where the key represents the choice value, and the value represents the corresponding checkbox label.

default

The default field value ([] if not defined).

description

A brief descriptive text displayed in code comments and in a tooltip at the upper right corner of the field.

label

The field label displayed in the UI. By default, it is automatically generated based on the property name assigned to the field. For example, availableSizes becomes Available sizes.

name

A string that specifies the name for the input controls. If not specified, the name attribute will be automatically generated.

overrideType

A stringified TypeScript type used for overriding the automatically generated field value type. This feature is only applicable when declaring the field in a collection.

required

Indicates whether the field input is mandatory, meaning it must be present during creation, and at least one value must be selected.

sortable

Indicates whether the checkboxes are sortable.

Last updated on January 6, 2024 at 14:00