본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

체크박스 그룹

원문 보기 위키 갱신

체크박스 그룹 (CheckboxGroup)

CheckboxGroup은 목록에서 여러 옵션을 선택할 수 있게 해주는 체크박스 묶음 컴포넌트예요. v-model로 값을 제어하거나 default-value로 초기 값을 설정할 수 있어요.

출처: Nuxt UI

본문

사용법 (Usage)

v-model 디렉티브로 CheckboxGroup의 값을 제어하거나, 상태를 제어할 필요가 없을 때는 default-value prop으로 초기 값을 설정할 수 있어요.

<script setup lang="ts">
const items = ref([
  "System",
  "Light",
  "Dark"
])
const value = ref([
  "System"
])
</script>

<template>
  <UCheckboxGroup v-model="value" :items="items" />
</template>

항목 (Items)

items prop을 문자열이나 숫자 배열로 사용해요.

<script setup lang="ts">
const items = ref([
  "System",
  "Light",
  "Dark"
])
const value = ref([
  "System"
])
</script>

<template>
  <UCheckboxGroup v-model="value" :items="items" />
</template>

다음 속성을 가진 객체 배열로도 전달할 수 있어요.

  • label?: string
  • description?: string
  • value?: string
  • disabled?: boolean
  • icon?: string
  • class?: any
  • ui?: { item?: ClassNameValue, container?: ClassNameValue, base?: ClassNameValue, 'indicator'?: ClassNameValue, icon?: ClassNameValue, wrapper?: ClassNameValue, label?: ClassNameValue, description?: ClassNameValue }
<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'

const items = ref<CheckboxGroupItem[]>([
  {
    label: "System",
    description: "Matches your device settings.",
    value: "system"
  },
  {
    label: "Light",
    description: "Always uses the light theme.",
    value: "light"
  },
  {
    label: "Dark",
    description: "Always uses the dark theme.",
    value: "dark"
  }
])
const value = ref([
  "system"
])
</script>

<template>
  <UCheckboxGroup v-model="value" :items="items" />
</template>

[!CAUTION]

객체를 사용할 때는 v-model 디렉티브나 default-value prop에서 객체의 value 속성을 참조해야 해요.

값 키 (Value Key)

value-key prop을 사용해 값을 설정하는 데 쓰이는 속성을 바꿀 수 있어요. 기본값은 value예요.

<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'

const items = ref<CheckboxGroupItem[]>([
  {
    label: "System",
    description: "Matches your device settings.",
    id: "system"
  },
  {
    label: "Light",
    description: "Always uses the light theme.",
    id: "light"
  },
  {
    label: "Dark",
    description: "Always uses the dark theme.",
    id: "dark"
  }
])
const value = ref([
  "light"
])
</script>

<template>
  <UCheckboxGroup v-model="value" value-key="id" :items="items" />
</template>

범례 (Legend)

legend prop을 사용해 CheckboxGroup의 범례를 설정할 수 있어요.

<script setup lang="ts">
const items = ref([
  "System",
  "Light",
  "Dark"
])
</script>

<template>
  <UCheckboxGroup legend="Theme" :default-value="[
  'System'
]" :items="items" />
</template>

색상 (Color)

color prop을 사용해 CheckboxGroup의 색상을 바꿀 수 있어요.

<script setup lang="ts">
const items = ref([
  "System",
  "Light",
  "Dark"
])
</script>

<template>
  <UCheckboxGroup color="neutral" :default-value="[
  'System'
]" :items="items" />
</template>

변형 (Variant)

variant prop을 사용해 CheckboxGroup의 변형(variant)을 바꿀 수 있어요.

<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'

const items = ref<CheckboxGroupItem[]>([
  {
    label: "System",
    value: "system",
    description: "Matches your device settings."
  },
  {
    label: "Light",
    value: "light",
    description: "Always uses the light theme."
  },
  {
    label: "Dark",
    value: "dark",
    description: "Always uses the dark theme."
  }
])
</script>

<template>
  <UCheckboxGroup color="primary" variant="card" :default-value="[
  'system'
]" :items="items" />
</template>

크기 (Size)

size prop을 사용해 CheckboxGroup의 크기를 바꿀 수 있어요.

<script setup lang="ts">
const items = ref([
  "System",
  "Light",
  "Dark"
])
</script>

<template>
  <UCheckboxGroup size="xl" variant="list" :default-value="[
  'System'
]" :items="items" />
</template>

방향 (Orientation)

orientation prop을 사용해 CheckboxGroup의 방향을 바꿀 수 있어요. 기본값은 vertical이에요.

<script setup lang="ts">
const items = ref([
  "System",
  "Light",
  "Dark"
])
</script>

<template>
  <UCheckboxGroup orientation="horizontal" variant="list" :default-value="[
  'System'
]" :items="items" />
</template>

표시기 (Indicator)

indicator prop을 사용해 표시기(indicator)의 위치를 바꾸거나 숨길 수 있어요. 기본값은 start예요.

[!NOTE]

항목의 icon은 표시기가 보이는 동안 체크 표시를 대체하고, 표시기가 hidden이면 라벨 위에 표시돼요.

<script setup lang="ts">
import type { CheckboxGroupItem } from '@nuxt/ui'

const items = ref<CheckboxGroupItem[]>([
  {
    label: "System",
    icon: "i-lucide-monitor",
    value: "System",
    class: "w-20"
  },
  {
    label: "Light",
    icon: "i-lucide-sun",
    class: "w-20",
    value: "Light"
  },
  {
    label: "Dark",
    icon: "i-lucide-moon",
    class: "w-20",
    value: "Dark"
  }
])
</script>

<template>
  <UCheckboxGroup indicator="hidden" orientation="horizontal" variant="table" :default-value="[
  'System'
]" :items="items" />
</template>

비활성화 (Disabled)

disabled prop을 사용해 CheckboxGroup을 비활성화할 수 있어요.

<script setup lang="ts">
const items = ref([
  "System",
  "Light",
  "Dark"
])
</script>

<template>
  <UCheckboxGroup disabled :default-value="[
  'System'
]" :items="items" />
</template>

API

Props

/**
 * Props for the CheckboxGroup component
 */
interface CheckboxGroupProps {
  /**
   * The element or component this component should render as.
   * @default 'div'
   */
  as?: any;
  legend?: string | undefined;
  /**
   * When `items` is an array of objects, select the field to use as the value.
   * @default 'value'
   */
  valueKey?: VK | undefined;
  /**
   * When `items` is an array of objects, select the field to use as the label.
   * @default 'label'
   */
  labelKey?: keyof Extract<NestedItem<T>, object> & string | DotPathKeys<Extract<NestedItem<T>, object>> | undefined;
  /**
   * When `items` is an array of objects, select the field to use as the description.
   * @default 'description'
   */
  descriptionKey?: keyof Extract<NestedItem<T>, object> & string | DotPathKeys<Extract<NestedItem<T>, object>> | undefined;
  items?: T | undefined;
  /**
   * The controlled value of the CheckboxGroup. Can be bind as `v-model`.
   */
  modelValue?: GetItemValue<T, VK, undefined, NestedItem<T>>[] | undefined;
  /**
   * The value of the CheckboxGroup when initially rendered. Use when you do not need to control the state of the CheckboxGroup.
   */
  defaultValue?: GetItemValue<T, VK, undefined, NestedItem<T>>[] | undefined;
  /**
   * @default 'md'
   */
  size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
  /**
   * @default 'list'
   */
  variant?: "table" | "list" | "card" | undefined;
  /**
   * The orientation the checkbox buttons are laid out.
   * @default 'vertical'
   */
  orientation?: "horizontal" | "vertical" | undefined;
  ui?: { root?: SlotClass; fieldset?: SlotClass; legend?: SlotClass; item?: SlotClass; } & { root?: SlotClass; container?: SlotClass; base?: SlotClass; indicator?: SlotClass; icon?: SlotClass; wrapper?: SlotClass; label?: SlotClass; description?: SlotClass; } | undefined;
  /**
   * When `true`, prevents the user from interacting with the checkboxes
   */
  disabled?: boolean | undefined;
  /**
   * Whether keyboard navigation should loop around
   * @default false
   */
  loop?: boolean | undefined;
  /**
   * The name of the field. Submitted with its owning form as part of a name/value pair.
   */
  name?: string | undefined;
  /**
   * When `true`, indicates that the user must set the value before the owning form can be submitted.
   */
  required?: boolean | undefined;
  /**
   * The icon displayed when checked, or above the label when `indicator` is `hidden`.
   * @default appConfig.ui.icons.check
   */
  icon?: any;
  /**
   * @default 'primary'
   */
  color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
  /**
   * Highlight the ring color like a focus state.
   * @default false
   */
  highlight?: boolean | undefined;
  /**
   * Position of the indicator.
   * @default 'start'
   */
  indicator?: "start" | "end" | "hidden" | undefined;
}

Slots

/**
 * Slots for the CheckboxGroup component
 */
interface CheckboxGroupSlots {
  legend(): any;
  label(): any;
  description(): any;
}

Emits

/**
 * Emitted events for the CheckboxGroup component
 */
interface CheckboxGroupEmits {
  update:modelValue: (payload: [value: GetItemValue<T, VK, undefined, NestedItem<T>>[]]) => void;
  change: (payload: [event: Event]) => void;
}

테마 (Theme)

export default defineAppConfig({
  ui: {
    checkboxGroup: {
      slots: {
        root: 'relative',
        fieldset: 'flex gap-x-2',
        legend: 'mb-1 block font-medium text-default',
        item: ''
      },
      variants: {
        orientation: {
          horizontal: {
            fieldset: 'flex-row'
          },
          vertical: {
            fieldset: 'flex-col'
          }
        },
        color: {
          primary: {},
          secondary: {},
          success: {},
          info: {},
          warning: {},
          error: {},
          neutral: {}
        },
        variant: {
          list: {
            fieldset: 'flex-wrap'
          },
          card: {
            fieldset: 'flex-wrap'
          },
          table: {
            item: [
              'border border-default hover:not-has-disabled:not-has-focus-visible:not-has-data-[state=checked]:bg-elevated/50',
              'transition-colors'
            ]
          }
        },
        size: {
          xs: {
            fieldset: 'gap-y-0.5',
            legend: 'text-xs'
          },
          sm: {
            fieldset: 'gap-y-0.5',
            legend: 'text-xs'
          },
          md: {
            fieldset: 'gap-y-1',
            legend: 'text-sm'
          },
          lg: {
            fieldset: 'gap-y-1',
            legend: 'text-sm'
          },
          xl: {
            fieldset: 'gap-y-1.5',
            legend: 'text-base'
          }
        },
        required: {
          true: {
            legend: "after:content-['*'] after:ms-0.5 after:text-error"
          }
        },
        highlight: {
          true: {},
          false: {}
        },
        disabled: {
          true: {}
        }
      },
      compoundVariants: [
        {
          color: 'primary',
          variant: 'table',
          class: {
            item: 'outline-primary/25 has-focus-visible:outline-3 not-has-disabled:has-focus-visible:border-primary has-focus-visible:z-[1]'
          }
        },
        {
          color: 'secondary',
          variant: 'table',
          class: {
            item: 'outline-secondary/25 has-focus-visible:outline-3 not-has-disabled:has-focus-visible:border-secondary has-focus-visible:z-[1]'
          }
        },
        {
          color: 'success',
          variant: 'table',
          class: {
            item: 'outline-success/25 has-focus-visible:outline-3 not-has-disabled:has-focus-visible:border-success has-focus-visible:z-[1]'
          }
        },
        {
          color: 'info',
          variant: 'table',
          class: {
            item: 'outline-info/25 has-focus-visible:outline-3 not-has-disabled:has-focus-visible:border-info has-focus-visible:z-[1]'
          }
        },
        {
          color: 'warning',
          variant: 'table',
          class: {
            item: 'outline-warning/25 has-focus-visible:outline-3 not-has-disabled:has-focus-visible:border-warning has-focus-visible:z-[1]'
          }
        },
        {
          color: 'error',
          variant: 'table',
          class: {
            item: 'outline-error/25 has-focus-visible:outline-3 not-has-disabled:has-focus-visible:border-error has-focus-visible:z-[1]'
          }
        },
        {
          color: 'neutral',
          variant: 'table',
          class: {
            item: 'outline-inverted/25 has-focus-visible:outline-3 not-has-disabled:has-focus-visible:border-inverted has-focus-visible:z-[1]'
          }
        },
        {
          variant: 'table',
          highlight: false,
          class: {
            item: 'hover:not-has-disabled:not-has-focus-visible:not-has-data-[state=checked]:border-accented hover:not-has-disabled:not-has-focus-visible:not-has-data-[state=checked]:z-[1]'
          }
        },
        {
          size: 'xs',
          variant: 'table',
          class: {
            item: 'p-2.5'
          }
        },
        {
          size: 'sm',
          variant: 'table',
          class: {
            item: 'p-3'
          }
        },
        {
          size: 'md',
          variant: 'table',
          class: {
            item: 'p-3.5'
          }
        },
        {
          size: 'lg',
          variant: 'table',
          class: {
            item: 'p-4'
          }
        },
        {
          size: 'xl',
          variant: 'table',
          class: {
            item: 'p-4.5'
          }
        },
        {
          orientation: 'horizontal',
          variant: 'table',
          class: {
            item: 'first-of-type:rounded-s-lg last-of-type:rounded-e-lg',
            fieldset: 'gap-0 -space-x-px'
          }
        },
        {
          orientation: 'vertical',
          variant: 'table',
          class: {
            item: 'first-of-type:rounded-t-lg last-of-type:rounded-b-lg',
            fieldset: 'gap-0 -space-y-px'
          }
        },
        {
          color: 'primary',
          variant: 'table',
          class: {
            item: 'has-data-[state=checked]:bg-primary/10 has-data-[state=checked]:border-primary/50 has-data-[state=checked]:z-[2]'
          }
        },
        {
          color: 'secondary',
          variant: 'table',
          class: {
            item: 'has-data-[state=checked]:bg-secondary/10 has-data-[state=checked]:border-secondary/50 has-data-[state=checked]:z-[2]'
          }
        },
        {
          color: 'success',
          variant: 'table',
          class: {
            item: 'has-data-[state=checked]:bg-success/10 has-data-[state=checked]:border-success/50 has-data-[state=checked]:z-[2]'
          }
        },
        {
          color: 'info',
          variant: 'table',
          class: {
            item: 'has-data-[state=checked]:bg-info/10 has-data-[state=checked]:border-info/50 has-data-[state=checked]:z-[2]'
          }
        },
        {
          color: 'warning',
          variant: 'table',
          class: {
            item: 'has-data-[state=checked]:bg-warning/10 has-data-[state=checked]:border-warning/50 has-data-[state=checked]:z-[2]'
          }
        },
        {
          color: 'error',
          variant: 'table',
          class: {
            item: 'has-data-[state=checked]:bg-error/10 has-data-[state=checked]:border-error/50 has-data-[state=checked]:z-[2]'
          }
        },
        {
          color: 'neutral',
          variant: 'table',
          class: {
            item: 'has-data-[state=checked]:bg-elevated has-data-[state=checked]:border-inverted/50 has-data-[state=checked]:z-[2]'
          }
        },
        {
          variant: 'table',
          disabled: true,
          class: {
            item: 'cursor-not-allowed'
          }
        }
      ],
      defaultVariants: {
        highlight: false,
        size: 'md',
        variant: 'list',
        color: 'primary'
      }
    }
  }
})

더 알아보기 (Learn more)