Skip to content

Commit

Permalink
fix: fix support for components inside labels to show up in GlobalStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Oct 13, 2021
1 parent 4001681 commit f43d14d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 13 deletions.
29 changes: 29 additions & 0 deletions packages/dnb-eufemia-sandbox/stories/components/GlobalStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ export default {
title: 'Eufemia/Components/GlobalStatus',
}

export const ComponentAsLabel = () => {
const [status, setStatus] = React.useState(null)

const Component = () => {
return 'my label'
}

return (
<>
<GlobalStatus id="test" />

<FormSet label_direction="vertical" global_status_id="test">
<ToggleButton
bottom
on_change={() => setStatus((s) => (!s ? 'min status' : null))}
>
set status
</ToggleButton>

<FormRow>
<Input label={<Component />} status={status} />
</FormRow>
<Input label={<Component />} status={status} />
<Input label={<Component />} status={status} />
</FormSet>
</>
)
}

const CustomStatus = () => (
<>
<H2>Custom Status</H2>
Expand Down
33 changes: 21 additions & 12 deletions packages/dnb-eufemia/src/components/global-status/GlobalStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
registerElement,
validateDOMAttributes,
dispatchCustomElementEvent,
convertJsxToString,
processChildren,
extendPropsWithContext,
} from '../../shared/component-helper'
Expand Down Expand Up @@ -49,7 +48,7 @@ export default class GlobalStatus extends React.PureComponent {
static propTypes = {
id: PropTypes.string,
status_id: PropTypes.string,
title: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
title: PropTypes.oneOfType([PropTypes.node, PropTypes.bool]),
default_title: PropTypes.string,
text: PropTypes.oneOfType([
PropTypes.string,
Expand All @@ -73,7 +72,7 @@ export default class GlobalStatus extends React.PureComponent {
autoclose: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
no_animation: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
delay: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
close_text: PropTypes.string,
close_text: PropTypes.node,
hide_close_button: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
Expand All @@ -86,7 +85,7 @@ export default class GlobalStatus extends React.PureComponent {
PropTypes.string,
PropTypes.bool,
]),
status_anchor_text: PropTypes.string,
status_anchor_text: PropTypes.node,
skeleton: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),

...spacingPropTypes,
Expand Down Expand Up @@ -611,14 +610,24 @@ export default class GlobalStatus extends React.PureComponent {
item.id || item.status_id
? `${item.status_id}-${i}`
: makeUniqueId()
const label = React.isValidElement(item.status_anchor_label)
? convertJsxToString(item.status_anchor_label)
: item.status_anchor_label || ''
const anchorText = String(
item.status_anchor_text || status_anchor_text
)
.replace('%s', label)
.replace(/[: ]$/g, '')

let anchorText = status_anchor_text

if (React.isValidElement(item.status_anchor_label)) {
anchorText = (
<>
{typeof status_anchor_text === 'string'
? status_anchor_text.replace('%s', '').trim()
: status_anchor_text}{' '}
{item.status_anchor_label}
</>
)
} else {
anchorText = String(item.status_anchor_text || status_anchor_text)
.replace('%s', item.status_anchor_label || '')
.replace(/[: ]$/g, '')
}

const useAutolink = item.status_id && isTrue(item.status_anchor_url)

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,53 @@ describe('GlobalStatus component', () => {
)
})

it('should support component given as labels', async () => {
const ToggleStatus = () => {
const [status, setStatus] = React.useState(null)

const Component = () => {
return 'my-label'
}

return (
<Switch
id="switch"
label={<Component />}
status={status}
status_no_animation={true}
global_status_id="main-to-be-empty"
on_change={({ checked }) => {
setStatus(checked ? 'error-message' : null)
}}
/>
)
}
const Comp = mount(
<>
<Component
id="main-to-be-empty"
autoscroll={false}
delay={0}
no_animation={true}
status_anchor_text={<span>custon anchor text</span>}
/>
<ToggleStatus />
</>
)

Comp.find('input#switch').simulate('change')

expect(Comp.find('.dnb-global-status__message p').at(0).text()).toBe(
'error-message'
)
expect(
Comp.find('.dnb-global-status__message__content ul li')
.at(0)
.find('a.dnb-anchor')
.text()
).toBe('custon anchor text my-label')
})

it('has to have a working auto close', () => {
const on_open = jest.fn()
const on_close = jest.fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,11 @@ Array [
onClick={[Function]}
onKeyDown={[Function]}
>
Gå til Label
Gå til
<span>
Label
</span>
</a>
</li>
</ul>
Expand Down

0 comments on commit f43d14d

Please sign in to comment.