Skip to content

Commit 093e819

Browse files
authored
feat: add hasAsteriskJustAfterLabel props
2 parents 35d742a + c46ed8c commit 093e819

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ props: {
133133
type: String,
134134
required: true
135135
},
136+
hasAsteriskJustAfterLabel: {
137+
type: Boolean,
138+
default: false
139+
},
136140
mandatoryAsteriskLegend: {
137141
type: String,
138142
default: '* field required'

src/components/Fields/Label.spec.js

+42-41
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,84 @@ import matchers from 'jest-vue-matcher'
22
import { mount } from '@vue/test-utils'
33
import Label from '@/components/Fields/Label'
44

5+
const HELP = '(help content)'
56
const LABEL = 'the label'
7+
const ITEM = {
8+
help: HELP,
9+
label: LABEL
10+
}
611
let wrapper
712

8-
describe('Label', () => {
9-
beforeEach(() => {
10-
wrapper = mount(Label, {
11-
propsData: {
12-
item: {
13-
label: LABEL
14-
}
15-
}
16-
})
17-
expect.extend(matchers(wrapper))
13+
const setup = ({
14+
hasAsteriskJustAfterLabel = false,
15+
item = ITEM
16+
} = {}) => {
17+
wrapper = mount(Label, {
18+
propsData: {
19+
item
20+
},
21+
computed: {
22+
hasAsteriskJustAfterLabel: () => hasAsteriskJustAfterLabel
23+
}
1824
})
25+
expect.extend(matchers(wrapper))
26+
return wrapper
27+
}
1928

29+
describe('Label', () => {
2030
afterEach(() => {
2131
wrapper.destroy()
2232
})
2333

2434
it('shows a label', () => {
35+
setup()
2536
expect('label').toHaveText(LABEL)
2637
})
2738

28-
it('shows an alternative label', async () => {
39+
it('shows an alternative label', () => {
2940
const ALTERNATIVE_LABEL = 'an alternative label'
30-
31-
await wrapper.setProps({
32-
item: {
33-
label: LABEL,
34-
alternativeLabel: ALTERNATIVE_LABEL
35-
}
36-
})
41+
const item = { ...ITEM, alternativeLabel: ALTERNATIVE_LABEL }
42+
setup({ item })
3743

3844
expect('label').toHaveText(ALTERNATIVE_LABEL)
3945
expect('label').not.toHaveText(LABEL)
4046
})
4147

42-
it('hides the label', async () => {
43-
await wrapper.setProps({
44-
item: {
45-
label: LABEL,
46-
showLabel: false
47-
}
48-
})
48+
it('hides the label', () => {
49+
const item = { ...ITEM, showLabel: false }
50+
setup({ item })
4951

5052
expect('label').not.toBeADomElement()
5153
})
5254

5355
it('has a for attribute by default', () => {
56+
setup()
57+
5458
expect('label').toHaveAttribute('for', 'the-label')
5559
})
5660

57-
it('has a custom for attribute', async () => {
61+
it('has a custom for attribute', () => {
5862
const ATTR_FOR = 'a-custom-for-attribute'
59-
60-
await wrapper.setProps({
61-
item: {
62-
label: LABEL,
63-
for: ATTR_FOR
64-
}
65-
})
63+
const item = { ...ITEM, for: ATTR_FOR }
64+
setup({ item })
6665

6766
expect('label').toHaveAttribute('for', ATTR_FOR)
6867
})
6968

7069
it('is mandatory by default', () => {
71-
expect('label').toHaveText('*')
70+
setup()
71+
expect('label').toHaveText(`${LABEL}${HELP}*`)
7272
})
7373

74-
it('is not mandatory', async () => {
75-
await wrapper.setProps({
76-
item: {
77-
label: LABEL,
78-
isRequired: false
79-
}
80-
})
74+
it('is not mandatory', () => {
75+
const item = { ...ITEM, isRequired: false }
76+
setup({ item })
8177

8278
expect('label').not.toHaveText('*')
8379
})
80+
81+
it('put the asterisk just after the label', () => {
82+
setup({ hasAsteriskJustAfterLabel: true })
83+
expect('label').toHaveText(`${LABEL}*${HELP}`)
84+
})
8485
})

src/components/Fields/Label.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<template lang="pug">
22
label.label(:for="item.for || item.label | slugify", v-if="item.showLabel !== false")
33
p {{ item.alternativeLabel || item.label }}
4+
span(v-if="item.isRequired !== false && hasAsteriskJustAfterLabel")
5+
sup.has-text-grey-light.is-size-7 *
46
span.helpLabel.has-text-grey-light.is-size-7.is-italic(v-if="item.help") {{ item.help }}
5-
span(v-if="item.isRequired !== false")
7+
span(v-if="item.isRequired !== false && !hasAsteriskJustAfterLabel")
68
sup.has-text-grey-light.is-size-7 *
79

810
</template>
@@ -20,6 +22,11 @@ export default {
2022
type: Object,
2123
required: true
2224
}
25+
},
26+
computed: {
27+
hasAsteriskJustAfterLabel () {
28+
return this.$parent.$parent.hasAsteriskJustAfterLabel
29+
}
2330
}
2431
}
2532
</script>

src/components/Form/Form.vue

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ export default {
8989
type: String,
9090
required: true
9191
},
92+
hasAsteriskJustAfterLabel: {
93+
type: Boolean,
94+
default: false
95+
},
9296
mandatoryAsteriskLegend: {
9397
type: String,
9498
default: '* field required'

0 commit comments

Comments
 (0)