Skip to content

Commit

Permalink
fix(FormLabel): don't apply margin-right when no content was given (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker authored Nov 22, 2023
1 parent 693f9d4 commit 057f957
Show file tree
Hide file tree
Showing 28 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ html[data-visual-test] .dnb-tooltip--hide {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down Expand Up @@ -1930,7 +1929,6 @@ button.dnb-button::-moz-focus-inner {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ exports[`Checkbox scss has to match style dependencies css 1`] = `
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ html[data-visual-test] .dnb-tooltip--hide {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down Expand Up @@ -1844,7 +1843,6 @@ button.dnb-button::-moz-focus-inner {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down Expand Up @@ -2447,7 +2445,6 @@ button.dnb-button::-moz-focus-inner {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down Expand Up @@ -2851,7 +2848,6 @@ html[data-whatinput=keyboard] .dnb-checkbox__status--error .dnb-checkbox__input:
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down Expand Up @@ -3510,7 +3506,6 @@ button .dnb-form-status__text {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,6 @@ button.dnb-button::-moz-focus-inner {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
11 changes: 8 additions & 3 deletions packages/dnb-eufemia/src/components/form-label/FormLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
skeletonDOMAttributes,
} from '../skeleton/SkeletonHelper'
import { pickFormElementProps } from '../../shared/helpers/filterValidProps'
import { omitSpacingProps } from '../flex/utils'
import Context from '../../shared/Context'
import {
import type {
DynamicElement,
DynamicElementParams,
SpacingProps,
Expand Down Expand Up @@ -83,6 +84,8 @@ export default function FormLabel(localProps: FormLabelAllProps) {
...attributes
} = props

const content = text || children

const isInteractive =
!props.disabled &&
!srOnly &&
Expand All @@ -97,7 +100,9 @@ export default function FormLabel(localProps: FormLabelAllProps) {
size && `dnb-h--${size}`,
isInteractive && 'dnb-form-label--interactive',
createSkeletonClass('font', skeleton, context),
createSpacingClasses(props),
createSpacingClasses(
content ? { right: 'small', ...props } : omitSpacingProps(props)
),
className
),
htmlFor: forId || for_id,
Expand All @@ -109,7 +114,7 @@ export default function FormLabel(localProps: FormLabelAllProps) {
skeletonDOMAttributes(params, skeleton, context)
validateDOMAttributes(localProps, params)

return <Element {...params}>{text || children}</Element>
return <Element {...params}>{content}</Element>
}

FormLabel._formElement = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,22 @@ describe('FormLabel component', () => {
)
})

it('should have default spacing', () => {
const { rerender } = render(<FormLabel forId="input" text="Label" />)

const element = document.querySelector('.dnb-form-label')

expect(Array.from(element.classList)).toContain(
'dnb-space__right--small'
)

rerender(<FormLabel forId="input" text="Label" />)

expect(Array.from(element.classList)).not.toContain('dnb-space__right')
})

it('should support spacing props', () => {
render(<FormLabel forId="input" top="large" />)
render(<FormLabel forId="input" top="large" text="Label" />)

const element = document.querySelector('.dnb-form-label')

Expand All @@ -31,6 +45,14 @@ describe('FormLabel component', () => {
)
})

it('should remove spacing props when no label was given', () => {
render(<FormLabel forId="input" top="large" />)

const element = document.querySelector('.dnb-form-label')

expect(Array.from(element.classList)).not.toContain('dnb-space')
})

it('should set correct class when srOnly is set', () => {
render(<FormLabel forId="input" srOnly />)

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.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ exports[`FormLabel scss has to match style dependencies css 1`] = `
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

width: auto;
padding: 0;
margin-right: 1rem;

text-align: left;
vertical-align: baseline;
Expand All @@ -27,10 +26,6 @@
margin-bottom: 0.5rem;
}

// Is not in use
// &--vertical-label {
// }

&[for]:not([disabled]) {
user-select: none; // Safari / Touch fix
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ html[data-visual-test] .dnb-tooltip--hide {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
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.
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,6 @@ html[data-visual-test] .dnb-tooltip--hide {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ exports[`Radio scss has to match style dependencies css 1`] = `
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,6 @@ html[data-visual-test] .dnb-tooltip--hide {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ exports[`Switch scss has to match style dependencies css 1`] = `
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ exports[`Textarea scss has to match style dependencies css 1`] = `
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ exports[`ToggleButton scss has to match style dependencies css 1`] = `
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down Expand Up @@ -629,7 +628,6 @@ button.dnb-button::-moz-focus-inner {
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
Expand Down Expand Up @@ -1033,7 +1031,6 @@ html[data-whatinput=keyboard] .dnb-checkbox__status--error .dnb-checkbox__input:
display: inline-block;
width: auto;
padding: 0;
margin-right: 1rem;
text-align: left;
vertical-align: baseline;
white-space: pre-wrap;
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.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('FieldBlock', () => {

expect(labelElement).toBeInTheDocument()
expect(labelElement.className).toBe(
'dnb-form-label dnb-space__top--zero dnb-space__bottom--x-small'
'dnb-form-label dnb-space__right--small dnb-space__top--zero dnb-space__bottom--x-small'
)
expect(labelElement.textContent).toBe('A Label Description')
})
Expand All @@ -123,7 +123,7 @@ describe('FieldBlock', () => {

expect(labelElement).toBeInTheDocument()
expect(labelElement.className).toBe(
'dnb-form-label dnb-space__top--zero dnb-space__bottom--x-small'
'dnb-form-label dnb-space__right--small dnb-space__top--zero dnb-space__bottom--x-small'
)
expect(labelElement.textContent).toBe('A Secondary Label')
})
Expand Down

0 comments on commit 057f957

Please sign in to comment.