Skip to content

feat(form-checkbox/radio): allow no label in plain mode (fixes #2911) #2912

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

Merged
merged 2 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
58 changes: 58 additions & 0 deletions src/components/form-checkbox/form-checkbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ describe('form-checkbox', () => {
wrapper.destroy()
})

it('default does not have aria-label attribute on input', async () => {
const wrapper = mount(Input, {
propsData: {
checked: false
},
slots: {
default: 'foobar'
}
})
expect(wrapper.find('input').attributes('aria-label')).not.toBeDefined()

wrapper.destroy()
})

it('has aria-label attribute on input when aria-label provided', async () => {
const wrapper = mount(Input, {
propsData: {
checked: false,
ariaLabel: 'bar'
},
slots: {
default: 'foo'
}
})
expect(wrapper.find('input').attributes('aria-label')).toBe('bar')

wrapper.destroy()
})

it('default has input class custom-control-input', async () => {
const wrapper = mount(Input, {
propsData: {
Expand All @@ -70,6 +99,7 @@ describe('form-checkbox', () => {
const input = wrapper.find('input')
expect(input.classes().length).toEqual(1)
expect(input.classes()).toContain('custom-control-input')
expect(input.classes()).not.toContain('position-static')

wrapper.destroy()
})
Expand Down Expand Up @@ -481,6 +511,34 @@ describe('form-checkbox', () => {
wrapper.destroy()
})

it('plain does not have class position-static when label provided', async () => {
const wrapper = mount(Input, {
propsData: {
plain: true,
checked: false
},
slots: {
default: 'foobar'
}
})
expect(wrapper.find('input').classes()).not.toContain('position-static')

wrapper.destroy()
})

it('plain has no label when no default slot content', async () => {
const wrapper = mount(Input, {
propsData: {
plain: true,
checked: false
}
})
expect(wrapper.find('label').exists()).toBe(false)
expect(wrapper.find('input').classes()).toContain('position-static')

wrapper.destroy()
})

it('plain has no input validation classes by default', async () => {
const wrapper = mount(Input, {
propsData: {
Expand Down
37 changes: 25 additions & 12 deletions src/mixins/form-radio-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export default {
// Only applicable when rendered with button style
type: String,
default: null
},
ariaLabel: {
// Placed on the input if present.
type: String,
default: null
}
},
data() {
Expand Down Expand Up @@ -172,7 +177,9 @@ export default {
'form-check-input': this.is_Plain,
'custom-control-input': this.is_Custom,
'is-valid': this.get_State === true && !this.is_BtnMode,
'is-invalid': this.get_State === false && !this.is_BtnMode
'is-invalid': this.get_State === false && !this.is_BtnMode,
// https://github.com/bootstrap-vue/bootstrap-vue/issues/2911
'position-static': this.is_Plain && !defaultSlot
},
directives: [
{
Expand All @@ -190,7 +197,8 @@ export default {
disabled: this.is_Disabled,
required: this.is_Required,
autocomplete: 'off',
'aria-required': this.is_Required || null
'aria-required': this.is_Required || null,
'aria-label': this.ariaLabel || null
},
domProps: {
value: this.value,
Expand All @@ -209,17 +217,22 @@ export default {
return button
} else {
// Not button mode
const label = h(
'label',
{
class: {
'form-check-label': this.is_Plain,
'custom-control-label': this.is_Custom
let label = h(false)
// If no label content in plain mode we dont render the label
// https://github.com/bootstrap-vue/bootstrap-vue/issues/2911
if (!(this.is_Plain && !defaultSlot)) {
label = h(
'label',
{
class: {
'form-check-label': this.is_Plain,
'custom-control-label': this.is_Custom
},
attrs: { for: this.safeId() }
},
attrs: { for: this.safeId() }
},
defaultSlot
)
defaultSlot
)
}
// Wrap it in a div
return h(
'div',
Expand Down