Skip to content

Commit 0952081

Browse files
authored
Merge pull request #1110 from dnum-mi/fix/checkboxset-legend
Fix/checkboxset legend
2 parents 1f94353 + eabc131 commit 0952081

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/components/DsfrCheckbox/DsfrCheckboxSet.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,63 @@ describe('DsfrCheckboxSet', () => {
155155
// Then
156156
expect(legendEl.nextElementSibling).toBe(null)
157157
})
158+
159+
describe('legend', () => {
160+
// Given
161+
const firstLabelText = 'Premier label'
162+
const secondLabelText = 'Deuxième label'
163+
const modelValue = []
164+
const options = [
165+
{
166+
id: '1',
167+
label: firstLabelText,
168+
value: 'un',
169+
modelValue,
170+
name: '1',
171+
},
172+
{
173+
id: '2',
174+
label: secondLabelText,
175+
value: 'deux',
176+
modelValue,
177+
name: '2',
178+
},
179+
]
180+
const legendText = 'Légende de l’ensemble des champs'
181+
it('should render legend cause slot is used', async () => {
182+
// When
183+
const { container } = render(DsfrCheckboxSet, {
184+
props: {
185+
legend: legendText,
186+
errorMessage: 'Message d’erreur',
187+
options,
188+
},
189+
})
190+
expect(container.querySelectorAll('legend')).toHaveLength(1)
191+
})
192+
it('should render legend cause props legend is not empty', async () => {
193+
// When
194+
const { container } = render(DsfrCheckboxSet, {
195+
props: {
196+
legend: legendText,
197+
options,
198+
},
199+
slots: {
200+
legend: () => legendText,
201+
},
202+
})
203+
expect(container.querySelectorAll('legend')).toHaveLength(1)
204+
})
205+
it('should not render legend cause no legend is provided', async () => {
206+
// When
207+
const { container } = render(DsfrCheckboxSet, {
208+
props: {
209+
options,
210+
},
211+
})
212+
213+
// Then
214+
expect(container.querySelectorAll('legend')).toHaveLength(0)
215+
})
216+
})
158217
})

src/components/DsfrCheckbox/DsfrCheckboxSet.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const props = withDefaults(defineProps<DsfrCheckboxSetProps>(), {
1919
modelValue: () => [],
2020
})
2121
22+
defineSlots<{
23+
default?: () => any
24+
legend?: () => any
25+
'required-tip'?: () => any
26+
}>()
27+
2228
const message = computed(() => {
2329
return props.errorMessage || props.validMessage
2430
})
@@ -45,6 +51,7 @@ const modelValue = defineModel()
4551
:role="(errorMessage || validMessage) ? 'group' : undefined"
4652
>
4753
<legend
54+
v-if="legend || $slots.legend"
4855
:id="titleId"
4956
class="fr-fieldset__legend fr-text--regular"
5057
>

0 commit comments

Comments
 (0)