-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(cdk-experimental/radio): create radio group and button directives #31050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
128ec6e
feat(cdk-experimental/radio): create radio group and button directives
wagnermaciel e0c464b
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel 7d6a9b4
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel 16027e2
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel f3ed60b
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel e849627
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel 399baa3
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel acab43c
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel e40b4f2
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel 7668670
fixup! feat(cdk-experimental/radio): create radio group and button di…
wagnermaciel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(cdk-experimental/radio): create radio group and button directives
- Loading branch information
commit 128ec6e875e2ca7a94c211d3e41c79a9297d8f1c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
load("//tools:defaults.bzl", "ng_project") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ng_project( | ||
name = "radio", | ||
srcs = glob( | ||
["**/*.ts"], | ||
exclude = ["**/*.spec.ts"], | ||
), | ||
deps = [ | ||
"//:node_modules/@angular/core", | ||
"//src/cdk-experimental/ui-patterns", | ||
"//src/cdk/a11y", | ||
"//src/cdk/bidi", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
export * from './public-api'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
export {CdkRadioGroup, CdkRadioButton} from './radio'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { | ||
AfterViewInit, | ||
booleanAttribute, | ||
computed, | ||
contentChildren, | ||
Directive, | ||
effect, | ||
ElementRef, | ||
inject, | ||
input, | ||
linkedSignal, | ||
model, | ||
signal, | ||
} from '@angular/core'; | ||
import {RadioButtonPattern, RadioGroupPattern} from '../ui-patterns'; | ||
import {Directionality} from '@angular/cdk/bidi'; | ||
import {toSignal} from '@angular/core/rxjs-interop'; | ||
import {_IdGenerator} from '@angular/cdk/a11y'; | ||
|
||
/** | ||
* A radio button group container. | ||
* | ||
* Radio groups are used to group multiple radio buttons or radio group labels so they function as | ||
* a single form control. The CdkRadioGroup is meant to be used in conjunction with CdkRadioButton | ||
* as follows: | ||
* | ||
* ```html | ||
* <div cdkRadioGroup> | ||
* <label cdkRadioButton value="1">Option 1</label> | ||
* <label cdkRadioButton value="2">Option 2</label> | ||
* <label cdkRadioButton value="3">Option 3</label> | ||
* </div> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: '[cdkRadioGroup]', | ||
exportAs: 'cdkRadioGroup', | ||
host: { | ||
'role': 'radiogroup', | ||
'class': 'cdk-radio-group', | ||
'[attr.tabindex]': 'pattern.tabindex()', | ||
'[attr.aria-readonly]': 'pattern.readonly()', | ||
'[attr.aria-disabled]': 'pattern.disabled()', | ||
'[attr.aria-orientation]': 'pattern.orientation()', | ||
'[attr.aria-activedescendant]': 'pattern.activedescendant()', | ||
'(keydown)': 'pattern.onKeydown($event)', | ||
'(pointerdown)': 'pattern.onPointerdown($event)', | ||
'(focusin)': 'onFocus()', | ||
}, | ||
}) | ||
export class CdkRadioGroup<V> implements AfterViewInit { | ||
/** The directionality (LTR / RTL) context for the application (or a subtree of it). */ | ||
private readonly _directionality = inject(Directionality); | ||
|
||
/** The CdkRadioButtons nested inside of the CdkRadioGroup. */ | ||
private readonly _cdkRadioButtons = contentChildren(CdkRadioButton, {descendants: true}); | ||
|
||
/** A signal wrapper for directionality. */ | ||
protected textDirection = toSignal(this._directionality.change, { | ||
wagnermaciel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
initialValue: this._directionality.value, | ||
}); | ||
|
||
/** The RadioButton UIPatterns of the child CdkRadioButtons. */ | ||
protected items = computed(() => this._cdkRadioButtons().map(radio => radio.pattern)); | ||
|
||
/** Whether the radio group is vertically or horizontally oriented. */ | ||
orientation = input<'vertical' | 'horizontal'>('vertical'); | ||
|
||
/** Whether focus should wrap when navigating. */ | ||
wrap = input(false, {transform: booleanAttribute}); // Radio groups typically don't wrap | ||
wagnermaciel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** Whether disabled items in the group should be skipped when navigating. */ | ||
skipDisabled = input(true, {transform: booleanAttribute}); | ||
|
||
/** The focus strategy used by the radio group. */ | ||
focusMode = input<'roving' | 'activedescendant'>('roving'); | ||
|
||
/** Whether the radio group is disabled. */ | ||
disabled = input(false, {transform: booleanAttribute}); | ||
|
||
/** Whether the radio group is readonly. */ | ||
readonly = input(false, {transform: booleanAttribute}); | ||
|
||
/** The value of the currently selected radio button. */ | ||
value = model<V | null>(null); | ||
|
||
/** The internal selection state for the radio group. */ | ||
private readonly _value = linkedSignal(() => (this.value() ? [this.value()!] : [])); | ||
wagnermaciel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** The current index that has been navigated to. */ | ||
activeIndex = model<number>(0); | ||
|
||
/** The RadioGroup UIPattern. */ | ||
pattern: RadioGroupPattern<V> = new RadioGroupPattern<V>({ | ||
...this, | ||
items: this.items, | ||
value: this._value, | ||
textDirection: this.textDirection, | ||
}); | ||
|
||
/** Whether the radio group has received focus yet. */ | ||
private _hasFocused = signal(false); | ||
|
||
/** Whether the radio buttons in the group have been initialized. */ | ||
private _isViewInitialized = signal(false); | ||
|
||
constructor() { | ||
effect(() => { | ||
wagnermaciel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this._isViewInitialized() && !this._hasFocused()) { | ||
this.pattern.setDefaultState(); | ||
} | ||
}); | ||
} | ||
|
||
ngAfterViewInit() { | ||
this._isViewInitialized.set(true); | ||
} | ||
|
||
onFocus() { | ||
this._hasFocused.set(true); | ||
} | ||
} | ||
|
||
/** A selectable radio button in a CdkRadioGroup. */ | ||
@Directive({ | ||
selector: '[cdkRadioButton]', | ||
exportAs: 'cdkRadioButton', | ||
host: { | ||
'role': 'radio', | ||
'class': 'cdk-radio-button', | ||
'[class.cdk-active]': 'pattern.active()', | ||
'[attr.tabindex]': 'pattern.tabindex()', | ||
'[attr.aria-checked]': 'pattern.selected()', | ||
'[attr.aria-disabled]': 'pattern.disabled()', | ||
}, | ||
}) | ||
export class CdkRadioButton<V> { | ||
/** A reference to the radio button element. */ | ||
private readonly _elementRef = inject(ElementRef); | ||
|
||
/** The parent CdkRadioGroup. */ | ||
private readonly _cdkRadioGroup = inject(CdkRadioGroup); | ||
|
||
/** A unique identifier for the radio button. */ | ||
private readonly _generatedId = inject(_IdGenerator).getId('cdk-radio-button-'); | ||
|
||
/** A unique identifier for the radio button. */ | ||
protected id = computed(() => this._generatedId); | ||
|
||
/** The value associated with the radio button. */ | ||
protected value = input.required<V>(); | ||
|
||
/** The parent RadioGroup UIPattern. */ | ||
protected group = computed(() => this._cdkRadioGroup.pattern); | ||
|
||
/** A reference to the radio button element to be focused on navigation. */ | ||
protected element = computed(() => this._elementRef.nativeElement); | ||
|
||
/** Whether the radio button is disabled. */ | ||
disabled = input(false, {transform: booleanAttribute}); | ||
|
||
/** The RadioButton UIPattern. */ | ||
pattern = new RadioButtonPattern<V>({ | ||
...this, | ||
id: this.id, | ||
value: this.value, | ||
group: this.group, | ||
element: this.element, | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.