Skip to content
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

feat: Allow ReactElement in LabeledValue value #7679

Merged
merged 20 commits into from
Feb 3, 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
13 changes: 13 additions & 0 deletions packages/@react-spectrum/labeledvalue/src/LabeledValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ export const LabeledValue = React.forwardRef(function LabeledValue<T extends Spe
} = props;
let domRef = useDOMRef(ref);

// todo(sanmalik) - fix this
snowystinger marked this conversation as resolved.
Show resolved Hide resolved
if (
domRef?.current &&
domRef.current.querySelectorAll("input, [contenteditable], textarea")
.length > 0
) {
throw new Error("LabeledValue cannot contain an editable element.");
}
reidbarber marked this conversation as resolved.
Show resolved Hide resolved

let children;
if (Array.isArray(value)) {
children = <FormattedStringList value={value} formatOptions={formatOptions as Intl.ListFormatOptions} />;
Expand All @@ -103,6 +112,10 @@ export const LabeledValue = React.forwardRef(function LabeledValue<T extends Spe
children = value;
}

if (React.isValidElement(value)) {
children = value;
}

return (
<Field {...props as any} wrapperProps={filterDOMProps(props as any)} ref={domRef} elementType="span" wrapperClassName={classNames(labelStyles, 'spectrum-LabeledValue')}>
<span>{children}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {ContextualHelp} from '@react-spectrum/contextualhelp';
import {Heading} from '@react-spectrum/text';
import {LabeledValue} from '..';
import React from 'react';
import { Link } from "@react-spectrum/link";

type LabeledValueStory = ComponentStoryObj<typeof LabeledValue>;

Expand Down Expand Up @@ -120,6 +121,15 @@ export let NumberRange: LabeledValueStory = {
name: 'RangeValue<Number>'
};


export let CustomComponent: LabeledValueStory = {
args: {
label: "Test",
value: <Link href="https://www.adobe.com">Adobe</Link>,
},
name: "CustomComponent",
};

export let WithContextualHelp: LabeledValueStory = {
args: {
label: 'Test',
Expand Down
30 changes: 30 additions & 0 deletions packages/@react-spectrum/labeledvalue/test/LabeledValue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,36 @@ describe('LabeledValue', function () {
expect(staticField).toHaveTextContent('10 – 20');
});

it("renders correctly with ReactNode value", function () {
let { getByTestId } = render(
<LabeledValue
data-testid="test-id"
label="Field label"
value={<a href="https://test.com">test</a>}
/>
);

let staticField = getByTestId("test-id");
expect(staticField).toBeInTheDocument();
expect(staticField).toHaveTextContent("test");
expect(
within(staticField).getByRole("link", { name: "test" })
).toBeInTheDocument();
});

it("throws when an editable value is provided", function () {
expect(() => {
render(
<LabeledValue
data-testid="test-id"
label={<input />}
value="test"
isEditable
/>
);
}).toThrow("LabeledValue cannot contain an editable element.");
});

it('attaches a user provided ref to the outer div', function () {
let ref = React.createRef();
let {getByTestId} = render(
Expand Down