Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Checkboxes don't work with Array #759

Open
2 tasks
sugoidesune opened this issue Sep 11, 2024 · 1 comment
Open
2 tasks

[Bug]: Checkboxes don't work with Array #759

sugoidesune opened this issue Sep 11, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@sugoidesune
Copy link

sugoidesune commented Sep 11, 2024

Reproduction

https://stackblitz.com/edit/nce3t7-yo8fpa?file=src%2FApp.vue

Describe the bug

Checkbox forces true/false value instead of adding/removing from array.
This behavior persists with v-model and v-model:checked.

Connected: #622

replicate:

<script setup lang="ts">
import { Checkbox } from '@/components/ui/checkbox';
import { ref } from 'vue';
const checkedNames = ref([]);
</script>

<template>
  <div class="flex items-center space-x-2">
    <div>Checked names: {{ checkedNames }}</div>
    <br />

    <Checkbox
      type="checkbox"
      id="jack"
      value="Jack"
      v-model:checked="checkedNames"
    />
    <label for="jack">Jack</label>

    <Checkbox
      type="checkbox"
      id="john"
      value="John"
      v-model:checked="checkedNames"
    />
    <label for="john">John</label>

    <Checkbox
      type="checkbox"
      id="mike"
      value="Mike"
      v-model:checked="checkedNames"
    />
    <label for="mike">Mike</label>
  </div>
</template>

System Info

windows 11

Contributes

  • I am willing to submit a PR to fix this issue
  • I am willing to submit a PR with failing tests
@sugoidesune sugoidesune added the bug Something isn't working label Sep 11, 2024
@sugoidesune
Copy link
Author

sugoidesune commented Sep 11, 2024

I asked AI to fix the component and now it works using v-model and arrays.

<script setup lang="ts">
import { type HTMLAttributes, computed } from "vue";
import type { CheckboxRootEmits, CheckboxRootProps } from "radix-vue";
import {
  CheckboxIndicator,
  CheckboxRoot,
  useForwardPropsEmits,
} from "radix-vue";
import { CheckIcon } from "@radix-icons/vue";
import { cn } from "@/lib/utils";

const props = defineProps<
  CheckboxRootProps & { class?: HTMLAttributes["class"] } & {
    modelValue?: boolean | any[];
    value?: any;
  }
>();

const emits = defineEmits<
  CheckboxRootEmits & { "update:modelValue": [value: boolean | any[]] }
>();

const isChecked = computed(() => {
  if (Array.isArray(props.modelValue)) {
    return props.modelValue.includes(props.value);
  }
  return props.modelValue;
});

const toggleCheckbox = (checked: boolean) => {
  if (Array.isArray(props.modelValue)) {
    const newValue = checked
      ? [...props.modelValue, props.value]
      : props.modelValue.filter((v) => v !== props.value);
    emits("update:modelValue", newValue);
  } else {
    emits("update:modelValue", checked);
  }
};

const delegatedProps = computed(() => {
  const { class: _, modelValue: __, value: ___, ...delegated } = props;
  return delegated;
});

const forwarded = useForwardPropsEmits(delegatedProps, emits);
</script>

<template>
  <CheckboxRoot
    v-bind="forwarded"
    :checked="isChecked"
    @update:checked="toggleCheckbox"
    :class="
      cn(
        'peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
        props.class
      )
    "
  >
    <CheckboxIndicator
      class="flex h-full w-full items-center justify-center text-current"
    >
      <slot>
        <CheckIcon class="h-4 w-4" />
      </slot>
    </CheckboxIndicator>
  </CheckboxRoot>
</template>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant