Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/form-component/src/app-components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React from 'react';
import type { PropsWithChildren, Ref } from 'react';

import { Button as DesignSystemButton } from '@digdir/designsystemet-react';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.icon {
width: 24px;
margin-right: 12px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { Meta, StoryObj } from '@storybook/react-vite';

import { DisplayText } from './DisplayText';

const meta = {
title: 'AppComponents/DisplayText',
component: DisplayText,
args: {
value: 'Hello world',
},
} satisfies Meta<typeof DisplayText>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Preview: Story = {};

export const WithIcon: Story = {
args: {
iconUrl: 'https://altinncdn.no/orgs/digdir/digdir.png',
iconAltText: 'Info icon',
},
};

export const WithLabel: Story = {
args: {
labelId: 'text-label',
},
render: (args) => (
<>
<span id='text-label'>Description</span>
<DisplayText {...args} />
</>
),
};

export const EmptyValue: Story = {
args: {
value: '',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { render, screen } from '@testing-library/react';

import { DisplayText } from './DisplayText';

describe('DisplayText', () => {
it('renders the value inside a span', () => {
render(<DisplayText value='Hello world' />);

expect(screen.getByText('Hello world')).toBeInTheDocument();
});

it('renders an icon with alt text when iconUrl is provided', () => {
render(<DisplayText value='Hello world' iconUrl='/icon.svg' iconAltText='Info icon' />);

const icon = screen.getByRole('img', { name: 'Info icon' });
expect(icon).toHaveAttribute('src', '/icon.svg');
});

it('omits the icon when iconUrl is not provided', () => {
render(<DisplayText value='Hello world' iconAltText='unused' />);

expect(screen.queryByRole('img')).not.toBeInTheDocument();
});

it('associates the value span with labelId via aria-labelledby', () => {
render(<DisplayText value='Hello world' labelId='text-label' />);

expect(screen.getByText('Hello world')).toHaveAttribute('aria-labelledby', 'text-label');
});
});
17 changes: 17 additions & 0 deletions libs/form-component/src/app-components/DisplayText/DisplayText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import classes from './DisplayText.module.css';

export type DisplayTextProps = {
value: string;
iconUrl?: string;
iconAltText?: string;
labelId?: string;
};

export function DisplayText({ value, iconUrl, iconAltText, labelId }: DisplayTextProps) {
return (
<>
{iconUrl && <img src={iconUrl} className={classes.icon} alt={iconAltText} />}
<span aria-labelledby={labelId}>{value}</span>
</>
);
}
2 changes: 2 additions & 0 deletions libs/form-component/src/app-components/DisplayText/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { DisplayText } from './DisplayText';
export type { DisplayTextProps } from './DisplayText';
1 change: 0 additions & 1 deletion libs/form-component/src/app-components/Input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export { Input } from './Input';
export type { InputProps } from './Input';
export { FormattedInput } from './FormattedInput';
export { NumericInput } from './NumericInput';

1 change: 1 addition & 0 deletions libs/form-component/src/app-components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './Flex';
export * from './Input';
export * from './hooks';
export * from './DisplayDate';
export * from './DisplayText';
export * from './DisplayNumber';
export * from './Spinner';
export * from './TextArea';
29 changes: 0 additions & 29 deletions src/App/frontend/src/app-components/Text/DisplayText.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
.label span {
font-size: var(--ds-body-md-font-size);
}

.icon {
width: 24px;
margin-right: 12px;
}
.vertical {
flex-direction: column;
}
Expand Down
9 changes: 5 additions & 4 deletions src/App/frontend/src/layout/Text/TextComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React from 'react';

import { DisplayText } from '@app/form-component';
import cn from 'classnames';

import { DisplayText } from 'src/app-components/Text/DisplayText';
import classes from 'src/app-components/Text/Text.module.css';
import { translationKey } from 'src/AppComponentsBridge';
import { getLabelId } from 'src/components/label/Label';
import { useLanguage } from 'src/features/language/useLanguage';
import { ComponentStructureWrapper } from 'src/layout/ComponentStructureWrapper';
import classes from 'src/layout/Text/Text.module.css';
import { useItemWhenType } from 'src/utils/layout/useNodeItem';
import type { PropsFromGenericComponent } from 'src/layout';

export const TextComponent = ({ baseComponentId }: PropsFromGenericComponent<'Text'>) => {
const { id, textResourceBindings, value, icon, direction: _direction } = useItemWhenType(baseComponentId, 'Text');
const direction = _direction ?? 'horizontal';
const { langAsString } = useLanguage();

if (!textResourceBindings?.title) {
return <DisplayText value={value} />;
Expand All @@ -34,7 +35,7 @@ export const TextComponent = ({ baseComponentId }: PropsFromGenericComponent<'Te
<DisplayText
value={value}
iconUrl={icon}
iconAltText={translationKey(textResourceBindings.title)}
iconAltText={langAsString(textResourceBindings.title)}
labelId={getLabelId(id)}
/>
</ComponentStructureWrapper>
Expand Down
Loading