Skip to content

feat: KeyValuePairs update pair label to ReactNode #3430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions pages/key-value-pairs/simple.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
import React from 'react';

import Icon from '~components/icon';
import KeyValuePairs from '~components/key-value-pairs';
import Link from '~components/link';
import SpaceBetween from '~components/space-between';

import ScreenshotArea from '../utils/screenshot-area';

Expand All @@ -27,6 +30,34 @@ export default function () {
label: 'Label for key',
value: 'Value',
},
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could take these three to the pemutations file and keep a simple one (the first one for example) in this file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved them over

label: (
<SpaceBetween size={'xxs'} direction={'horizontal'} alignItems={'center'}>
<Icon key={'icon'} name={'status-info'} />
<div key={'label'}>Label for key</div>
</SpaceBetween>
),
value: 'Info icon at the start',
},
{
label: (
<SpaceBetween size={'xxs'} direction={'horizontal'} alignItems={'center'}>
<div key={'label'}>Label for key</div>
<Icon key={'icon'} name={'external'} />
</SpaceBetween>
),
value: 'External icon at the end',
},
{
label: (
<SpaceBetween size={'xxs'} direction={'horizontal'} alignItems={'center'}>
<div key={'label'}>Label for key</div>
<Icon key={'icon'} name={'external'} />
</SpaceBetween>
),
value: 'External icon at the end with info link',
info: <Link>Info</Link>,
},
],
},
]}
Expand Down
41 changes: 40 additions & 1 deletion src/key-value-pairs/__tests__/key-value-pairs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import * as React from 'react';
import { render } from '@testing-library/react';

import Icon from '../../../lib/components/icon';
import KeyValuePairs from '../../../lib/components/key-value-pairs';
import Link from '../../../lib/components/link';
import createWrapper from '../../../lib/components/test-utils/dom';
import SpaceBetween from '../../../lib/components/space-between';
import createWrapper, { IconWrapper } from '../../../lib/components/test-utils/dom';

function renderKeyValuePairs(jsx: React.ReactElement) {
const { container, ...rest } = render(jsx);
Expand All @@ -30,6 +32,43 @@ describe('KeyValuePairs', () => {
expect(wrapper.findItems()[0]!.findValue()!.getElement()).toHaveTextContent('Value');
});

test('renders correctly with ReactNode label', () => {
const { wrapper } = renderKeyValuePairs(
<KeyValuePairs
items={[
{
label: (
<SpaceBetween size={'xxs'} direction={'horizontal'} alignItems={'center'}>
<Icon name={'status-info'} />
Label after icon
</SpaceBetween>
),
value: 'Value',
},
{
label: (
<SpaceBetween size={'xxs'} direction={'horizontal'} alignItems={'center'}>
Label before icon
<Icon name={'external'} />
</SpaceBetween>
),
value: 'Value',
},
]}
/>
);

const firstLabel = wrapper.findItems()[0]!.findLabel();
expect(firstLabel!.getElement().firstElementChild!.children).toHaveLength(2);
expect(firstLabel!.findIcon()).toBeInstanceOf(IconWrapper);
expect(firstLabel!.getElement()).toHaveTextContent('Label after icon');

const secondLabel = wrapper.findItems()[1]!.findLabel();
expect(secondLabel!.getElement().firstElementChild!.children).toHaveLength(2);
expect(secondLabel!.findIcon()).toBeInstanceOf(IconWrapper);
expect(secondLabel!.getElement()).toHaveTextContent('Label before icon');
});

test('renders label with info correctly', () => {
const { wrapper } = renderKeyValuePairs(
<KeyValuePairs
Expand Down
2 changes: 1 addition & 1 deletion src/key-value-pairs/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export namespace KeyValuePairsProps {

export interface Pair extends BaseComponentProps {
type?: 'pair';
label: string;
label: React.ReactNode;
value: React.ReactNode;
info?: React.ReactNode;
}
Expand Down
1 change: 1 addition & 0 deletions src/key-value-pairs/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

.term {
@include styles.font-label;
display: inline-flex;
color: awsui.$color-text-label;
margin-block-end: awsui.$space-key-value-gap;
}
Expand Down
Loading