Skip to content

Commit

Permalink
fixup! feat: add title prop to #icon, refactor and fix auto sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Jan 23, 2019
1 parent ba3cab3 commit 989e619
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports[`FormStatus component have to match snapshot 1`] = `
width={null}
>
<span
aria-label="nodejs"
aria-label="exclamation"
className="dnb-icon"
role="img"
title={null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`IconPrimary component have to match snapshot 1`] = `
width={null}
>
<span
aria-label="nodejs"
aria-label="question"
className="dnb-icon dnb-icon--default"
role="img"
title={null}
Expand Down
25 changes: 17 additions & 8 deletions packages/dnb-ui-lib/src/components/icon/Icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,26 @@ export default class Icon extends PureComponent {
return processChildren(props)
}

static getIconNameFromComponent(props) {
return typeof props.icon === 'string'
? props.icon
: props.icon &&
typeof props.icon === 'object' &&
(props.icon.displayName || props.icon.name)
}

static calcSize(props) {
const { size, height, width } = props

let sizeAsInt = -1
let sizeAsString = null

// get the icon name - we use is for several things
const name =
typeof props.icon === 'string'
? props.icon
: props.icon.displayName || props.icon.name

// if there is no size, check if we can find the actuall size in the name
if (!size || size === DefaultIconSize) {
const nameParts = (name || '').split('_')
// get the icon name - we use is for several things
const name = Icon.getIconNameFromComponent(props)

const nameParts = String(name || '').split('_')
if (nameParts.length > 1) {
const lastPartOfIconName = nameParts.reverse()[0]
const potentialSize = ListDefaultIconSizes.filter(
Expand Down Expand Up @@ -243,8 +248,12 @@ export default class Icon extends PureComponent {
role: 'img',
title
})

// get the alt
wrapperParams['aria-label'] = (alt || title || name).replace(/_/g, ' ')
wrapperParams['aria-label'] = String(
alt || title || Icon.getIconNameFromComponent(props)
).replace(/_/g, ' ')

if (area_hidden) {
// wrapperParams['role'] = 'presentation' // almost the same as aria-hidden
wrapperParams['aria-hidden'] = area_hidden
Expand Down

0 comments on commit 989e619

Please sign in to comment.