Skip to content

Commit 6fe7296

Browse files
GuillaumeRxPatrykLucka
authored andcommitted
feat: Allow usage of Text in Value props (#2984)
This allows the usage of `Text` in the `Value` component props `value` and `extra`.
1 parent 2753e13 commit 6fe7296

File tree

6 files changed

+73
-18
lines changed

6 files changed

+73
-18
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "hy0TMeQeqznNQRX2j7DnxRt1Nn5Z+v0rjaWNpe1fEWE=",
10+
"shasum": "nWxYWnUSrrm7uZeqQoIaP3l1hbk2ZWRKGULlxodyuhw=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "VR3Zwjo0yqKLkuKHGDfS9AmuyW3KMbXSmi9Nh9JgCMw=",
10+
"shasum": "WnX2s+XAfT18c6WH1hAniRAIgDEe7VyAieuRXFEDIEY=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-sdk/src/jsx/components/Value.test.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Text } from './Text';
12
import { Value } from './Value';
23

34
describe('Value', () => {
@@ -13,4 +14,36 @@ describe('Value', () => {
1314
},
1415
});
1516
});
17+
18+
it('renders with text elements', () => {
19+
const result = (
20+
<Value
21+
value={<Text color="error">0.05 ETH</Text>}
22+
extra={<Text color="error">$200</Text>}
23+
/>
24+
);
25+
26+
expect(result).toStrictEqual({
27+
type: 'Value',
28+
key: null,
29+
props: {
30+
extra: {
31+
type: 'Text',
32+
key: null,
33+
props: {
34+
children: '$200',
35+
color: 'error',
36+
},
37+
},
38+
value: {
39+
type: 'Text',
40+
key: null,
41+
props: {
42+
children: '0.05 ETH',
43+
color: 'error',
44+
},
45+
},
46+
},
47+
});
48+
});
1649
});

packages/snaps-sdk/src/jsx/components/Value.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createSnapComponent } from '../component';
2+
import type { TextElement } from './Text';
23

34
/**
45
* The props of the {@link Value} component.
@@ -7,8 +8,8 @@ import { createSnapComponent } from '../component';
78
* @property extra - The extra text shown on the left side.
89
*/
910
export type ValueProps = {
10-
value: string;
11-
extra: string;
11+
value: TextElement | string;
12+
extra: TextElement | string;
1213
};
1314

1415
const TYPE = 'Value';
@@ -26,6 +27,8 @@ const TYPE = 'Value';
2627
* @returns A value element.
2728
* @example
2829
* <Value value="0.05 ETH" extra="$200" />
30+
* @example
31+
* <Value value={<Text color='error'>0.05 ETH</Text>} extra={<Text color='error'>$200</Text>} />
2932
*/
3033
export const Value = createSnapComponent<ValueProps, typeof TYPE>(TYPE);
3134

packages/snaps-sdk/src/jsx/validation.test.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,12 +1325,15 @@ describe('SpinnerStruct', () => {
13251325
});
13261326

13271327
describe('ValueStruct', () => {
1328-
it.each([<Value extra="foo" value="bar" />])(
1329-
'validates a value element',
1330-
(value) => {
1331-
expect(is(value, ValueStruct)).toBe(true);
1332-
},
1333-
);
1328+
it.each([
1329+
<Value extra="foo" value="bar" />,
1330+
<Value
1331+
value={<Text color="error">0.05 ETH</Text>}
1332+
extra={<Text color="error">$200</Text>}
1333+
/>,
1334+
])('validates a value element', (value) => {
1335+
expect(is(value, ValueStruct)).toBe(true);
1336+
});
13341337

13351338
it.each([
13361339
'foo',
@@ -1343,6 +1346,10 @@ describe('ValueStruct', () => {
13431346
<Value />,
13441347
// @ts-expect-error - Invalid props.
13451348
<Value left="foo" />,
1349+
<Value
1350+
value={<Heading>0.05 ETH</Heading>}
1351+
extra={<Heading>$200</Heading>}
1352+
/>,
13461353
<Text>foo</Text>,
13471354
<Box>
13481355
<Text>foo</Text>

packages/snaps-sdk/src/jsx/validation.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -667,14 +667,6 @@ export const CopyableStruct: Describe<CopyableElement> = element('Copyable', {
667667
*/
668668
export const DividerStruct: Describe<DividerElement> = element('Divider');
669669

670-
/**
671-
* A struct for the {@link ValueElement} type.
672-
*/
673-
export const ValueStruct: Describe<ValueElement> = element('Value', {
674-
value: string(),
675-
extra: string(),
676-
});
677-
678670
/**
679671
* A struct for the {@link HeadingElement} type.
680672
*/
@@ -728,6 +720,26 @@ export const TextStruct: Describe<TextElement> = element('Text', {
728720
),
729721
});
730722

723+
/**
724+
* A struct for the {@link ValueElement} type.
725+
*/
726+
export const ValueStruct: Describe<ValueElement> = element('Value', {
727+
value: selectiveUnion((value) => {
728+
if (typeof value === 'string') {
729+
return string();
730+
}
731+
732+
return TextStruct;
733+
}),
734+
extra: selectiveUnion((value) => {
735+
if (typeof value === 'string') {
736+
return string();
737+
}
738+
739+
return TextStruct;
740+
}),
741+
});
742+
731743
/**
732744
* A subset of JSX elements that are allowed as children of the Tooltip component.
733745
* This set should include all text components and the Image.

0 commit comments

Comments
 (0)