-
Notifications
You must be signed in to change notification settings - Fork 334
Expand file tree
/
Copy pathRadioGroup.vue
More file actions
341 lines (303 loc) · 7.83 KB
/
RadioGroup.vue
File metadata and controls
341 lines (303 loc) · 7.83 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<script lang="ts">
import { PropType, defineComponent } from 'vue';
import { _VIEW } from '@shell/config/query-params';
import RadioButton from '@components/Form/Radio/RadioButton.vue';
import { generateRandomAlphaString } from '@shell/utils/string';
interface Option {
value: unknown,
label: string,
description?: string,
radioOptionId?: string,
}
export default defineComponent({
name: 'RadioGroup',
components: { RadioButton },
props: {
/**
* Name for the checkbox grouping, must be unique on page.
*/
name: {
type: String,
required: true
},
/**
* Options can be an array of {label, value}, or just values.
*/
options: {
type: Array as PropType<Option[] | string[]>,
required: true
},
/**
* If options are just values, then labels can be a corresponding display
* value.
*/
labels: {
type: Array as PropType<string[]>,
default: null
},
/**
* The selected value.
*/
value: {
type: [Boolean, String, Object],
default: null
},
/**
* Disable the radio group.
*/
disabled: {
type: Boolean,
default: false
},
/**
* The radio group editing mode.
* @values _EDIT, _VIEW
*/
mode: {
type: String,
default: 'edit'
},
/**
* Label for above the radios.
*/
label: {
type: String,
default: null
},
/**
* The i18n key to use for the radio group label.
*/
labelKey: {
type: String,
default: null
},
/**
* Radio group tooltip.
*/
tooltip: {
type: [String, Object],
default: null
},
/**
* The i18n key to use for the radio group tooltip.
*/
tooltipKey: {
type: String,
default: null
},
/**
* Show radio buttons in column or row.
*/
row: {
type: Boolean,
default: false
}
},
emits: ['update:value'],
data() {
return {
currFocusedElem: undefined as undefined | EventTarget | null,
radioOptionsIdPrefix: `radio-option-${ generateRandomAlphaString(12) }-`
};
},
computed: {
/**
* Creates a collection of Options from the provided props.
*/
normalizedOptions(): Option[] {
const out: Option[] = [];
for (let i = 0; i < this.options.length; i++) {
const opt = this.options[i];
if (typeof opt === 'object' && opt) {
out.push({
...opt,
radioOptionId: `${ this.radioOptionsIdPrefix }${ i }`
});
} else if (this.labels) {
out.push({
label: this.labels[i],
value: opt,
radioOptionId: `${ this.radioOptionsIdPrefix }${ i }`
});
} else {
out.push({
label: opt,
value: opt,
radioOptionId: `${ this.radioOptionsIdPrefix }${ i }`
});
}
}
return out;
},
/**
* Determines the view mode for the radio group.
*/
isView(): boolean {
return this.mode === _VIEW;
},
/**
* Determines if the radio group is disabled.
*/
isDisabled(): boolean {
return (this.disabled || this.isView);
},
/**
* Radio Group Aria Label based on the label present on this input
*/
radioGroupAriaLabel(): string | undefined {
// seems like VoiceOver screen reader isn't really picking up aria-labelledby
// let's just gather the label that comes in and assign it.
// We allow override with $attrs['aria-label'] for more control
if (this.$attrs['aria-label']) {
return this.$attrs['aria-label'] as string || undefined;
}
return this.labelKey ? this.t(this.labelKey) : this.label ? this.label : undefined;
},
/**
* Radio Group Aria DescribedBy parent attribute for extendability
*/
radioGroupAriaDescribedBy(): string | undefined {
return this.$attrs['aria-describedby'] as string || undefined;
},
/**
* Radio Group value for aria-activedescendant HTML prop
*/
ariaActiveDescendant(): string | undefined {
const activeOpt = this.normalizedOptions.find((opt) => opt.value === this.value);
if (this.value && activeOpt) {
return activeOpt.radioOptionId;
}
return '';
}
},
beforeUnmount() {
const radioGroup = this.$refs?.radioGroup as HTMLInputElement;
radioGroup.removeEventListener('focusin', this.focusChanged);
},
mounted() {
const radioGroup = this.$refs?.radioGroup as HTMLInputElement;
radioGroup.addEventListener('focusin', this.focusChanged);
},
methods: {
focusChanged(ev: Event) {
this.currFocusedElem = ev.target;
},
/**
* Keyboard left/right event listener to select next/previous option. Emits
* the input event.
*/
clickNext(direction: number): void {
// moving focus away from a custom group element and pressing arrow keys
// should not have any effect on the group - custom UI for radiogroup option(s)
if (this.currFocusedElem !== this.$refs?.radioGroup) {
return;
}
const opts = this.normalizedOptions;
const selected = opts.find((x) => x.value === this.value);
let newIndex = (selected ? opts.indexOf(selected) : -1) + direction;
if (newIndex >= opts.length) {
newIndex = opts.length - 1;
} else if (newIndex < 0) {
newIndex = 0;
}
this.$emit('update:value', opts[newIndex].value);
}
}
});
</script>
<template>
<div>
<!-- Label -->
<div
v-if="label || labelKey || tooltip || tooltipKey || $slots.label"
class="radio-group label"
>
<slot name="label">
<h3>
<t
v-if="labelKey"
:k="labelKey"
/>
<template v-else-if="label">
{{ label }}
</template>
<i
v-if="tooltipKey"
v-clean-tooltip="t(tooltipKey)"
class="icon icon-info icon-lg"
/>
<i
v-else-if="tooltip"
v-clean-tooltip="tooltip"
class="icon icon-info icon-lg"
/>
</h3>
</slot>
</div>
<!-- Group -->
<div
ref="radioGroup"
role="radiogroup"
:aria-label="radioGroupAriaLabel"
:aria-describedby="radioGroupAriaDescribedBy"
:aria-activedescendant="ariaActiveDescendant"
class="radio-group"
:class="{'row':row}"
:tabindex="isDisabled ? -1 : 0"
:aria-disabled="isDisabled"
@keydown.down.prevent.stop="clickNext(1)"
@keydown.up.prevent.stop="clickNext(-1)"
@keydown.space.enter.stop.prevent
>
<div
v-for="(option, i) in normalizedOptions"
:key="i"
>
<slot
:v-bind="$attrs"
:option="option"
:is-disabled="isDisabled"
:name="i"
>
<!-- Default input -->
<RadioButton
:name="name"
:value="value"
:label="option.label"
:radio-option-id="option.radioOptionId"
:description="option.description"
:val="option.value"
:disabled="isDisabled"
:data-testid="`radio-button-${i}`"
:mode="mode"
:prevent-focus-on-radio-groups="true"
@update:value="$emit('update:value', $event)"
/>
</slot>
</div>
</div>
</div>
</template>
<style lang='scss' scoped>
.radio-group {
&:focus, &:focus-visible {
border: none;
outline: none;
}
&:focus-visible .radio-button-checked {
@include focus-outline;
}
h3 {
position: relative;
}
&.row {
display: flex;
.radio-container {
margin-right: 10px;
}
}
.label{
font-size: 14px !important;
}
}
</style>