On this page

Switch field

The switch field stores a boolean value and can be used as a replacement for the checkbox field.

Switch field

Examples

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

# blocks/IconBox.vue

<template>
  <div>
    <SomeIcon
      :class="{
        'h-6 w-6': !highlightIcon,
        'h-12 w-12': highlightIcon,
      }"
    />
    ...
  </div>
</template>

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

defineProps({
  highlightIcon: switchField({
    label: 'Make icon stand out',
    default: true,
  }),
})
</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: {
    active: {
      type: 'switch',
      options: {
        label: 'Show in product list',
        default: true,
      },
    },
    // ...
  },
})

Options

You can specify the following options in the switch field:

OptionDescription

default

The default field value (false if not defined).

description

A brief descriptive text displayed in code comments and in the tooltip when hovering over the field label.

falseLabel

The label for the false value. Defaults to No.

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, showTitle becomes Show title.

name

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

required

Indicates whether the field input is mandatory, requiring its presence during creation, with the value set to true.

trueLabel

The label for the true value. Defaults to Yes.

Last updated on January 7, 2024 at 15:58