Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Snap UI Radio component #26181

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { SnapUIInput } from '../snaps/snap-ui-input';
import { SnapUIForm } from '../snaps/snap-ui-form';
import { SnapUIButton } from '../snaps/snap-ui-button';
import { SnapUIDropdown } from '../snaps/snap-ui-dropdown';
import { SnapUIRadioGroup } from '../snaps/snap-ui-radio-group';
import { SnapUICheckbox } from '../snaps/snap-ui-checkbox';
import { SnapUITooltip } from '../snaps/snap-ui-tooltip';
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
Expand Down Expand Up @@ -87,6 +88,7 @@ export const safeComponentList = {
SnapUIButton,
SnapUIForm,
SnapUIDropdown,
SnapUIRadioGroup,
SnapUICheckbox,
SnapUITooltip,
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
Expand Down
1 change: 1 addition & 0 deletions ui/components/app/snaps/snap-ui-radio-group/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './snap-ui-radio-group';
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import React, { FunctionComponent, useEffect, useState } from 'react';
import { useSnapInterfaceContext } from '../../../../contexts/snaps';
import {
AlignItems,
Display,
FlexDirection,
TextVariant,
} from '../../../../helpers/constants/design-system';
import {
Box,
HelpText,
HelpTextSeverity,
Label,
Text,
} from '../../../component-library';

export type SnapUIRadioOption = { value: string; name: string };

export type SnapUIRadioGroupProps = {
name: string;
label?: string;
error?: string;
options: SnapUIRadioOption[];
form?: string;
};

export const SnapUIRadioGroup: FunctionComponent<SnapUIRadioGroupProps> = ({
name,
label,
error,
form,
...props
}) => {
const { handleInputChange, getValue } = useSnapInterfaceContext();

const initialValue = getValue(name, form) as string;

const [value, setValue] = useState(initialValue ?? '');

useEffect(() => {
if (initialValue && value !== initialValue) {
setValue(initialValue);
}
}, [initialValue]);

const handleChange = (newValue: string) => {
setValue(newValue);
handleInputChange(name, newValue, form);
};

const displayRadioOptions = (options: SnapUIRadioOption[]) => {
return options.map((option: SnapUIRadioOption) => {
return (
<Box display={Display.Flex} alignItems={AlignItems.center}>
<input
type="radio"
id={option.name}
name={name}
value={option.value}
checked={value === option.value}
onChange={() => handleChange(option.value)}
/>
<Text
as="label"
htmlFor={option.name}
variant={TextVariant.bodyMd}
marginLeft={1}
>
{option.name}
</Text>
</Box>
);
});
};

return (
<Box
className="snap-ui-renderer__radio"
display={Display.Flex}
flexDirection={FlexDirection.Column}
>
{label && <Label htmlFor={name}>{label}</Label>}
{displayRadioOptions(props.options)}
{error && (
<HelpText severity={HelpTextSeverity.Danger} marginTop={1}>
{error}
</HelpText>
)}
</Box>
);
};
20 changes: 20 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
ButtonElement,
JSXElement,
DropdownElement,
RadioGroupElement,
CheckboxElement,
} from '@metamask/snaps-sdk/jsx';
import { getJsxChildren } from '@metamask/snaps-utils';
import { button as buttonFn } from './button';
import { dropdown as dropdownFn } from './dropdown';
import { radioGroup as radioGroupFn } from './radioGroup';
import { checkbox as checkboxFn } from './checkbox';
import { UIComponentFactory, UIComponentParams } from './types';

Expand Down Expand Up @@ -84,6 +86,24 @@ export const field: UIComponentFactory<FieldElement> = ({ element, form }) => {
};
}

case 'RadioGroup': {
const radioGroup = child as RadioGroupElement;
const radioGroupMapped = radioGroupFn({
element: radioGroup,
} as UIComponentParams<RadioGroupElement>);
return {
element: 'SnapUIRadioGroup',
props: {
...radioGroupMapped.props,
id: radioGroup.props.name,
label: element.props.label,
name: radioGroup.props.name,
form,
error: element.props.error,
},
};
}

case 'Checkbox': {
const checkbox = child as CheckboxElement;
const checkboxMapped = checkboxFn({
Expand Down
2 changes: 2 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { italic } from './italic';
import { link } from './link';
import { field } from './field';
import { dropdown } from './dropdown';
import { radioGroup } from './radioGroup';
import { value } from './value';
import { checkbox } from './checkbox';
import { tooltip } from './tooltip';
Expand All @@ -40,6 +41,7 @@ export const COMPONENT_MAPPING = {
Link: link,
Field: field,
Dropdown: dropdown,
RadioGroup: radioGroup,
Value: value,
Checkbox: checkbox,
Tooltip: tooltip,
Expand Down
26 changes: 26 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/radioGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { RadioGroupElement, RadioElement } from '@metamask/snaps-sdk/jsx';
import { getJsxChildren } from '@metamask/snaps-utils';

import { UIComponentFactory } from './types';

export const radioGroup: UIComponentFactory<RadioGroupElement> = ({
element,
form,
}) => {
const children = getJsxChildren(element) as RadioElement[];

const options = children.map((child) => ({
value: child.props.value,
name: child.props.children,
}));

return {
element: 'SnapUIRadioGroup',
props: {
id: element.props.name,
name: element.props.name,
form,
options,
},
};
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { JSXElementStruct } from '@metamask/snaps-sdk/jsx';
import { COMPONENT_MAPPING } from './components';

const EXCLUDED_COMPONENTS = ['Option'];
const EXCLUDED_COMPONENTS = ['Option', 'Radio'];

describe('Snap UI mapping', () => {
it('supports all exposed components', () => {
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/snaps/snap-ui-renderer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const mapToTemplate = (params: MapToTemplateParams): UIComponent => {
const { type, key } = params.element;
const elementKey = key ?? generateKey(params.map, params.element);
const mapped = COMPONENT_MAPPING[
type as Exclude<JSXElement['type'], 'Option'>
type as Exclude<JSXElement['type'], 'Option' | 'Radio'>
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
](params as any);
Expand Down
Loading