-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathNcFormGroup.vue
More file actions
158 lines (137 loc) Ā· 3.63 KB
/
NcFormGroup.vue
File metadata and controls
158 lines (137 loc) Ā· 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!--
- SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<script setup lang="ts">
import type { Slot } from 'vue'
import { createElementId } from '../../utils/createElementId.ts'
const {
label = undefined,
description = undefined,
hideLabel = false,
hideDescription = false,
noGap = false,
} = defineProps<{
/**
* Group label #label slot can be used for custom label content
*/
label?: string
/**
* Optional fieldset description. #description slot can be used for custom description content
*/
description?: string
/**
* Hide the label visually but keep it accessible for screen readers
*/
hideLabel?: boolean
/**
* Hide the description visually but keep it accessible for screen readers
*/
hideDescription?: boolean
/**
* Disable default fieldset content gap between content elements
*/
noGap?: boolean
}>()
const slots = defineSlots<{
/**
* Content
*/
default?: Slot
/**
* Custom label content
*/
label?: Slot
/**
* Custom description content
*/
description?: Slot
}>()
const id = `nc-form-group-${createElementId()}`
const descriptionId = `${id}-description`
const hasDescription = () => !!description || !!slots.description
const getDescriptionId = () => hasDescription() ? descriptionId : undefined
const hasContentOnly = () => hideLabel && (!hasDescription() || hideDescription)
</script>
<template>
<fieldset
:class="[$style.formGroup, { [$style.formGroup_noGap]: noGap }]"
:aria-describedby="getDescriptionId()">
<legend :class="[$style.formGroup__label, { 'hidden-visually': hideLabel }]">
<slot name="label">
{{ label || 'ā ļø Missing label' }}
</slot>
</legend>
<div v-if="hasDescription()" :id="descriptionId" :class="[$style.formGroup__description, { 'hidden-visually': hideDescription }]">
<slot name="description">
{{ description }}
</slot>
</div>
<div :class="[$style.formGroup__content, { [$style.formGroup__content_only]: hasContentOnly() }]">
<slot />
</div>
</fieldset>
</template>
<style lang="scss" module>
.formGroup {
--form-element-label-offset: calc(var(--border-radius-element) + var(--default-grid-baseline));
--form-group-content-gap: calc(2 * var(--default-grid-baseline));
&.formGroup_noGap {
--form-group-content-gap: 0;
}
}
.formGroup__label {
padding-inline: var(--form-element-label-offset);
font-size: var(--font-size);
font-weight: var(--font-weight-heading, bold);
}
.formGroup__description {
padding-inline: var(--form-element-label-offset);
color: var(--color-text-maxcontrast);
}
.formGroup__content {
display: flex;
flex-direction: column;
gap: var(--form-group-content-gap);
margin-block-start: calc(2.5 * var(--default-grid-baseline));
&.formGroup__content_only {
margin-block-start: 0;
}
}
</style>
<docs>
### General
Labelled group of form elements.
```vue
<template>
<NcFormGroup label="Personal information">
<NcTextField label="First name" />
<NcTextField label="Last name" />
</NcFormGroup>
</template>
```
### With description
```vue
<template>
<NcFormGroup label="Personal information" description="Your contact details">
<NcTextField label="First name" />
<NcTextField label="Last name" />
</NcFormGroup>
</template>
```
### Hidden label/description
You can visually hide the label and/or the description.\
Note: you still must provide the label. Do not visually hide the missing label warning!
```vue
<template>
<NcFormGroup
label="Personal information"
description="Your contact details"
hide-label
hide-description>
<NcTextField label="First name" />
<NcTextField label="Last name" />
</NcFormGroup>
</template>
```
</docs>