-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract radio-button and radio-group logic to mixins (#6680)
- Loading branch information
1 parent
34652cd
commit c1d2e1f
Showing
10 changed files
with
635 additions
and
536 deletions.
There are no files selected for viewing
This file contains 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 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,50 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2017 - 2023 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import type { Constructor } from '@open-wc/dedupe-mixin'; | ||
import type { ActiveMixinClass } from '@vaadin/a11y-base/src/active-mixin.js'; | ||
import type { DelegateFocusMixinClass } from '@vaadin/a11y-base/src/delegate-focus-mixin.js'; | ||
import type { DisabledMixinClass } from '@vaadin/a11y-base/src/disabled-mixin.js'; | ||
import type { FocusMixinClass } from '@vaadin/a11y-base/src/focus-mixin.js'; | ||
import type { KeyboardMixinClass } from '@vaadin/a11y-base/src/keyboard-mixin.js'; | ||
import type { DelegateStateMixinClass } from '@vaadin/component-base/src/delegate-state-mixin.js'; | ||
import type { CheckedMixinClass } from '@vaadin/field-base/src/checked-mixin.js'; | ||
import type { InputMixinClass } from '@vaadin/field-base/src/input-mixin.js'; | ||
import type { LabelMixinClass } from '@vaadin/field-base/src/label-mixin.js'; | ||
|
||
/** | ||
* Fired when the `checked` property changes. | ||
*/ | ||
export type RadioButtonCheckedChangedEvent = CustomEvent<{ value: boolean }>; | ||
|
||
export interface RadioButtonCustomEventMap { | ||
'checked-changed': RadioButtonCheckedChangedEvent; | ||
} | ||
|
||
export interface RadioButtonEventMap extends HTMLElementEventMap, RadioButtonCustomEventMap {} | ||
|
||
/** | ||
* A mixin providing common radio-button functionality. | ||
*/ | ||
export declare function RadioButtonMixin<T extends Constructor<HTMLElement>>( | ||
base: T, | ||
): Constructor<ActiveMixinClass> & | ||
Constructor<CheckedMixinClass> & | ||
Constructor<DelegateFocusMixinClass> & | ||
Constructor<DelegateStateMixinClass> & | ||
Constructor<DisabledMixinClass> & | ||
Constructor<FocusMixinClass> & | ||
Constructor<InputMixinClass> & | ||
Constructor<KeyboardMixinClass> & | ||
Constructor<LabelMixinClass> & | ||
Constructor<RadioButtonMixinClass> & | ||
T; | ||
|
||
export declare class RadioButtonMixinClass { | ||
/** | ||
* The name of the radio button. | ||
*/ | ||
name: string; | ||
} |
This file contains 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,67 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2017 - 2023 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import { ActiveMixin } from '@vaadin/a11y-base/src/active-mixin.js'; | ||
import { DelegateFocusMixin } from '@vaadin/a11y-base/src/delegate-focus-mixin.js'; | ||
import { CheckedMixin } from '@vaadin/field-base/src/checked-mixin.js'; | ||
import { InputController } from '@vaadin/field-base/src/input-controller.js'; | ||
import { LabelMixin } from '@vaadin/field-base/src/label-mixin.js'; | ||
import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js'; | ||
|
||
/** | ||
* A mixin providing common radio-button functionality. | ||
* | ||
* @polymerMixin | ||
* @mixes ActiveMixin | ||
* @mixes CheckedMixin | ||
* @mixes DelegateFocusMixin | ||
* @mixes LabelMixin | ||
*/ | ||
export const RadioButtonMixin = (superclass) => | ||
class RadioButtonMixinClass extends LabelMixin(CheckedMixin(DelegateFocusMixin(ActiveMixin(superclass)))) { | ||
static get properties() { | ||
return { | ||
/** | ||
* The name of the radio button. | ||
* | ||
* @type {string} | ||
*/ | ||
name: { | ||
type: String, | ||
value: '', | ||
}, | ||
}; | ||
} | ||
|
||
/** @override */ | ||
static get delegateAttrs() { | ||
return [...super.delegateAttrs, 'name']; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
|
||
this._setType('radio'); | ||
|
||
// Set the string "on" as the default value for the radio button following the HTML specification: | ||
// https://html.spec.whatwg.org/multipage/input.html#dom-input-value-default-on | ||
this.value = 'on'; | ||
} | ||
|
||
/** @protected */ | ||
ready() { | ||
super.ready(); | ||
|
||
this.addController( | ||
new InputController(this, (input) => { | ||
this._setInputElement(input); | ||
this._setFocusElement(input); | ||
this.stateTarget = input; | ||
this.ariaTarget = input; | ||
}), | ||
); | ||
this.addController(new LabelledInputController(this.inputElement, this._labelController)); | ||
} | ||
}; |
This file contains 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 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 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,68 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2017 - 2023 Vaadin Ltd. | ||
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ | ||
*/ | ||
import type { Constructor } from '@open-wc/dedupe-mixin'; | ||
import type { DisabledMixinClass } from '@vaadin/a11y-base/src/disabled-mixin.js'; | ||
import type { FocusMixinClass } from '@vaadin/a11y-base/src/focus-mixin.js'; | ||
import type { KeyboardMixinClass } from '@vaadin/a11y-base/src/keyboard-mixin.js'; | ||
import type { ControllerMixinClass } from '@vaadin/component-base/src/controller-mixin.js'; | ||
import type { FieldMixinClass } from '@vaadin/field-base/src/field-mixin.js'; | ||
import type { LabelMixinClass } from '@vaadin/field-base/src/label-mixin.js'; | ||
import type { ValidateMixinClass } from '@vaadin/field-base/src/validate-mixin.js'; | ||
|
||
/** | ||
* Fired when the `invalid` property changes. | ||
*/ | ||
export type RadioGroupInvalidChangedEvent = CustomEvent<{ value: boolean }>; | ||
|
||
/** | ||
* Fired when the `value` property changes. | ||
*/ | ||
export type RadioGroupValueChangedEvent = CustomEvent<{ value: string }>; | ||
|
||
/** | ||
* Fired whenever the field is validated. | ||
*/ | ||
export type RadioGroupValidatedEvent = CustomEvent<{ valid: boolean }>; | ||
|
||
export interface RadioGroupCustomEventMap { | ||
'invalid-changed': RadioGroupInvalidChangedEvent; | ||
|
||
'value-changed': RadioGroupValueChangedEvent; | ||
|
||
validated: RadioGroupValidatedEvent; | ||
} | ||
|
||
export interface RadioGroupEventMap extends HTMLElementEventMap, RadioGroupCustomEventMap {} | ||
|
||
/** | ||
* A mixin providing common radio-group functionality. | ||
*/ | ||
export declare function RadioGroupMixin<T extends Constructor<HTMLElement>>( | ||
base: T, | ||
): Constructor<ControllerMixinClass> & | ||
Constructor<DisabledMixinClass> & | ||
Constructor<FieldMixinClass> & | ||
Constructor<FocusMixinClass> & | ||
Constructor<KeyboardMixinClass> & | ||
Constructor<LabelMixinClass> & | ||
Constructor<RadioGroupMixinClass> & | ||
Constructor<ValidateMixinClass> & | ||
T; | ||
|
||
export declare class RadioGroupMixinClass { | ||
/** | ||
* The value of the radio group. | ||
*/ | ||
value: string | null | undefined; | ||
|
||
/** | ||
* When present, the user cannot modify the value of the radio group. | ||
* The property works similarly to the `disabled` property. | ||
* While the `disabled` property disables all the radio buttons inside the group, | ||
* the `readonly` property disables only unchecked ones. | ||
*/ | ||
readonly: boolean; | ||
} |
Oops, something went wrong.