|
| 1 | +--- |
| 2 | +title: Form Group Guidance |
| 3 | +component: formgroup |
| 4 | +description: FormGroup is a wrapper component used to group multiple form controls such as checkboxes or switches together, providing consistent spacing and layout. |
| 5 | +--- |
| 6 | + |
| 7 | +import { FormGroup, FormControlLabel, FormControl, FormLabel, Checkbox, Switch, Box } from "@sistent/sistent"; |
| 8 | + |
| 9 | +export const VerticalFormGroupDemo = () => { |
| 10 | + return ( |
| 11 | + <Box sx={{ display: "flex", justifyContent: "center" }}> |
| 12 | + <FormControl component="fieldset"> |
| 13 | + <FormLabel component="legend">Vertical Group</FormLabel> |
| 14 | + <FormGroup> |
| 15 | + <FormControlLabel control={<Checkbox defaultChecked />} label="Option A" /> |
| 16 | + <FormControlLabel control={<Checkbox />} label="Option B" /> |
| 17 | + <FormControlLabel control={<Checkbox />} label="Option C" /> |
| 18 | + </FormGroup> |
| 19 | + </FormControl> |
| 20 | + </Box> |
| 21 | + ); |
| 22 | +}; |
| 23 | + |
| 24 | +export const HorizontalFormGroupDemo = () => { |
| 25 | + return ( |
| 26 | + <Box sx={{ display: "flex", justifyContent: "center" }}> |
| 27 | + <FormControl component="fieldset"> |
| 28 | + <FormLabel component="legend">Horizontal Group</FormLabel> |
| 29 | + <FormGroup row> |
| 30 | + <FormControlLabel control={<Checkbox defaultChecked />} label="Option A" /> |
| 31 | + <FormControlLabel control={<Checkbox />} label="Option B" /> |
| 32 | + <FormControlLabel control={<Checkbox />} label="Option C" /> |
| 33 | + </FormGroup> |
| 34 | + </FormControl> |
| 35 | + </Box> |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +export const SwitchFormGroupDemo = () => { |
| 40 | + return ( |
| 41 | + <Box sx={{ display: "flex", justifyContent: "center" }}> |
| 42 | + <FormControl component="fieldset"> |
| 43 | + <FormLabel component="legend">Notifications</FormLabel> |
| 44 | + <FormGroup> |
| 45 | + <FormControlLabel control={<Switch defaultChecked />} label="Email Notifications" /> |
| 46 | + <FormControlLabel control={<Switch />} label="Push Notifications" /> |
| 47 | + <FormControlLabel control={<Switch />} label="SMS Notifications" /> |
| 48 | + </FormGroup> |
| 49 | + </FormControl> |
| 50 | + </Box> |
| 51 | + ); |
| 52 | +}; |
| 53 | + |
| 54 | +<a id="Functions"> |
| 55 | + <h2>Functions</h2> |
| 56 | +</a> |
| 57 | + |
| 58 | +<p> |
| 59 | + The FormGroup component provides a structured way to group related form controls |
| 60 | + together. It ensures consistent spacing, alignment, and layout for multiple |
| 61 | + checkboxes or switches in a form. |
| 62 | +</p> |
| 63 | + |
| 64 | +<h3>Vertical Layout</h3> |
| 65 | +<p> |
| 66 | + By default, FormGroup stacks its children vertically, which is ideal for longer |
| 67 | + lists of options where each item needs its own line for clarity. |
| 68 | +</p> |
| 69 | + |
| 70 | +<Row $Hcenter className="image-container"> |
| 71 | + <ThemeWrapper> |
| 72 | + <VerticalFormGroupDemo /> |
| 73 | + </ThemeWrapper> |
| 74 | +</Row> |
| 75 | + |
| 76 | +<h3>Horizontal Layout</h3> |
| 77 | +<p> |
| 78 | + Use the <code>row</code> prop to arrange form controls horizontally. This is |
| 79 | + useful when options are short and can fit side by side without crowding. |
| 80 | +</p> |
| 81 | + |
| 82 | +<Row $Hcenter className="image-container"> |
| 83 | + <ThemeWrapper> |
| 84 | + <HorizontalFormGroupDemo /> |
| 85 | + </ThemeWrapper> |
| 86 | +</Row> |
| 87 | + |
| 88 | +<h3>With Switches</h3> |
| 89 | +<p> |
| 90 | + FormGroup works equally well with Switch components, making it ideal for |
| 91 | + settings panels and preference toggles. |
| 92 | +</p> |
| 93 | + |
| 94 | +<Row $Hcenter className="image-container"> |
| 95 | + <ThemeWrapper> |
| 96 | + <SwitchFormGroupDemo /> |
| 97 | + </ThemeWrapper> |
| 98 | +</Row> |
| 99 | + |
| 100 | +<a id="Best-Practices"> |
| 101 | + <h2>Best Practices</h2> |
| 102 | +</a> |
| 103 | + |
| 104 | +<h3>When to Use FormGroup</h3> |
| 105 | +<ul> |
| 106 | + <li>Use when grouping two or more related checkboxes or switches</li> |
| 107 | + <li>Wrap inside a <code>FormControl</code> with a <code>FormLabel</code> for proper accessibility</li> |
| 108 | + <li>Use <code>row</code> prop for short option labels that fit horizontally</li> |
| 109 | + <li>Prefer vertical layout for longer labels or more than four options</li> |
| 110 | +</ul> |
| 111 | + |
| 112 | +<h3>Common Patterns</h3> |
| 113 | +<ul> |
| 114 | + <li>Always pair with <code>FormControlLabel</code> to provide descriptive labels</li> |
| 115 | + <li>Use <code>FormLabel</code> as a group heading for screen reader support</li> |
| 116 | + <li>Keep option labels concise and scannable</li> |
| 117 | + <li>Use consistent control types within a single FormGroup (all checkboxes or all switches)</li> |
| 118 | +</ul> |
| 119 | + |
| 120 | +<h3>Accessibility</h3> |
| 121 | +<ul> |
| 122 | + <li>Wrap FormGroup in a <code>FormControl</code> with <code>component="fieldset"</code></li> |
| 123 | + <li>Always include a <code>FormLabel</code> with <code>component="legend"</code> for screen readers</li> |
| 124 | + <li>Ensure each control has a descriptive label via <code>FormControlLabel</code></li> |
| 125 | + <li>Supports full keyboard navigation out of the box</li> |
| 126 | +</ul> |
| 127 | + |
| 128 | +<a id="Customization"> |
| 129 | + <h2>Customization</h2> |
| 130 | +</a> |
| 131 | + |
| 132 | +<p> |
| 133 | + FormGroup can be customized through props and styling to match your design requirements. |
| 134 | +</p> |
| 135 | + |
| 136 | +<h3>Props</h3> |
| 137 | +<ul> |
| 138 | + <li><code>children</code>: The form controls to be grouped (e.g., FormControlLabel with Checkbox or Switch)</li> |
| 139 | + <li><code>row</code>: If <code>true</code>, the group is displayed in a horizontal row instead of a column</li> |
| 140 | + <li><code>sx</code>: Custom styling using the theme system</li> |
| 141 | + <li><code>classes</code>: Override or extend the styles applied to the component</li> |
| 142 | +</ul> |
0 commit comments