Skip to content

Commit

Permalink
fix(Tag): properly handle spacing props
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed May 20, 2022
1 parent 5841990 commit d7129d8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/dnb-eufemia/src/components/tag/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { usePropsWithContext } from '../../shared/hooks'
// Internal
import TagGroup from './TagGroup'
import { TagGroupContext } from './TagContext'
import { createSpacingClasses } from '../space/SpacingHelper'

export interface TagProps {
/**
Expand Down Expand Up @@ -83,6 +84,14 @@ const Tag = (localProps: TagProps & ISpacingProps) => {
const tagGroupContext = React.useContext(TagGroupContext)

// Extract additional props from global context
const allProps = usePropsWithContext(
localProps,
defaultProps,
context?.translation?.Tag,
context?.Tag,
tagGroupContext
)

const {
className,
skeleton,
Expand All @@ -93,25 +102,22 @@ const Tag = (localProps: TagProps & ISpacingProps) => {
onDelete,
omitOnKeyUpDeleteEvent,
...props
} = usePropsWithContext(
localProps,
defaultProps,
context?.translation?.Tag,
context?.Tag,
tagGroupContext
)
} = allProps

const content = text || children
const isClickable = !!onClick
const isRemovable = !!onDelete && !isClickable
const isInteractive = isClickable || isRemovable

const spacingClasses = createSpacingClasses(props)
const tagClassNames = classnames(
'dnb-tag',
className,
spacingClasses,
isInteractive && 'dnb-tag--interactive',
isRemovable && 'dnb-tag--removable'
)
const buttonAttr: typeof props & Pick<ButtonProps, 'element' | 'type'> =
props

const isDeleteKeyboardEvent = (keyboardEvent) => {
return (
Expand All @@ -125,9 +131,6 @@ const Tag = (localProps: TagProps & ISpacingProps) => {
}
}

const buttonAttr: typeof props & Pick<ButtonProps, 'element' | 'type'> =
props

if (!isInteractive) {
buttonAttr.element = 'span'
buttonAttr.type = ''
Expand Down
42 changes: 42 additions & 0 deletions packages/dnb-eufemia/src/components/tag/__tests__/Tag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ describe('Tag Group', () => {
skeletonClassName
)
})

it('should support spacing props', () => {
render(
<Tag.Group label="tags" top="2rem">
<Tag>Tag</Tag>
</Tag.Group>
)

const element = screen.getByTestId('tag-group')
const attributes = Array.from(element.attributes).map(
(attr) => attr.name
)

expect(attributes).toEqual(['class', 'data-testid'])
expect(Array.from(element.classList)).toEqual([
'dnb-tag__group',
'dnb-space__top--large',
])
})
})

describe('Tag', () => {
Expand Down Expand Up @@ -195,6 +214,29 @@ describe('Tag', () => {
).toBeTruthy()
})

it('should support spacing props', () => {
render(
<Tag.Group label="tags">
<Tag left="2rem">Tag</Tag>
</Tag.Group>
)

const element = screen.getByTestId('tag')
const attributes = Array.from(element.attributes).map(
(attr) => attr.name
)

expect(attributes).toEqual(['class', 'data-testid'])
expect(Array.from(element.classList)).toEqual([
'dnb-button',
'dnb-button--unstyled',
'dnb-button--has-text',
'dnb-space__left--large',
'dnb-tag',
'dnb-button--size-small',
])
})

describe('with onClick', () => {
it('renders a clickable tag with the correct attributes if onClick is defined', () => {
const clickableClassName = 'dnb-tag--interactive'
Expand Down

0 comments on commit d7129d8

Please sign in to comment.