Skip to content

Commit

Permalink
fix: make also #tabs title set as a component possible to `<Tabs.Cont…
Browse files Browse the repository at this point in the history
…ent title={<></>} ... />`
  • Loading branch information
tujoworker committed Sep 26, 2019
1 parent 3cc76a1 commit f0fa463
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
14 changes: 6 additions & 8 deletions packages/dnb-ui-lib/src/components/tabs/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import classnames from 'classnames'
import keycode from 'keycode'
import Context from '../../shared/Context'
import {
slugify,
isTrue,
makeUniqueId,
registerElement,
Expand Down Expand Up @@ -602,7 +603,11 @@ class ContentWrapper extends PureComponent {
class CustomContent extends PureComponent {
static propTypes = {
displayName: PropTypes.string,
title: PropTypes.string, // eslint-disable-line
title: PropTypes.oneOfType([
PropTypes.object,
PropTypes.node,
PropTypes.func
]), // eslint-disable-line
hash: PropTypes.string, // eslint-disable-line
selected: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), // eslint-disable-line
disabled: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), // eslint-disable-line
Expand Down Expand Up @@ -646,13 +651,6 @@ class CustomContent extends PureComponent {
}
}

const slugify = s =>
s
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '')

Tabs.Content = CustomContent
Tabs.ContentWrapper = ContentWrapper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ exports[`Tabs component have to match snapshot 1`] = `
"displayName": "displayName",
"hash": "hash",
"selected": "selected",
"title": "title",
"title": Object {},
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
transformToReactEventCase,
pickRenderProps,
detectOutsideClick,
makeUniqueId
makeUniqueId,
slugify
} from '../component-helper'

beforeAll(() => {
Expand Down Expand Up @@ -363,3 +364,15 @@ describe('"makeUniqueId" should', () => {
)
})
})

describe('"slugify" should', () => {
it('have a correctly slugifyed string', () => {
expect(slugify('What ever !#.- 0123')).toEqual('what-ever-0123')
})
it('also if we send in only a number a correctly slugifyed string', () => {
expect(slugify(123)).toEqual('123')
})
it('or other types', () => {
expect(slugify({ foo: 'bar' })).toEqual('object-object')
})
})
7 changes: 7 additions & 0 deletions packages/dnb-ui-lib/src/shared/component-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,10 @@ export const makeUniqueId = (pendix = '', length = 8) =>
.substr(2, length) + idIncrement++
).slice(-length)
let idIncrement = 0

export const slugify = s =>
String(s)
.toLowerCase()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '')
2 changes: 1 addition & 1 deletion packages/dnb-ui-lib/stories/components/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default [
<Tabs.Content title="First">
<H2>First</H2>
</Tabs.Content>
<Tabs.Content title="Second" selected>
<Tabs.Content title={<>Second as component</>} selected>
<H2>Second</H2>
</Tabs.Content>
</Tabs>
Expand Down

0 comments on commit f0fa463

Please sign in to comment.