Skip to content

Commit

Permalink
fix: Allow any element as the child of Container
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Sep 4, 2024
1 parent f5d32c7 commit 82018a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/snaps-sdk/src/jsx/components/Container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GenericSnapElement } from '../component';
import { createSnapComponent } from '../component';
import type { BoxElement } from './Box';
import type { FooterElement } from './Footer';

/**
Expand All @@ -8,7 +8,7 @@ import type { FooterElement } from './Footer';
* @property children - The Box and the Footer or the Box element.
*/
export type ContainerProps = {
children: [BoxElement, FooterElement] | BoxElement;
children: [GenericSnapElement, FooterElement] | GenericSnapElement;
};

const TYPE = 'Container';
Expand Down
9 changes: 9 additions & 0 deletions packages/snaps-sdk/src/jsx/validation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,15 @@ describe('ContainerStruct', () => {
<Button name="confirm">Confirm</Button>
</Footer>
</Container>,
<Container>
<Text>Hello world!</Text>
</Container>,
<Container>
<Text>Hello world!</Text>
<Footer>
<Button name="confirm">Confirm</Button>
</Footer>
</Container>,
])('validates a container element', (value) => {
expect(is(value, ContainerStruct)).toBe(true);
});
Expand Down
35 changes: 16 additions & 19 deletions packages/snaps-sdk/src/jsx/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,25 +471,6 @@ export const FooterStruct: Describe<FooterElement> = element('Footer', {
children: FooterChildStruct,
});

/**
* A subset of JSX elements that are allowed as children of the Container component.
* This set should include a single Box or a tuple of a Box and a Footer component.
*/
export const ContainerChildStruct = nullUnion([
tuple([BoxStruct, FooterStruct]),
BoxStruct,
]);

/**
* A struct for the {@link ContainerElement} type.
*/
export const ContainerStruct: Describe<ContainerElement> = element(
'Container',
{
children: ContainerChildStruct,
},
);

/**
* A struct for the {@link CopyableElement} type.
*/
Expand Down Expand Up @@ -636,6 +617,22 @@ export const BoxChildStruct = typedUnion([
SectionStruct,
]);

/**
* A struct for the {@link ContainerElement} type.
*/
export const ContainerStruct: Describe<ContainerElement> = element(
'Container',
{
children: nullUnion([
tuple([BoxChildStruct, FooterStruct]),
BoxChildStruct,
]) as unknown as Struct<
[GenericSnapElement, FooterElement] | GenericSnapElement,
null
>,
},
);

/**
* For now, the allowed JSX elements at the root are the same as the allowed
* children of the Box component.
Expand Down

0 comments on commit 82018a3

Please sign in to comment.