Skip to content

Commit

Permalink
feat(Forms): add labelSrOnly to Value.* components (#4319)
Browse files Browse the repository at this point in the history
Based on #4311

---------

Co-authored-by: -l <anderslangseth@gmail.com>
  • Loading branch information
tujoworker and langz authored Nov 21, 2024
1 parent b6621c2 commit 46f68ae
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ import { SummaryListProperties } from '@dnb/eufemia/src/extensions/forms/Value/S

## Properties

<PropertiesTable props={SummaryListProperties} />
<PropertiesTable
props={SummaryListProperties}
omit={['label', 'labelSrOnly']}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import ValueProvider from '../Provider/ValueProvider'
import { ValueProps } from '../../types'
import { useVerifyChildren } from './useVerifyChildren'

export type Props = Omit<DlAllProps, 'label' | 'children'> & {
export type Props = Omit<
DlAllProps,
'label' | 'labelSrOnly' | 'children'
> & {
children: React.ReactNode
transformLabel?: ValueProps['transformLabel']
inheritVisibility?: ValueProps['inheritVisibility']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ describe('Field.SummaryList', () => {
expect(document.querySelector('dt')).toHaveClass('dnb-sr-only')
})

it('should set dnb-sr-only class when labelSrOnly is true', () => {
render(
<Value.SummaryList>
<Value.String label="Label" labelSrOnly showEmpty />
</Value.SummaryList>
)

const element = document.querySelector('dt')
expect(element).toHaveClass('dnb-sr-only')
expect(element).toHaveTextContent('Label')
})

it('should warn when child is not a Value.* component', () => {
const log = jest.spyOn(console, 'log').mockImplementation()

Expand Down
5 changes: 5 additions & 0 deletions packages/dnb-eufemia/src/extensions/forms/Value/ValueDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export const ValueProperties: PropertiesTableProps = {
type: 'string',
status: 'optional',
},
labelSrOnly: {
doc: 'Use `true` to make the label only readable by screen readers.',
type: 'boolean',
status: 'optional',
},
transformLabel: {
doc: 'Transforms the label before it gets displayed. Receives the label as the first parameter. The second parameter is a object containing the `convertJsxToString` function.',
type: 'function',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function ValueBlock(props: Props) {
const {
className,
label: labelProp,
labelSrOnly,
transformLabel = (label: Props['label']) => label,
inline,
maxWidth = props.composition ? props.maxWidth : 'large',
Expand Down Expand Up @@ -119,7 +120,7 @@ function ValueBlock(props: Props) {
<Dt
className={classnames(
'dnb-forms-value-block__label',
!label && 'dnb-sr-only'
(!label || labelSrOnly) && 'dnb-sr-only'
)}
>
{label && <strong>{label}</strong>}
Expand Down Expand Up @@ -163,6 +164,7 @@ function ValueBlock(props: Props) {
element="strong" // enhance a11y: https://www.w3.org/WAI/WCAG21/Techniques/html/H49
className="dnb-forms-value-block__label"
labelDirection={inline ? 'horizontal' : 'vertical'}
srOnly={labelSrOnly}
>
{label}
</FormLabel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ describe('ValueBlock', () => {
expect(element).toHaveClass('dnb-forms-value-block--max-width-medium')
})

it('should set dnb-sr-only class when labelSrOnly is true', () => {
render(
<ValueBlock label="Label" labelSrOnly>
content
</ValueBlock>
)

const element = document.querySelector('.dnb-form-label')
expect(element).toHaveClass('dnb-sr-only')
expect(element).toHaveTextContent('Label')
})

it('should put children in a wrapper element "__content"', () => {
render(<ValueBlock>content</ValueBlock>)

Expand Down
3 changes: 3 additions & 0 deletions packages/dnb-eufemia/src/extensions/forms/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ export interface ValueProps<Value = unknown>
*/
label?: React.ReactNode

/** Use `true` to make the label only readable by screen readers. */
labelSrOnly?: boolean

/**
* Use `true` to inherit the label from a visible (rendered) field with the same path.
*/
Expand Down

0 comments on commit 46f68ae

Please sign in to comment.