Skip to content

Commit d3a8b58

Browse files
committed
form item default layout
1 parent 6f43381 commit d3a8b58

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

packages/uui-form-item/lib/uui-form-item.element.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LitElement, html, css } from 'lit';
2+
import { property, state } from 'lit/decorators.js';
23
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
34

45
// TODO: Make sure validation messages can be seen for the whole Form Item. Make them follow the screen if form controls are taller than available screen height.
@@ -15,13 +16,64 @@ export class UUIFormItemElement extends LitElement {
1516
static styles = [
1617
css`
1718
:host {
18-
/* Styles goes here */
19+
position: relative;
20+
display: block;
21+
margin-top: var(--uui-size-space-5);
22+
margin-bottom: var(--uui-size-space-5);
23+
}
24+
#label {
25+
margin-top: -5px;
26+
margin-bottom: 5px;
27+
}
28+
#description {
29+
color: var(--uui-interface-contrast-disabled);
30+
font-size: var(--uui-type-small-size);
31+
}
32+
#label + #description {
33+
margin-top: -8px;
34+
min-height: 8px;
1935
}
2036
`,
2137
];
38+
/*
39+
@property({type: String})
40+
label: string | null = null;
41+
*/
42+
43+
@property({ type: String })
44+
description: string | null = null;
45+
46+
@state()
47+
private _labelSlotHasContent = false;
48+
49+
private _labelSlotChanged = (e: Event) => {
50+
this._labelSlotHasContent =
51+
(e.target as HTMLSlotElement).assignedNodes({ flatten: true }).length > 0;
52+
};
53+
54+
@state()
55+
private _descriptionSlotHasContent = false;
56+
57+
private _descriptionSlotChanged = (e: Event) => {
58+
this._descriptionSlotHasContent =
59+
(e.target as HTMLSlotElement).assignedNodes({ flatten: true }).length > 0;
60+
};
2261

2362
render() {
2463
return html`
64+
<div id="label" style=${this._labelSlotHasContent ? '' : 'display: none'}>
65+
<slot name="label" @slotchange=${this._labelSlotChanged}></slot>
66+
</div>
67+
<div
68+
id="description"
69+
style=${this._descriptionSlotHasContent || this.description !== null
70+
? ''
71+
: 'display: none'}>
72+
${this.description}
73+
<slot
74+
name="description"
75+
@slotchange=${this._descriptionSlotChanged}></slot>
76+
</div>
2577
<uui-form-validation-message>
2678
<slot></slot>
2779
<slot name="message" slot="message"></slot>

packages/uui-form-item/lib/uui-form-item.story.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ export const Overview: Story = () => html`
2020
</uui-form-item>
2121
`;
2222

23-
export const MultipleInputs: Story = () => html` <form
23+
export const Example: Story = () => html` <form
2424
is="uui-form"
2525
style="max-width: 800px;">
2626
<uui-form-item>
2727
<uui-label slot="label" for="phoneInput" required>Phone number</uui-label>
28+
<span slot="description"
29+
>Form item accepts a description, keep it short.</span
30+
>
2831
<div>
2932
<uui-input
3033
id="phoneInput"
@@ -44,7 +47,31 @@ export const MultipleInputs: Story = () => html` <form
4447
</uui-input>
4548
</div>
4649
</uui-form-item>
47-
50+
<uui-form-item>
51+
<uui-label slot="label" for="cityinput">City</uui-label>
52+
<span slot="description"></span>
53+
<div>
54+
<uui-input
55+
id="cityinput"
56+
type="text"
57+
name="phone"
58+
placeholder=""
59+
required
60+
required-message="You must enter a city">
61+
</uui-input>
62+
</div>
63+
</uui-form-item>
64+
<uui-form-item>
65+
<div>
66+
<uui-input
67+
type="text"
68+
name="postal"
69+
placeholder=""
70+
required
71+
required-message="You must enter a postal number">
72+
</uui-input>
73+
</div>
74+
</uui-form-item>
4875
<uui-button type="submit" label="Submit" look="positive"> Submit </uui-button>
4976
5077
<uui-button type="reset" label="Reset" look="secondary"> Reset </uui-button>

0 commit comments

Comments
 (0)