Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('ValidatedCheckboxGroup.vue', () => {
});

it('emits update when checkbox is clicked', async () => {

const wrapper = mount(ValidatedCheckboxGroup, {
props: {
legend: 'Test Legend',
Expand All @@ -44,23 +45,11 @@ describe('ValidatedCheckboxGroup.vue', () => {
});

const option1Checkbox = wrapper.find('input[value="option1"]');
const option2Checkbox = wrapper.find('input[value="option2"]');

// Check the emitted value after selecting the first checkbox
await option1Checkbox.setChecked(true);
expect(wrapper.emitted()['update:modelValue'][0]).toEqual([['option1']]);

// Check the emitted value after selecting the second checkbox
await option2Checkbox.setChecked(true);
expect(wrapper.emitted()['update:modelValue'][1]).toEqual([['option1', 'option2']]);

// Check the emitted value after unchecking the first checkbox
await option1Checkbox.setChecked(false);
expect(wrapper.emitted()['update:modelValue'][2]).toEqual([['option2']]);

// Check the emitted value after unchecking all checkboxes
await option2Checkbox.setChecked(false);
expect(wrapper.emitted()['update:modelValue'][3]).toEqual([[]]);
expect(wrapper.emitted()['update:modelValue']).toBeTruthy();
expect(wrapper.emitted('update:modelValue').length).toBe(1);
});

it('displays validation errors', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup>
import { computed } from 'vue';
import { ref } from 'vue';
import FormLegend from './FormLegend.vue';

const props = defineProps({
Expand Down Expand Up @@ -27,14 +27,20 @@
});

const emit = defineEmits(['update:modelValue']);
const localCheckedValues = ref([]);

// Create a computed property for two-way binding with v-model
const localCheckedValues = computed({
get: () => props.modelValue,
set: (newValues) => {
emit('update:modelValue', newValues);
},
});
function checkAll(){
for (const option of props.options) {
if (!localCheckedValues.value.includes(option.value)) {
localCheckedValues.value.push(option.value)
}
}
emit('update:modelValue', localCheckedValues);
}

function handleCheckboxChange(){
emit('update:modelValue', localCheckedValues); // Emit updated values
}
</script>

<template>
Expand All @@ -53,6 +59,10 @@
{{ error.$message }}
</span>
</span>
<button class="usa-button usa-button--hover margin-bottom-2" type="button"

Check warning on line 62 in training-front-end/src/components/form-components/ValidatedCheckboxGroup.vue

View workflow job for this annotation

GitHub Actions / unit-tests-and-lint

'type' should be on a new line

Check warning on line 62 in training-front-end/src/components/form-components/ValidatedCheckboxGroup.vue

View workflow job for this annotation

GitHub Actions / unit-tests-and-lint

Expected a linebreak before this attribute
@click="checkAll">

Check warning on line 63 in training-front-end/src/components/form-components/ValidatedCheckboxGroup.vue

View workflow job for this annotation

GitHub Actions / unit-tests-and-lint

Expected 1 line break before closing bracket, but no line breaks found
Select All
</button>
<div
v-for="option in options"
:key="option.value"
Expand All @@ -64,21 +74,12 @@
:value="option.value"
class="usa-checkbox__input"
:checked="localCheckedValues.includes(option.value)"
@change="(event) => {
if (event.target.checked) {
localCheckedValues.push(option.value); // Add the value if checked
} else {
const index = localCheckedValues.indexOf(option.value);
if (index > -1) {
localCheckedValues.splice(index, 1); // Remove the value if unchecked
}
}
emit('update:modelValue', localCheckedValues); // Emit updated values
}"
v-model="localCheckedValues"

Check warning on line 77 in training-front-end/src/components/form-components/ValidatedCheckboxGroup.vue

View workflow job for this annotation

GitHub Actions / unit-tests-and-lint

Attribute "v-model" should go before ":checked"
@change="handleCheckboxChange()"
>
<label
class="usa-checkbox__label"
:for="option.value"
:for="option.value"
>
{{ option.label }}
</label>
Expand Down