Skip to content

Commit dbc2d21

Browse files
committed
chore(docs): Added Menu With Form Controls Demo
1 parent fed2b9f commit dbc2d21

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Starting with `react-md@2.8.0`, you can render the `Checkbox`, `Radio`, and
2+
`Switch` components within menus while maintaining accessibility using the
3+
`MenuItemCheckbox`, `MenuItemRadio`, and `MenuItemSwitch` components. These
4+
components are fully controlled and require an `id`, `checked`, and
5+
`onCheckedChange` props to work correctly.
6+
7+
Check out the example below to see these new components in action.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* eslint-disable react/jsx-key */
2+
import { DropdownMenu, MenuItem, MenuItemSeparator } from "@react-md/menu";
3+
import {
4+
MenuItemCheckbox,
5+
MenuItemRadio,
6+
MenuItemSwitch,
7+
} from "@react-md/form";
8+
import React, { ReactElement, useState } from "react";
9+
10+
type Decoration = "none" | "underline" | "overline" | "strike-through";
11+
12+
const decorations: readonly Decoration[] = [
13+
"none",
14+
"underline",
15+
"overline",
16+
"strike-through",
17+
];
18+
19+
export default function MenusWithFormControls(): ReactElement | null {
20+
const [bold, setBold] = useState(false);
21+
const [italic, setItalic] = useState(false);
22+
const [decoration, setDecoration] = useState<Decoration>("none");
23+
const [checked, setChecked] = useState(false);
24+
25+
return (
26+
<DropdownMenu
27+
id="some-id"
28+
items={[
29+
<MenuItem id="item-1">Item 1</MenuItem>,
30+
<MenuItemCheckbox
31+
id="font-bold"
32+
checked={bold}
33+
onCheckedChange={(checked) => setBold(checked)}
34+
>
35+
Bold
36+
</MenuItemCheckbox>,
37+
<MenuItemCheckbox
38+
id="font-italic"
39+
checked={italic}
40+
onCheckedChange={(checked) => setItalic(checked)}
41+
>
42+
Italic
43+
</MenuItemCheckbox>,
44+
<MenuItemSeparator />,
45+
...decorations.map((dec) => (
46+
<MenuItemRadio
47+
id={`decoration-${dec}`}
48+
checked={decoration === dec}
49+
onCheckedChange={() => setDecoration(dec)}
50+
>
51+
{dec}
52+
</MenuItemRadio>
53+
)),
54+
<MenuItemSwitch
55+
id="toggle-thing"
56+
checked={checked}
57+
onCheckedChange={(checked) => setChecked(checked)}
58+
>
59+
Do Stuff?
60+
</MenuItemSwitch>,
61+
]}
62+
>
63+
Menu
64+
</DropdownMenu>
65+
);
66+
}

packages/documentation/src/components/Demos/Form/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ import discreteSliders from "./DiscreteSliders.md";
6666
import ConfigurableSlider from "./ConfigurableSlider";
6767
import configurableSlider from "./ConfigurableSlider.md";
6868

69+
import MenusWithFormControls from "./MenusWithFormControls";
70+
import menusWithFormControls from "./MenusWithFormControls.md";
71+
6972
import ExampleForm from "./ExampleForm";
7073
import exampleForm from "./ExampleForm.md";
7174

@@ -173,6 +176,11 @@ const demos = [
173176
description: configurableSlider,
174177
children: <ConfigurableSlider />,
175178
},
179+
{
180+
name: "Menus with Form Controls",
181+
description: menusWithFormControls,
182+
children: <MenusWithFormControls />,
183+
},
176184
{
177185
name: "Example Form",
178186
description: exampleForm,

0 commit comments

Comments
 (0)