-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support conditional children in most JSX components (#2506)
This adds support for conditional children in most JSX components. The exceptions are `Field`, `Row`, and other components which don't have children in the first place. For example, the following is now possible: ```tsx <Box direction="horizontal" alignment="space-between"> {condition && <Text>Hello</Text>} </Box> ``` To support this, I've added `boolean` as valid prop for the JSX components. The `getJsxChildren` function filters out any falsy or literal `true` values. I've also added a small contributing guide for conventions around JSX components, including the support for conditional children. Closes #2505. --------- Co-authored-by: MetaMask Bot <[email protected]>
- Loading branch information
1 parent
c281824
commit d738f7c
Showing
56 changed files
with
543 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Contributing | ||
|
||
## Adding new JSX components | ||
|
||
The MetaMask Snaps SDK uses JSX components for building user interfaces in | ||
Snaps. To add a new component, follow these steps: | ||
|
||
1. Create a new file in the `src/jsx/components` directory, optionally in a | ||
subdirectory if the component is part of a group of related components. | ||
2. It is recommended to copy an existing component file and modify it to create | ||
the new component. | ||
3. Create a test for the new component, following the existing test files for | ||
the other components. | ||
4. Add the new component to the `src/jsx/components/index.ts` file, exporting it | ||
from the file. | ||
- This file also contains a `JSXElement` union type that should | ||
be updated to include the new component. | ||
5. Add validation for the component to `src/jsx/validation.ts`, making sure to | ||
use the `Describe` helper type to ensure that the component is correctly | ||
validated. | ||
- Make sure to add tests for the validation in `src/jsx/validation.test.ts` | ||
as well. | ||
6. If the component is stateful, make sure to add it to the | ||
`SnapInterfaceController` too. | ||
- You can use [this PR](https://github.com/MetaMask/snaps/pull/2501) as a | ||
reference. | ||
|
||
### Component props | ||
|
||
When adding a new component, make sure to document the props that the component | ||
accepts. This can be done by adding a JSDoc comment to the component function | ||
declaration, like so: | ||
|
||
```typescript | ||
/** | ||
* The props of the {@link Dropdown} component. | ||
* | ||
* @property name - The name of the dropdown. This is used to identify the | ||
* state in the form data. | ||
* @property value - The selected value of the dropdown. | ||
* @property children - The children of the dropdown. | ||
*/ | ||
export type DropdownProps = { | ||
name: string; | ||
value?: string | undefined; | ||
children: SnapsChildren<OptionElement>; | ||
}; | ||
``` | ||
|
||
The props type should be exported from the component file and used in the | ||
component function declaration, like so: | ||
|
||
```typescript | ||
// ... | ||
export const Dropdown = createSnapComponent<DropdownProps, typeof TYPE>(TYPE); | ||
``` | ||
|
||
To ensure consistency, make sure to follow the existing patterns in the codebase | ||
when documenting component props. | ||
|
||
#### Optional props | ||
|
||
Optional props should be both marked with a `?` in the type definition, and | ||
also include `undefined` in the type definition. This is to ensure that the | ||
component can be used with or without TypeScript's exact optional props feature. | ||
|
||
```typescript | ||
export type ComponentProps = { | ||
value?: string | undefined; | ||
}; | ||
``` | ||
|
||
#### Children | ||
|
||
In most cases, the children prop should be defined as `SnapsChildren` to allow | ||
for both a single child element or an array of child elements. There are some | ||
exceptions to this rule, such as when the component only accepts a single child | ||
element (e.g., `Field`). | ||
|
||
Children should also accept `boolean` and `null` values to allow for conditional | ||
rendering of child elements. This is handled automatically by the | ||
`SnapsChildren` type. | ||
|
||
```typescript | ||
export type ComponentProps = { | ||
children: SnapsChildren<string>; // Nestable<string | boolean | null>; | ||
}; | ||
``` | ||
|
||
If the children are optional, make sure to include `undefined` in the type and | ||
add the `?` to the prop definition. | ||
|
||
```typescript | ||
export type ComponentProps = { | ||
children?: SnapsChildren<string> | undefined; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.