Skip to content

Commit

Permalink
Update preinstalled example to make buttons actually work
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Oct 11, 2024
1 parent 1251f80 commit 6a31d26
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/examples/packages/preinstalled/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snaps.git"
},
"source": {
"shasum": "NzLP9BfbhyGMuOG5TgjxLkvOUQUKJI58t+dTOoY+ylE=",
"shasum": "7rntsnBzcTZzFRHWNhO+yXQ8KTK2uR49xsl+DVmyOnQ=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
14 changes: 11 additions & 3 deletions packages/examples/packages/preinstalled/src/components/dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { SnapComponent } from '@metamask/snaps-sdk/jsx';
import {
Field,
Form,
Box,
Container,
Heading,
Expand All @@ -19,14 +21,20 @@ export const Dialog: SnapComponent = () => (
<Box>
<Heading>Custom Dialog</Heading>
<Text>
This is a custom dialog. It has a custom Footer and can be resolved to
This is a custom dialog. It has a custom footer and can be resolved to
any value.
</Text>
<Input name="custom-input" placeholder="Enter something..." />
<Form name="form">
<Field label="Field">
<Input name="custom-input" placeholder="Enter something..." />
</Field>
</Form>
</Box>
<Footer>
<Button name="cancel">Cancel</Button>
<Button name="confirm">Confirm</Button>
<Button name="confirm" form="form">
Confirm
</Button>
</Footer>
</Container>
);
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './dialog';
export * from './result';
34 changes: 34 additions & 0 deletions packages/examples/packages/preinstalled/src/components/result.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { SnapComponent } from '@metamask/snaps-sdk/jsx';
import {
Copyable,
Box,
Container,
Heading,
Text,
Footer,
Button,
} from '@metamask/snaps-sdk/jsx';

export type ResultProps = {
value: string;
};

/**
* A result component.
*
* @param props - The props of the component.
* @param props.value - The value to display.
* @returns The result component.
*/
export const Result: SnapComponent<ResultProps> = ({ value }) => (
<Container>
<Box>
<Heading>Custom Dialog</Heading>
<Text>The form was submitted with the following value:</Text>
<Copyable value={value} />
</Box>
<Footer>
<Button name="ok">Ok</Button>
</Footer>
</Container>
);
49 changes: 47 additions & 2 deletions packages/examples/packages/preinstalled/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { MethodNotFoundError } from '@metamask/snaps-sdk';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import type {
OnRpcRequestHandler,
OnUserInputHandler,
} from '@metamask/snaps-sdk';

import { Dialog } from './components';
import { Dialog, Result } from './components';

/**
* Handle incoming JSON-RPC requests from the dapp, sent through the
Expand Down Expand Up @@ -31,3 +34,45 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => {
throw new MethodNotFoundError({ method: request.method });
}
};

export const onUserInput: OnUserInputHandler = async ({
event,
id,
context,
}) => {
if (event.type === 'ButtonClickEvent') {
if (event.name === 'cancel') {
await snap.request({
method: 'snap_resolveInterface',
params: {
id,
value: null,
},
});
}

if (event.name === 'ok') {
await snap.request({
method: 'snap_resolveInterface',
params: {
id,
value: String(context?.value),
},
});
}
}

if (event.type === 'FormSubmitEvent') {
if (event.name === 'form') {
const value = String(event.value['custom-input']);
await snap.request({
method: 'snap_updateInterface',
params: {
id,
ui: <Result value={value} />,
context: { value },
},
});
}
}
};

0 comments on commit 6a31d26

Please sign in to comment.