Skip to content

Commit

Permalink
feat(b-icon): add proper title support (closes #5711) (#5724)
Browse files Browse the repository at this point in the history
* update icon-base.js

* update package.json

* Add tests

* Improved default title handling

* Update icon-base.js

Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
  • Loading branch information
Hiws and jacobmllr95 authored Sep 3, 2020
1 parent 6a892f0 commit 3756b2c
Show file tree
Hide file tree
Showing 8 changed files with 5,722 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/icons/helpers/icon-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { toFloat } from '../../utils/number'

// Common icon props (should be cloned/spread before using)
export const commonIconProps = {
title: {
type: String
// default: null
},
variant: {
type: String,
default: null
Expand Down Expand Up @@ -132,6 +136,8 @@ export const BVIconBase = /*#__PURE__*/ Vue.extend({
$inner = h('g', {}, [$inner])
}

const $title = props.title ? h('title', props.title) : null

return h(
'svg',
mergeData(
Expand All @@ -156,7 +162,7 @@ export const BVIconBase = /*#__PURE__*/ Vue.extend({
}
}
),
[$inner]
[$title, $inner]
)
}
})
20 changes: 15 additions & 5 deletions src/icons/helpers/make-icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const makeIcon = (name, content) => {
const kebabName = kebabCase(name)
const iconName = `BIcon${pascalCase(name)}`
const iconNameClass = `bi-${kebabName}`
const iconTitle = kebabName.replace(/-/g, ' ')
const svgContent = trim(content || '')
// Return the icon component definition
return /*#__PURE__*/ Vue.extend({
Expand All @@ -31,11 +32,20 @@ export const makeIcon = (name, content) => {
render(h, { data, props }) {
return h(
BVIconBase,
mergeData(data, {
staticClass: iconNameClass,
props: { ...props, content: svgContent },
attrs: { 'aria-label': kebabName.replace(/-/g, ' ') }
})
mergeData(
// Defaults
{
props: { title: iconTitle },
attrs: { 'aria-label': iconTitle }
},
// User data
data,
// Required data
{
staticClass: iconNameClass,
props: { ...props, content: svgContent }
}
)
)
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/icons/icons.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// --- BEGIN AUTO-GENERATED FILE ---
//
// @IconsVersion: 1.0.0
// @Generated: 2020-09-01T12:06:08.751Z
// @Generated: 2020-09-03T14:07:12.302Z
//
// This file is generated on each build. Do not edit this file!

Expand Down
2 changes: 1 addition & 1 deletion src/icons/icons.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// --- BEGIN AUTO-GENERATED FILE ---
//
// @IconsVersion: 1.0.0
// @Generated: 2020-09-01T12:06:08.751Z
// @Generated: 2020-09-03T14:07:12.302Z
//
// This file is generated on each build. Do not edit this file!

Expand Down
42 changes: 42 additions & 0 deletions src/icons/icons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,46 @@ describe('icons', () => {

wrapper.destroy()
})

it('b-icon title prop works', async () => {
const wrapper = mount(BIcon, {
localVue,
propsData: {
icon: 'circle-fill',
title: 'Circle'
}
})

expect(wrapper.exists()).toBe(true)
expect(wrapper.element.tagName).toBe('svg')
expect(wrapper.classes()).toContain('b-icon')
expect(wrapper.classes()).toContain('bi')
expect(wrapper.classes()).toContain('bi-circle-fill')

const $title = wrapper.find('title')
expect($title.exists()).toBe(true)
expect($title.text()).toBe('Circle')

wrapper.destroy()
})

it('b-icon <title> should not render when title is undefined', async () => {
const wrapper = mount(BIcon, {
localVue,
propsData: {
icon: 'circle-fill'
}
})

expect(wrapper.exists()).toBe(true)
expect(wrapper.element.tagName).toBe('svg')
expect(wrapper.classes()).toContain('b-icon')
expect(wrapper.classes()).toContain('bi')
expect(wrapper.classes()).toContain('bi-circle-fill')

const $title = wrapper.find('title')
expect($title.exists()).toBe(false)

wrapper.destroy()
})
})
40 changes: 40 additions & 0 deletions src/icons/iconstack.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,44 @@ describe('icons > b-iconstack', () => {

wrapper.destroy()
})

it('b-iconstack title prop works', async () => {
const wrapper = mount(BIconstack, {
propsData: {
icon: 'circle-fill',
title: 'Circle'
}
})

expect(wrapper.exists()).toBe(true)
expect(wrapper.element.tagName).toBe('svg')
expect(wrapper.classes()).toContain('b-icon')
expect(wrapper.classes()).toContain('b-iconstack')
expect(wrapper.classes()).toContain('bi')

const $title = wrapper.find('title')
expect($title.exists()).toBe(true)
expect($title.text()).toBe('Circle')

wrapper.destroy()
})

it('b-iconstack <title> should not render when title is undefined', async () => {
const wrapper = mount(BIconstack, {
propsData: {
icon: 'circle-fill'
}
})

expect(wrapper.exists()).toBe(true)
expect(wrapper.element.tagName).toBe('svg')
expect(wrapper.classes()).toContain('b-icon')
expect(wrapper.classes()).toContain('b-iconstack')
expect(wrapper.classes()).toContain('bi')

const $title = wrapper.find('title')
expect($title.exists()).toBe(false)

wrapper.destroy()
})
})
Loading

0 comments on commit 3756b2c

Please sign in to comment.