Skip to content

Commit

Permalink
Add disabled states to all input components
Browse files Browse the repository at this point in the history
  • Loading branch information
david0xd committed Jan 22, 2025
1 parent 687b582 commit 9a3d1a6
Show file tree
Hide file tree
Showing 19 changed files with 254 additions and 4 deletions.
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": "ssGPi4fxyZSvKOMXbVdOa/vnTx0qrq6WZWCUV91dLqo=",
"shasum": "OGd9oaCS1/I80grXQCFf5900ZUH2hpmnyklFTSpTh34=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/packages/browserify/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": "+342Ghzfo9UpTxJgqIPOieHvqRTnf4h00s8r0DpbozY=",
"shasum": "EG/2i55Y2f9IV97KMkFS1rgetcZsKrhbTlgoJ07M0zs=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
11 changes: 9 additions & 2 deletions packages/snaps-sdk/src/jsx/components/form/Checkbox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ describe('Checkbox', () => {
});
});

it('renders a checkbox with a variant and a label', () => {
it('renders a disabled checkbox with a variant and a label', () => {
const result = (
<Checkbox name="foo" label="Foo" checked={true} variant="toggle" />
<Checkbox
name="foo"
label="Foo"
checked={true}
variant="toggle"
disabled={true}
/>
);

expect(result).toStrictEqual({
Expand All @@ -26,6 +32,7 @@ describe('Checkbox', () => {
checked: true,
variant: 'toggle',
label: 'Foo',
disabled: true,
},
key: null,
});
Expand Down
3 changes: 3 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { createSnapComponent } from '../../component';
* @property checked - Whether the checkbox is checked or not.
* @property label - An optional label for the checkbox.
* @property variant - An optional variant for the checkbox.
* @property disabled - Whether the Checkbox is disabled.
*/
export type CheckboxProps = {
name: string;
checked?: boolean | undefined;
label?: string | undefined;
variant?: 'default' | 'toggle' | undefined;
disabled?: boolean | undefined;
};

const TYPE = 'Checkbox';
Expand All @@ -27,6 +29,7 @@ const TYPE = 'Checkbox';
* @param props.checked - Whether the checkbox is checked or not.
* @param props.label - An optional label for the checkbox.
* @param props.variant - An optional variant for the checkbox.
* @param props.disabled - Whether the Checkbox is disabled.
* @returns A checkbox element.
* @example
* <Checkbox name="accept-terms" />
Expand Down
3 changes: 3 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import type { OptionElement } from './Option';
* state in the form data.
* @property value - The selected value of the dropdown.
* @property children - The children of the dropdown.
* @property disabled - Whether the Dropdown is disabled.
*/
export type DropdownProps = {
name: string;
value?: string | undefined;
children: SnapsChildren<OptionElement>;
disabled?: boolean | undefined;
};

const TYPE = 'Dropdown';
Expand All @@ -26,6 +28,7 @@ const TYPE = 'Dropdown';
* state in the form data.
* @param props.value - The selected value of the dropdown.
* @param props.children - The children of the dropdown.
* @param props.disabled - Whether the Dropdown is disabled.
* @returns A dropdown element.
* @example
* <Dropdown name="dropdown">
Expand Down
13 changes: 13 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/FileInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ describe('FileInput', () => {
key: null,
});
});

it('renders disabled file input', () => {
const result = <FileInput name="foo" disabled={true} />;

expect(result).toStrictEqual({
type: 'FileInput',
props: {
name: 'foo',
disabled: true,
},
key: null,
});
});
});
3 changes: 3 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/FileInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import { createSnapComponent } from '../../component';
* specified, the file input field accepts all file types.
* @property compact - Whether the file input field is compact. Default is
* `false`.
* @property disabled - whether the file input is disabled.
*/
export type FileInputProps = {
name: string;
accept?: string[] | undefined;
compact?: boolean | undefined;
disabled?: boolean | undefined;
};

const TYPE = 'FileInput';
Expand All @@ -33,6 +35,7 @@ const TYPE = 'FileInput';
* valid values, see the [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/accept).
* @param props.compact - Whether the file input field is compact. Default is
* `false`.
* @param props.disabled - Whether the file input is disabled.
* @returns A file input element.
* @example
* <FileInput name="file" accept={['image/*']} />
Expand Down
54 changes: 54 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,58 @@ describe('Input', () => {
key: null,
});
});

it('renders a disabled text input', () => {
const result = <Input name="foo" type="text" disabled={true} />;

expect(result).toStrictEqual({
type: 'Input',
props: {
name: 'foo',
type: 'text',
disabled: true,
},
key: null,
});
});

it('renders a disabled number input', () => {
const result = (
<Input
name="foo"
type="number"
min={0}
max={10}
step={1}
disabled={true}
/>
);

expect(result).toStrictEqual({
type: 'Input',
props: {
name: 'foo',
type: 'number',
min: 0,
max: 10,
step: 1,
disabled: true,
},
key: null,
});
});

it('renders a disabled password input', () => {
const result = <Input name="foo" type="password" disabled={true} />;

expect(result).toStrictEqual({
type: 'Input',
props: {
name: 'foo',
type: 'password',
disabled: true,
},
key: null,
});
});
});
2 changes: 2 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type GenericInputProps = {
name: string;
value?: string | undefined;
placeholder?: string | undefined;
disabled?: boolean | undefined;
};

export type TextInputProps = { type: 'text' } & GenericInputProps;
Expand Down Expand Up @@ -57,6 +58,7 @@ const TYPE = 'Input';
* Only applicable to the type `number` input.
* @param props.step - The step value of the input field.
* Only applicable to the type `number` input.
* @param props.disabled - Whether the input is disabled.
* @returns An input element.
* @example
* <Input name="username" type="text" />
Expand Down
18 changes: 18 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Radio.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,22 @@ describe('Radio', () => {
key: null,
});
});

it('renders a disabled radio option', () => {
const result = (
<Radio value="foo" disabled={true}>
Foo
</Radio>
);

expect(result).toStrictEqual({
type: 'Radio',
props: {
value: 'foo',
disabled: true,
children: 'Foo',
},
key: null,
});
});
});
3 changes: 3 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { createSnapComponent } from '../../component';
* @property value - The value of the radio option. This is used to populate the
* state in the form data.
* @property children - The text to display.
* @property disabled - Whether the Radio is disabled.
*/
type RadioProps = {
value: string;
children: string;
disabled?: boolean | undefined;
};

const TYPE = 'Radio';
Expand All @@ -22,6 +24,7 @@ const TYPE = 'Radio';
* @param props.value - The value of the radio option. This is used to populate the
* state in the form data.
* @param props.children - The text to display.
* @param props.disabled - Whether the Radio is disabled.
* @returns A radio element.
* @example
* <RadioGroup name="radio-group">
Expand Down
36 changes: 36 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/RadioGroup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,40 @@ describe('RadioGroup', () => {
key: null,
});
});

it('renders a disabled Radio group', () => {
const result = (
<RadioGroup name="radio-choice" disabled={true}>
<Radio value="A">Option A</Radio>
<Radio value="B">Option B</Radio>
</RadioGroup>
);

expect(result).toStrictEqual({
type: 'RadioGroup',
props: {
name: 'radio-choice',
disabled: true,
children: [
{
type: 'Radio',
key: null,
props: {
value: 'A',
children: 'Option A',
},
},
{
type: 'Radio',
key: null,
props: {
value: 'B',
children: 'Option B',
},
},
],
},
key: null,
});
});
});
3 changes: 3 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/RadioGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ const TYPE = 'RadioGroup';
*
* @property name - The name of the dropdown. This is used to identify the
* state in the form data.
* @property value - The value of the radio group element.
* @property children - Radio options in form of <Radio> elements.
* @property disabled - Whether the RadioGroup is disabled.
*/
type RadioGroupProps = {
name: string;
value?: string | undefined;
children: SnapsChildren<RadioElement>;
disabled?: boolean | undefined;
};

/**
Expand Down
41 changes: 41 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Selector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,45 @@ describe('Selector', () => {
key: null,
});
});

it('renders a disabled selector', () => {
const result = (
<Selector
name="selector"
value="foo"
title="Choose an option"
disabled={true}
>
<SelectorOption value="foo">
<Card title="Foo" value="$1" />
</SelectorOption>
</Selector>
);

expect(result).toStrictEqual({
type: 'Selector',
props: {
name: 'selector',
value: 'foo',
title: 'Choose an option',
disabled: true,
children: {
type: 'SelectorOption',
props: {
value: 'foo',
children: {
type: 'Card',
props: {
title: 'Foo',
value: '$1',
},
key: null,
},
},
key: null,
},
},
key: null,
});
});
});
3 changes: 3 additions & 0 deletions packages/snaps-sdk/src/jsx/components/form/Selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import type { SelectorOptionElement } from './SelectorOption';
* @property title - The title of the selector. This is displayed in the UI.
* @property value - The selected value of the selector.
* @property children - The children of the selector.
* @property disabled - Whether the Selector is disabled.
*/
export type SelectorProps = {
name: string;
title: string;
value?: string | undefined;
children: SnapsChildren<SelectorOptionElement>;
disabled?: boolean | undefined;
};

const TYPE = 'Selector';
Expand All @@ -29,6 +31,7 @@ const TYPE = 'Selector';
* @param props.title - The title of the selector field. This is displayed in the UI.
* @param props.value - The selected value of the selector.
* @param props.children - The children of the selector.
* @property disabled - Whether the Selector is disabled.
* @returns A selector element.
* @example
* <Selector name="selector">
Expand Down
Loading

0 comments on commit 9a3d1a6

Please sign in to comment.