Skip to content

Commit

Permalink
fix(Field.Toggle): removes invisible label when label not provided (#…
Browse files Browse the repository at this point in the history
…3984)

Fixes https://jira.tech.dnb.no/browse/DCE-12453

---------

Co-authored-by: Tobias Høegh <[email protected]>
  • Loading branch information
langz and tujoworker authored Sep 24, 2024
1 parent a122fa2 commit c945d5b
Show file tree
Hide file tree
Showing 29 changed files with 129 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,18 @@ export const VariantButtonsWithHelp = () => {
</ComponentBox>
)
}

export const VariantButtonsWithoutLabel = () => {
return (
<ComponentBox data-visual-test="toggle-variant-buttons-without-label">
Text above the toggle:
<Field.Toggle
valueOn="on"
valueOff="off"
variant="buttons"
value="on"
onChange={(value) => console.log('onChange', value)}
/>
</ComponentBox>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ import * as Examples from './Examples'

<Examples.VariantButtonsWithHelp />

<VisibleWhenVisualTest>
<Examples.VariantButtonsWithoutLabel />
</VisibleWhenVisualTest>

#### Checkbox button

<Examples.VariantCheckboxButton />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ describe('Field.Toggle', () => {
})
expect(screenshot).toMatchImageSnapshot()
})

it('have to match buttons variant without label', async () => {
const screenshot = await makeScreenshot({
url,
selector:
'[data-visual-test="toggle-variant-buttons-without-label"]',
})
expect(screenshot).toMatchImageSnapshot()
})
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ export function Toggle() {
validateInitially
onChange={(value) => console.log('onChange', value)}
/>
<>
No label:
<Field.Toggle
variant="buttons"
valueOn="checked"
valueOff="unchecked"
required
validateInitially
onChange={(value) => console.log('onChange', value)}
/>
</>
</>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ function FieldBlock(props: Props) {
)
}

const hasLabelDescription = isFragment(labelDescription)
? fragmentHasChildren(labelDescription) &&
!fragmentHasOnlyUndefinedChildren(labelDescription)
: labelDescription

return (
<FieldBlockContext.Provider
value={{
Expand All @@ -457,7 +462,7 @@ function FieldBlock(props: Props) {
<FormLabel {...labelProps}>
<SubmitIndicator state={fieldState}>
{label}
{labelDescription && (
{hasLabelDescription && (
<span className="dnb-forms-field-block__label-description">
{labelDescription}
</span>
Expand Down Expand Up @@ -571,6 +576,26 @@ export function getMessage(item: Partial<StateWithMessage>): StateMessage {
content) as StateMessage
}

function isFragment(fragment: React.ReactNode) {
return React.isValidElement(fragment) && fragment.type === React.Fragment
}

function fragmentHasChildren(fragment: React.ReactNode) {
return (
React.isValidElement(fragment) &&
React.Children.count(fragment.props.children) > 0
)
}

function fragmentHasOnlyUndefinedChildren(fragment: React.ReactNode) {
const isUndefined = (child) => child === undefined

return (
React.isValidElement(fragment) &&
React.Children.toArray(fragment.props.children).every(isUndefined)
)
}

FieldBlock._supportsSpacingProps = true

export default FieldBlock
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,72 @@ describe('FieldBlock', () => {
expect(labelElement).toHaveTextContent('A Label')
})

it('should render a "labelDescription"', () => {
render(
<FieldBlock labelDescription="A Label Description">
content
</FieldBlock>
)
describe('labelDescription', () => {
it('should render with a string', () => {
render(
<FieldBlock labelDescription="A Label Description">
content
</FieldBlock>
)

const labelElement = document.querySelector('label')
const labelElement = document.querySelector('label')

expect(labelElement).toBeInTheDocument()
expect(labelElement).toHaveClass(
'dnb-form-label dnb-space__right--small dnb-space__top--zero dnb-space__bottom--x-small'
)
expect(labelElement).toHaveTextContent('A Label Description')
expect(labelElement).toBeInTheDocument()
expect(labelElement).toHaveClass(
'dnb-form-label dnb-space__right--small dnb-space__top--zero dnb-space__bottom--x-small'
)
expect(labelElement).toHaveTextContent('A Label Description')
})

it('should render with JSX content', () => {
render(
<FieldBlock labelDescription={<span>A Label Description</span>}>
content
</FieldBlock>
)

const labelElement = document.querySelector('label')

expect(labelElement).toBeInTheDocument()
expect(labelElement).toHaveClass(
'dnb-form-label dnb-space__right--small dnb-space__top--zero dnb-space__bottom--x-small'
)
expect(labelElement).toHaveTextContent('A Label Description')
})

it('should render with a React element', () => {
const LabelDescription = () => <span>A Label Description</span>

render(
<FieldBlock labelDescription={<LabelDescription />}>
content
</FieldBlock>
)

const labelElement = document.querySelector('label')

expect(labelElement).toBeInTheDocument()
expect(labelElement).toHaveClass(
'dnb-form-label dnb-space__right--small dnb-space__top--zero dnb-space__bottom--x-small'
)
expect(labelElement).toHaveTextContent('A Label Description')
})

it('should not render when empty fragment is given', () => {
render(<FieldBlock labelDescription={<></>}>content</FieldBlock>)

expect(
document.querySelector('.dnb-forms-field-block__label-description')
).toBeNull()

const labelElement = document.querySelector('label')

expect(labelElement).toBeInTheDocument()
expect(labelElement).toHaveClass(
'dnb-form-label dnb-space__right--small dnb-space__top--zero dnb-space__bottom--x-small'
)
expect(labelElement).toHaveTextContent('')
})
})

describe('labelSuffix', () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c945d5b

Please sign in to comment.