-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathNcCheckboxContent.vue
More file actions
287 lines (255 loc) · 5.98 KB
/
NcCheckboxContent.vue
File metadata and controls
287 lines (255 loc) · 5.98 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!--
- SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<span
class="checkbox-content"
:class="{
['checkbox-content-' + type]: true,
'checkbox-content--button-variant': buttonVariant,
'checkbox-content--has-text': !!$slots.default,
}">
<!--
label can't be used here because of shift+click firefox bug
https://bugzilla.mozilla.org/show_bug.cgi?id=559506
-->
<span
class="checkbox-content__icon"
:class="{
'checkbox-content__icon--checked': isChecked,
[iconClass]: true,
}"
:aria-hidden="true"
inert>
<!-- @slot The checkbox/radio icon, you can use it for adding an icon to the button variant
@binding {bool} checked The input checked state
@binding {bool} loading The loading state
-->
<slot
name="icon"
:checked="isChecked"
:loading="loading">
<NcLoadingIcon v-if="loading" />
<NcIconToggleSwitch
v-else-if="isSwitchType"
:checked="isChecked"
:size="iconSize"
inline />
<component
:is="checkboxRadioIconElement"
v-else-if="!buttonVariant"
:size="iconSize" />
</slot>
</span>
<span v-if="$slots.default || $slots.description" class="checkbox-content__wrapper">
<span
v-if="$slots.default"
:id="labelId"
class="checkbox-content__text"
:class="textClass">
<!-- @slot The checkbox/radio label -->
<slot />
</span>
<span v-if="!isButtonType && $slots.description" :id="descriptionId" class="checkbox-content__description">
<slot name="description" />
</span>
</span>
</span>
</template>
<script>
import CheckboxBlankOutline from 'vue-material-design-icons/CheckboxBlankOutline.vue'
import CheckboxMarked from 'vue-material-design-icons/CheckboxMarked.vue'
import MinusBox from 'vue-material-design-icons/MinusBox.vue'
import RadioboxBlank from 'vue-material-design-icons/RadioboxBlank.vue'
import RadioboxMarked from 'vue-material-design-icons/RadioboxMarked.vue'
import NcIconToggleSwitch from '../NcIconToggleSwitch/NcIconToggleSwitch.vue'
import NcLoadingIcon from '../NcLoadingIcon/index.js'
export const TYPE_CHECKBOX = 'checkbox'
export const TYPE_RADIO = 'radio'
export const TYPE_SWITCH = 'switch'
export const TYPE_BUTTON = 'button'
export default {
name: 'NcCheckboxContent',
components: {
NcLoadingIcon,
NcIconToggleSwitch,
},
props: {
/**
* Class for the icon element
*/
iconClass: {
type: [String, Object],
default: null,
},
/**
* Class for the text element
*/
textClass: {
type: [String, Object],
default: null,
},
/**
* Type of the input. checkbox, radio, switch, or button.
*
* Only use button when used in a `tablist` container and the
* `tab` role is set.
*
* @type {'checkbox'|'radio'|'switch'|'button'}
*/
type: {
type: String,
default: 'checkbox',
validator: (type) => [
TYPE_CHECKBOX,
TYPE_RADIO,
TYPE_SWITCH,
TYPE_BUTTON,
].includes(type),
},
/**
* Toggle the alternative button style
*/
buttonVariant: {
type: Boolean,
default: false,
},
/**
* True if the entry is checked
*/
isChecked: {
type: Boolean,
default: false,
},
/**
* Indeterminate state
*/
indeterminate: {
type: Boolean,
default: false,
},
/**
* Loading state
*/
loading: {
type: Boolean,
default: false,
},
/**
* Icon size
*/
iconSize: {
type: Number,
default: 24,
},
/**
* Label id attribute
*/
labelId: {
type: String,
required: true,
},
/**
* Description id attribute
*/
descriptionId: {
type: String,
required: true,
},
},
computed: {
isButtonType() {
return this.type === TYPE_BUTTON
},
isSwitchType() {
return this.type === TYPE_SWITCH
},
/**
* Returns the proper Material icon depending on the select case
*
* @return {object}
*/
checkboxRadioIconElement() {
if (this.type === TYPE_RADIO) {
if (this.isChecked) {
return RadioboxMarked
}
return RadioboxBlank
}
// Checkbox
if (this.indeterminate) {
return MinusBox
}
if (this.isChecked) {
return CheckboxMarked
}
return CheckboxBlankOutline
},
},
}
</script>
<style lang="scss" scoped>
.checkbox-content {
display: flex;
align-items: center;
flex-direction: row;
gap: var(--default-grid-baseline);
user-select: none;
min-height: var(--default-clickable-area);
border-radius: var(--checkbox-radio-switch--border-radius);
padding: var(--default-grid-baseline) calc((var(--default-clickable-area) - var(--icon-height)) / 2);
// Set to 100% to make text overflow work on button style
width: 100%;
// but restrict to content so plain checkboxes / radio switches do not expand
max-width: fit-content;
&__wrapper {
flex: 1 0;
}
&__text {
&:empty {
// hide text if empty to ensure checkbox outline is a circle instead of oval
display: none;
}
}
&-checkbox:not(&--button-variant) &__icon,
&-radio:not(&--button-variant) &__icon,
&-switch:not(&--button-variant) &__icon {
margin-block: calc((var(--default-clickable-area) - 2 * var(--default-grid-baseline) - var(--icon-height)) / 2) auto;
line-height: 0;
}
&-checkbox:not(&--button-variant) &__icon--has-description,
&-radio:not(&--button-variant) &__icon--has-description,
&-switch:not(&--button-variant) &__icon--has-description {
display: flex;
align-items: center;
margin-block-end: 0;
align-self: start;
}
&__icon > * {
width: var(--icon-size);
height: var(--icon-height);
color: var(--color-primary-element);
}
&__description {
display: block;
color: var(--color-text-maxcontrast);
font-weight: var(--font-weight-default, normal);
}
&--button-variant {
.checkbox-content__icon:not(.checkbox-content__icon--checked) > * {
color: var(--color-primary-element);
}
.checkbox-content__icon--checked > * {
color: var(--color-primary-element-text);
}
}
&--has-text {
padding-right: $icon-margin;
}
&, * {
cursor: pointer;
flex-shrink: 0;
}
}
</style>