Skip to content

Commit 9c826e9

Browse files
authored
Allow Field in Box (#2926)
This PR allows the usage of `Field` in `Box` to be able to put two field in a row.
1 parent 6bd6fd8 commit 9c826e9

File tree

8 files changed

+44
-30
lines changed

8 files changed

+44
-30
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": "4yLB19XYAdGgHBPFlVOzCkb/JUZCjSajPRSQWs+a3uE=",
10+
"shasum": "d1js9Y4zJwk7n/e47V5fdMreo1FBtVKl4GtPhfckZrs=",
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": "+0hxp1uhfCqe9KR+4RPDSPGHFTgRyGULKLn9XWwCmsY=",
10+
"shasum": "VvMm+Zet1+q467UNweIuLLtYVoHoJdTeNjZn0xnEwO8=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-controllers/src/interface/utils.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,21 @@ describe('constructState', () => {
242242
});
243243
});
244244

245+
it('handles root level Field', () => {
246+
const element = (
247+
<Box>
248+
<Field label="foo">
249+
<Input name="foo" type="text" value="bar" />
250+
</Field>
251+
</Box>
252+
);
253+
254+
const result = constructState({}, element);
255+
expect(result).toStrictEqual({
256+
foo: 'bar',
257+
});
258+
});
259+
245260
it('handles root level inputs with value', () => {
246261
const element = (
247262
<Box>

packages/snaps-rpc-methods/src/permitted/createInterface.test.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Form,
1010
Container,
1111
Footer,
12+
Copyable,
1213
} from '@metamask/snaps-sdk/jsx';
1314
import type { JsonRpcRequest, PendingJsonRpcResponse } from '@metamask/utils';
1415

@@ -140,7 +141,7 @@ describe('snap_createInterface', () => {
140141
error: {
141142
code: -32602,
142143
message:
143-
'Invalid params: At path: ui -- Expected type to be one of: "Address", "Bold", "Box", "Button", "Copyable", "Divider", "Dropdown", "RadioGroup", "FileInput", "Form", "Heading", "Input", "Image", "Italic", "Link", "Row", "Spinner", "Text", "Tooltip", "Checkbox", "Card", "Icon", "Selector", "Section", "Avatar", "Container", but received: undefined.',
144+
'Invalid params: At path: ui -- Expected type to be one of: "Address", "Bold", "Box", "Button", "Copyable", "Divider", "Dropdown", "RadioGroup", "Field", "FileInput", "Form", "Heading", "Input", "Image", "Italic", "Link", "Row", "Spinner", "Text", "Tooltip", "Checkbox", "Card", "Icon", "Selector", "Section", "Avatar", "Container", but received: undefined.',
144145
stack: expect.any(String),
145146
},
146147
id: 1,
@@ -179,7 +180,7 @@ describe('snap_createInterface', () => {
179180
ui: (
180181
<Box>
181182
<Field label="Input">
182-
<Input name="input" />
183+
<Copyable value="foo" />
183184
</Field>
184185
</Box>
185186
) as JSXElement,
@@ -190,7 +191,7 @@ describe('snap_createInterface', () => {
190191
error: {
191192
code: -32602,
192193
message:
193-
'Invalid params: At path: ui.props.children -- Expected type to be one of: "Address", "Bold", "Box", "Button", "Copyable", "Divider", "Dropdown", "RadioGroup", "FileInput", "Form", "Heading", "Input", "Image", "Italic", "Link", "Row", "Spinner", "Text", "Tooltip", "Checkbox", "Card", "Icon", "Selector", "Section", "Avatar", but received: "Field".',
194+
'Invalid params: At path: ui.props.children.props.children -- Expected the value to satisfy a union of `tuple | tuple | tuple | object | object | object | object | object | object`, but received: [object Object].',
194195
stack: expect.any(String),
195196
},
196197
id: 1,

packages/snaps-sdk/src/jsx/components/form/Field.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export type FieldProps = {
3232
const TYPE = 'Field';
3333

3434
/**
35-
* A field component, which is used to create a form field. This component can
36-
* only be used as a child of the {@link Form} component.
35+
* A field component, which is used to create a form field.
3736
*
3837
* @param props - The props of the component.
3938
* @param props.label - The label of the field.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ describe('BoxStruct', () => {
567567
<Text>foo</Text>
568568
<Text>bar</Text>
569569
</Box>,
570+
<Box>
571+
<Field label="foo">
572+
<Input name="foo" />
573+
</Field>
574+
</Box>,
570575
<Box>
571576
<Text>foo</Text>
572577
<Row label="label">
@@ -620,11 +625,6 @@ describe('BoxStruct', () => {
620625
<Image src="<svg />" alt="alt" />
621626
</Row>
622627
</Box>,
623-
<Box>
624-
<Field label="foo">
625-
<Input name="foo" />
626-
</Field>
627-
</Box>,
628628
<Box>
629629
<Value extra="foo" value="bar" />
630630
</Box>,

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -507,22 +507,6 @@ export const FieldStruct: Describe<FieldElement> = element('Field', {
507507
children: FieldChildStruct,
508508
});
509509

510-
/**
511-
* A subset of JSX elements that are allowed as children of the Form component.
512-
*/
513-
export const FormChildStruct = children(
514-
// eslint-disable-next-line @typescript-eslint/no-use-before-define
515-
[FieldStruct, lazy(() => BoxChildStruct)],
516-
) as unknown as Struct<SnapsChildren<GenericSnapElement>, null>;
517-
518-
/**
519-
* A struct for the {@link FormElement} type.
520-
*/
521-
export const FormStruct: Describe<FormElement> = element('Form', {
522-
children: FormChildStruct,
523-
name: string(),
524-
});
525-
526510
/**
527511
* A struct for the {@link BoldElement} type.
528512
*/
@@ -584,6 +568,19 @@ export const BoxStruct: Describe<BoxElement> = element('Box', {
584568
center: optional(boolean()),
585569
});
586570

571+
/**
572+
* A subset of JSX elements that are allowed as children of the Form component.
573+
*/
574+
export const FormChildStruct = BoxChildrenStruct;
575+
576+
/**
577+
* A struct for the {@link FormElement} type.
578+
*/
579+
export const FormStruct: Describe<FormElement> = element('Form', {
580+
children: FormChildStruct,
581+
name: string(),
582+
});
583+
587584
const FooterButtonStruct = refine(ButtonStruct, 'FooterButton', (value) => {
588585
if (
589586
typeof value.props.children === 'string' ||
@@ -796,6 +793,7 @@ export const BoxChildStruct = typedUnion([
796793
DividerStruct,
797794
DropdownStruct,
798795
RadioGroupStruct,
796+
FieldStruct,
799797
FileInputStruct,
800798
FormStruct,
801799
HeadingStruct,

packages/snaps-simulator/src/features/builder/utils.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Copyable,
1010
Heading,
1111
Option,
12+
Container,
1213
} from '@metamask/snaps-sdk/jsx';
1314
import type { NodeModel } from '@minoru/react-dnd-treeview';
1415

@@ -30,7 +31,7 @@ describe('isValidBoxChild', () => {
3031
});
3132

3233
it('returns false for invalid box children', () => {
33-
const child = Field({ children: Input({ name: 'input' }) });
34+
const child = Container({ children: Input({ name: 'input' }) });
3435
expect(isValidBoxChild(child)).toBe(false);
3536
});
3637
});
@@ -80,7 +81,7 @@ describe('setElementChildren', () => {
8081
const element = Box({
8182
children: [Text({ children: 'foo' }), Heading({ children: 'baz' })],
8283
});
83-
const children = Field({ children: Input({ name: 'input' }) });
84+
const children = Container({ children: Input({ name: 'input' }) });
8485

8586
const result = setElementChildren(element, children, isValidBoxChild);
8687
expect(result).toStrictEqual(element.props.children);

0 commit comments

Comments
 (0)