Skip to content

Commit 1ccaef7

Browse files
marissahuysentruytcastastrophe
authored andcommitted
docs(form): adds missing stories, controls, test cases
- adds missing stories and corresponding documentation - adds new test cases - adds fieldLabelAlignment control for left/right aligned field labels
1 parent 239ef97 commit 1ccaef7

File tree

3 files changed

+90
-14
lines changed

3 files changed

+90
-14
lines changed

components/fieldlabel/stories/form.stories.js

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
1+
import { Template as Checkbox } from "@spectrum-css/checkbox/stories/template.js";
2+
import { Template as Fieldgroup } from "@spectrum-css/fieldgroup/stories/template.js";
13
import { Template as Picker } from "@spectrum-css/picker/stories/template.js";
24
import { disableDefaultModes } from "@spectrum-css/preview/modes";
35
import { Template as Stepper } from "@spectrum-css/stepper/stories/template.js";
46
import { Template as TextField } from "@spectrum-css/textfield/stories/template.js";
7+
import { Template } from "./form.template.js";
58
import pkgJson from "../package.json";
69
import { FormGroup } from "./form.test.js";
710

811
/**
9-
* The form component is used for aligning multiple inputs and field groups within a form.
12+
* The form component is used for aligning multiple inputs and field groups within a form. It provides structure and spacing for your form fields.
1013
*/
1114
export default {
1215
title: "Form",
1316
component: "Form",
1417
argTypes: {
15-
labelsAbove: {
16-
name: "Labels above",
17-
type: { name: "boolean" },
18+
labelPosition: {
19+
name: "Label position",
20+
description: "Position of the field label in relation to the input.",
21+
type: { name: "string" },
1822
table: {
19-
type: { summary: "boolean" },
23+
type: { summary: "string" },
2024
category: "Component",
2125
},
22-
control: "boolean",
26+
control: "select",
27+
options: ["top", "side"],
2328
},
29+
fieldLabelAlignment: {
30+
name: "Field label alignment",
31+
type: { name: "string" },
32+
table: {
33+
type: { summary: "string" },
34+
category: "Component",
35+
},
36+
control: "select",
37+
options: ["left", "right"],
38+
if: { arg: "labelPosition", eq: "side" },
39+
},
40+
items: { table: { disable: true } },
2441
},
2542
args: {
2643
rootClass: "spectrum-Form",
27-
labelsAbove: false,
44+
labelPosition: "top",
2845
},
2946
parameters: {
3047
packageJson: pkgJson,
3148
},
3249
};
3350

51+
/**
52+
* A stacked layout with the labels above the form fields. The class `.spectrum-Form--labelPosition` is applied to the form itself. You do not need to apply a specific class to the field label because `.spectrum-FieldLabel--left` is applied to each to each [field label](/docs/components-field-label--docs) by default.
53+
*/
3454
export const Default = FormGroup.bind({});
3555
Default.args = {
3656
items: [
@@ -65,7 +85,26 @@ Default.args = {
6585
}, context),
6686
],
6787
}, {
68-
label: "Amount",
88+
label: "Interest",
89+
id: "form-example-interests",
90+
content: [
91+
(passthroughs, context) => Fieldgroup({
92+
layout: "horizontal",
93+
items: [
94+
Checkbox({
95+
...passthroughs,
96+
label: "Kittens",
97+
customClasses: ["spectrum-FieldGroup-item"],
98+
}, context),
99+
Checkbox({
100+
...passthroughs,
101+
label: "Puppies",
102+
customClasses: ["spectrum-FieldGroup-item"],
103+
}, context),]
104+
}),
105+
],
106+
},{
107+
label: "Age",
69108
id: "form-example-amount",
70109
content: [
71110
(passthroughs, context) => Stepper({
@@ -76,6 +115,35 @@ Default.args = {
76115
],
77116
};
78117

118+
/**
119+
* A two column layout with left-aligned label text.
120+
*/
121+
export const LeftAlignedSideLabels = Template.bind({});
122+
LeftAlignedSideLabels.args = {
123+
...Default.args,
124+
labelPosition: "side",
125+
};
126+
LeftAlignedSideLabels.tags = ["!dev"];
127+
LeftAlignedSideLabels.parameters = {
128+
chromatic: { disableSnapshot: true },
129+
};
130+
LeftAlignedSideLabels.storyName = "Left-aligned side labels";
131+
132+
/**
133+
* A two column layout with right-aligned label text. `.spectrum-FieldLabel--right` is applied to each to each [field label](/docs/components-field-label--docs).
134+
*/
135+
export const RightAlignedSideLabels = Template.bind({});
136+
RightAlignedSideLabels.args = {
137+
...Default.args,
138+
labelPosition: "side",
139+
fieldLabelAlignment: "right",
140+
};
141+
RightAlignedSideLabels.tags = ["!dev"];
142+
RightAlignedSideLabels.parameters = {
143+
chromatic: { disableSnapshot: true },
144+
};
145+
RightAlignedSideLabels.storyName = "Right-aligned side labels";
146+
79147
// ********* VRT ONLY ********* //
80148
export const WithForcedColors = FormGroup.bind({});
81149
WithForcedColors.args = Default.args;

components/fieldlabel/stories/form.template.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import "../index.css";
1111

1212
export const Template = ({
1313
rootClass = "spectrum-Form",
14-
labelsAbove = false,
14+
labelPosition = "top",
15+
fieldLabelAlignment = "left",
1516
customClasses = [],
1617
customStyles = {},
1718
id = getRandomId("form"),
@@ -20,7 +21,7 @@ export const Template = ({
2021
<form
2122
class=${classMap({
2223
[rootClass]: true,
23-
[`${rootClass}--labelsAbove`]: labelsAbove,
24+
[`${rootClass}--labelsAbove`]: labelPosition === "top",
2425
...customClasses.reduce((a, c) => ({ ...a, [c]: true }), {}),
2526
})}
2627
id=${ifDefined(id)}
@@ -36,7 +37,7 @@ export const Template = ({
3637
${when(label, () => FieldLabel({
3738
label,
3839
forInput: item.id,
39-
alignment: labelsAbove ? undefined : "left",
40+
alignment: labelPosition === "side" ? fieldLabelAlignment : undefined,
4041
}, context))}
4142
<div class=${classMap({
4243
[`${rootClass}-itemField`]: true,

components/fieldlabel/stories/form.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ import { Template } from "./form.template";
44
export const FormGroup = Variants({
55
Template,
66
testData: [
7-
{},
87
{
9-
testHeading: "Labels above",
10-
labelsAbove: true,
8+
testHeading: "Default",
9+
},
10+
{
11+
testHeading: "Side labels with left-aligned text",
12+
labelPosition: "side",
13+
},
14+
{
15+
testHeading: "Side labels with right-aligned text",
16+
labelPosition: "side",
17+
fieldLabelAlignment: "right",
1118
}
1219
],
1320
});

0 commit comments

Comments
 (0)