UNPKG

9.05 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { UniqueSelectionDispatcher } from '@angular/cdk/collections';
9import { Directive, inject } from '@angular/core';
10import { CdkMenuItemSelectable } from './menu-item-selectable';
11import { CdkMenuItem } from './menu-item';
12import * as i0 from "@angular/core";
13/** Counter used to set a unique id and name for a selectable item */
14let nextId = 0;
15/**
16 * A directive providing behavior for the "menuitemradio" ARIA role, which behaves similarly to
17 * a conventional radio-button. Any sibling `CdkMenuItemRadio` instances within the same `CdkMenu`
18 * or `CdkMenuGroup` comprise a radio group with unique selection enforced.
19 */
20class CdkMenuItemRadio extends CdkMenuItemSelectable {
21 constructor() {
22 super();
23 /** The unique selection dispatcher for this radio's `CdkMenuGroup`. */
24 this._selectionDispatcher = inject(UniqueSelectionDispatcher);
25 /** An ID to identify this radio item to the `UniqueSelectionDispatcher`. */
26 this._id = `${nextId++}`;
27 this._registerDispatcherListener();
28 }
29 ngOnDestroy() {
30 super.ngOnDestroy();
31 this._removeDispatcherListener();
32 }
33 /**
34 * Toggles the checked state of the radio-button.
35 * @param options Options the configure how the item is triggered
36 * - keepOpen: specifies that the menu should be kept open after triggering the item.
37 */
38 trigger(options) {
39 super.trigger(options);
40 if (!this.disabled) {
41 this._selectionDispatcher.notify(this._id, '');
42 }
43 }
44 /** Configure the unique selection dispatcher listener in order to toggle the checked state */
45 _registerDispatcherListener() {
46 this._removeDispatcherListener = this._selectionDispatcher.listen((id) => {
47 this.checked = this._id === id;
48 });
49 }
50 static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkMenuItemRadio, deps: [], target: i0.ɵɵFactoryTarget.Directive }); }
51 static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.0.0", type: CdkMenuItemRadio, isStandalone: true, selector: "[cdkMenuItemRadio]", host: { attributes: { "role": "menuitemradio" }, properties: { "class.cdk-menu-item-radio": "true" } }, providers: [
52 { provide: CdkMenuItemSelectable, useExisting: CdkMenuItemRadio },
53 { provide: CdkMenuItem, useExisting: CdkMenuItemSelectable },
54 ], exportAs: ["cdkMenuItemRadio"], usesInheritance: true, ngImport: i0 }); }
55}
56export { CdkMenuItemRadio };
57i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.0", ngImport: i0, type: CdkMenuItemRadio, decorators: [{
58 type: Directive,
59 args: [{
60 selector: '[cdkMenuItemRadio]',
61 exportAs: 'cdkMenuItemRadio',
62 standalone: true,
63 host: {
64 'role': 'menuitemradio',
65 '[class.cdk-menu-item-radio]': 'true',
66 },
67 providers: [
68 { provide: CdkMenuItemSelectable, useExisting: CdkMenuItemRadio },
69 { provide: CdkMenuItem, useExisting: CdkMenuItemSelectable },
70 ],
71 }]
72 }], ctorParameters: function () { return []; } });
73//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVudS1pdGVtLXJhZGlvLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2Nkay9tZW51L21lbnUtaXRlbS1yYWRpby50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxPQUFPLEVBQUMseUJBQXlCLEVBQUMsTUFBTSwwQkFBMEIsQ0FBQztBQUNuRSxPQUFPLEVBQUMsU0FBUyxFQUFFLE1BQU0sRUFBWSxNQUFNLGVBQWUsQ0FBQztBQUMzRCxPQUFPLEVBQUMscUJBQXFCLEVBQUMsTUFBTSx3QkFBd0IsQ0FBQztBQUM3RCxPQUFPLEVBQUMsV0FBVyxFQUFDLE1BQU0sYUFBYSxDQUFDOztBQUV4QyxxRUFBcUU7QUFDckUsSUFBSSxNQUFNLEdBQUcsQ0FBQyxDQUFDO0FBRWY7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