-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathradio-group.tsx
More file actions
385 lines (329 loc) · 12.4 KB
/
radio-group.tsx
File metadata and controls
385 lines (329 loc) · 12.4 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Build, Component, Element, Event, Host, Listen, Method, Prop, State, Watch, h } from '@stencil/core';
import { checkInvalidState } from '@utils/forms';
import { debounce, renderHiddenInput } from '@utils/helpers';
import { hostContext } from '@utils/theme';
import { getIonTheme } from '../../global/ionic-global';
import type { RadioGroupChangeEventDetail, RadioGroupCompareFn } from './radio-group-interface';
/**
* @virtualProp {"ios" | "md"} mode - The mode determines the platform behaviors of the component.
* @virtualProp {"ios" | "md" | "ionic"} theme - The theme determines the visual appearance of the component.
*
* @part supporting-text - Supporting text displayed above the radios.
* @part helper-text - Supporting text displayed above the radios when the radio group is valid.
* @part error-text - Supporting text displayed above the radios when the radio group is invalid and touched.
*/
@Component({
tag: 'ion-radio-group',
styleUrls: {
ios: 'radio-group.ios.scss',
md: 'radio-group.md.scss',
ionic: 'radio-group.ionic.scss',
},
shadow: true,
})
export class RadioGroup implements ComponentInterface {
private inputId = `ion-rg-${radioGroupIds++}`;
private helperTextId = `${this.inputId}-helper-text`;
private errorTextId = `${this.inputId}-error-text`;
private labelId = `${this.inputId}-lbl`;
private label?: HTMLIonLabelElement | null;
private validationObserver?: MutationObserver;
@Element() el!: HTMLElement;
/**
* Track validation state for proper aria-live announcements.
*/
@State() isInvalid = false;
@State() private hintTextId?: string;
/**
* If `true`, the radios can be deselected.
*/
@Prop() allowEmptySelection = false;
/**
* This property allows developers to specify a custom function or property
* name for comparing objects when determining the selected option in the
* ion-radio-group. When not specified, the default behavior will use strict
* equality (===) for comparison.
*/
@Prop() compareWith?: string | RadioGroupCompareFn | null;
/**
* The name of the control, which is submitted with the form data.
*/
@Prop() name: string = this.inputId;
/**
* the value of the radio group.
*/
@Prop({ mutable: true }) value?: any | null;
/**
* The helper text to display at the top of the radio group.
*/
@Prop() helperText?: string;
/**
* The error text to display at the top of the radio group.
*/
@Prop() errorText?: string;
@Watch('value')
valueChanged(value: any | undefined) {
this.setRadioTabindex(value);
this.ionValueChange.emit({ value });
}
/**
* Emitted when the value has changed.
*
* This event will not emit when programmatically setting the `value` property.
*/
@Event() ionChange!: EventEmitter<RadioGroupChangeEventDetail>;
/**
* Emitted when the `value` property has changed.
* This is used to ensure that `ion-radio` can respond
* to any value property changes from the group.
*
* @internal
*/
@Event() ionValueChange!: EventEmitter<RadioGroupChangeEventDetail>;
componentDidLoad() {
/**
* There's an issue when assigning a value to the radio group
* within the Angular primary content (rendering within the
* app component template). When the template is isolated to a route,
* the value is assigned correctly.
* To address this issue, we need to ensure that the watcher is
* called after the component has finished loading,
* allowing the emit to be dispatched correctly.
*/
this.valueChanged(this.value);
}
/** @internal - Recompute which radio has tabindex 0. Call when radios are added/removed. */
@Method()
async updateRadiosTabindex() {
this.scheduleTabindexUpdate();
}
/** Ensures that the tabindex update is debounced and only called once. */
private scheduleTabindexUpdate = debounce(() => this.setRadioTabindex(this.value), 0);
private setRadioTabindex = (value: any | undefined) => {
const radios = this.getRadios();
// Get the first radio that is not disabled and the checked one
const first = radios.find((radio) => !radio.disabled);
const checked = radios.find((radio) => radio.value === value && !radio.disabled);
if (!first && !checked) {
return;
}
// If an enabled checked radio exists, set it to be the focusable radio
// otherwise we default to focus the first radio
const focusable = checked || first;
for (const radio of radios) {
const tabindex = radio === focusable ? 0 : -1;
radio.setButtonTabindex(tabindex);
}
};
async connectedCallback() {
// Get the list header if it exists and set the id
// this is used to set aria-labelledby
const header = this.el.querySelector('ion-list-header') || this.el.querySelector('ion-item-divider');
if (header) {
const label = (this.label = header.querySelector('ion-label'));
if (label) {
this.labelId = label.id = this.name + '-lbl';
}
}
// Watch for class changes to update validation state.
if (Build.isBrowser && typeof MutationObserver !== 'undefined') {
this.validationObserver = new MutationObserver(() => {
const newIsInvalid = checkInvalidState(this.el);
if (this.isInvalid !== newIsInvalid) {
this.isInvalid = newIsInvalid;
/**
* Screen readers tend to announce changes
* to `aria-describedby` when the attribute
* is changed during a blur event for a
* native form control.
* However, the announcement can be spotty
* when using a non-native form control
* and `forceUpdate()`.
* This is due to `forceUpdate()` internally
* rescheduling the DOM update to a lower
* priority queue regardless if it's called
* inside a Promise or not, thus causing
* the screen reader to potentially miss the
* change.
* By using a State variable inside a Promise,
* it guarantees a re-render immediately at
* a higher priority.
*/
Promise.resolve().then(() => {
this.hintTextId = this.getHintTextId();
});
}
});
this.validationObserver.observe(this.el, {
attributes: true,
attributeFilter: ['class'],
});
}
// Always set initial state
this.isInvalid = checkInvalidState(this.el);
}
componentWillLoad() {
this.hintTextId = this.getHintTextId();
}
disconnectedCallback() {
// Clean up validation observer to prevent memory leaks.
if (this.validationObserver) {
this.validationObserver.disconnect();
this.validationObserver = undefined;
}
}
private getRadios(): HTMLIonRadioElement[] {
return Array.from(this.el.querySelectorAll('ion-radio'));
}
/**
* Emits an `ionChange` event.
*
* This API should be called for user committed changes.
* This API should not be used for external value changes.
*/
private emitValueChange(event?: Event) {
const { value } = this;
this.ionChange.emit({ value, event });
}
private onClick = (ev: Event) => {
ev.preventDefault();
/**
* The Radio Group component mandates that only one radio button
* within the group can be selected at any given time. Since `ion-radio`
* is a shadow DOM component, it cannot natively perform this behavior
* using the `name` attribute.
*/
const selectedRadio = ev.target && (ev.target as HTMLElement).closest('ion-radio');
/**
* Our current disabled prop definition causes Stencil to mark it
* as optional. While this is not desired, fixing this behavior
* in Stencil is a significant breaking change, so this effort is
* being de-risked in STENCIL-917. Until then, we compromise
* here by checking for falsy `disabled` values instead of strictly
* checking `disabled === false`.
*/
if (selectedRadio && !selectedRadio.disabled) {
const currentValue = this.value;
const newValue = selectedRadio.value;
if (newValue !== currentValue) {
this.value = newValue;
this.emitValueChange(ev);
} else if (this.allowEmptySelection) {
this.value = undefined;
this.emitValueChange(ev);
}
}
};
@Listen('keydown', { target: 'document' })
onKeydown(ev: KeyboardEvent) {
// We don't want the value to automatically change/emit when the radio group is part of a select interface
// as this will cause the interface to close when navigating through the radio group options
const inSelectInterface = !!this.el.closest('ion-select-popover') || !!this.el.closest('ion-select-modal');
if (ev.target && !this.el.contains(ev.target as HTMLElement)) {
return;
}
// Get all radios inside of the radio group and then
// filter out disabled radios since we need to skip those
const radios = this.getRadios().filter((radio) => !radio.disabled);
// Only move the radio if the current focus is in the radio group
if (ev.target && radios.includes(ev.target as HTMLIonRadioElement)) {
const index = radios.findIndex((radio) => radio === ev.target);
const current = radios[index];
let next;
// If hitting arrow down or arrow right, move to the next radio
// If we're on the last radio, move to the first radio
if (['ArrowDown', 'ArrowRight'].includes(ev.key)) {
next = index === radios.length - 1 ? radios[0] : radios[index + 1];
}
// If hitting arrow up or arrow left, move to the previous radio
// If we're on the first radio, move to the last radio
if (['ArrowUp', 'ArrowLeft'].includes(ev.key)) {
next = index === 0 ? radios[radios.length - 1] : radios[index - 1];
}
if (next && radios.includes(next)) {
next.setFocus(ev);
if (!inSelectInterface) {
this.value = next.value;
this.emitValueChange(ev);
}
}
// Update the radio group value when a user presses the
// space bar on top of a selected radio
if ([' '].includes(ev.key)) {
const previousValue = this.value;
this.value = this.allowEmptySelection && this.value !== undefined ? undefined : current.value;
if (previousValue !== this.value || this.allowEmptySelection) {
/**
* Value change should only be emitted if the value is different,
* such as selecting a new radio with the space bar or if
* the radio group allows for empty selection and the user
* is deselecting a checked radio.
*/
this.emitValueChange(ev);
}
// Prevent browsers from jumping
// to the bottom of the screen
ev.preventDefault();
}
}
}
/** @internal */
@Method()
async setFocus() {
const radioToFocus = this.getRadios().find((r) => r.tabIndex !== -1);
radioToFocus?.setFocus();
}
/**
* Renders the helper text or error text values
*/
private renderHintText() {
const { helperText, errorText, helperTextId, errorTextId, isInvalid } = this;
const hasHintText = !!helperText || !!errorText;
if (!hasHintText) {
return;
}
return (
<div class="radio-group-top">
<div id={helperTextId} class="helper-text" part="supporting-text helper-text" aria-live="polite">
{!isInvalid ? helperText : null}
</div>
<div id={errorTextId} class="error-text" part="supporting-text error-text" role="alert">
{isInvalid ? errorText : null}
</div>
</div>
);
}
private getHintTextId(): string | undefined {
const { helperText, errorText, helperTextId, errorTextId, isInvalid } = this;
if (isInvalid && errorText) {
return errorTextId;
}
if (helperText) {
return helperTextId;
}
return undefined;
}
render() {
const { label, labelId, el, name, value } = this;
const theme = getIonTheme(this);
renderHiddenInput(true, el, name, value, false);
return (
<Host
class={{
[theme]: true,
'in-list': hostContext('ion-list', el),
}}
role="radiogroup"
aria-labelledby={label ? labelId : null}
aria-describedby={this.hintTextId}
aria-invalid={this.isInvalid ? 'true' : undefined}
onClick={this.onClick}
>
{this.renderHintText()}
<slot></slot>
</Host>
);
}
}
let radioGroupIds = 0;