Skip to content

Commit

Permalink
refactor: move ARIA attributes logic to FieldAriaController (#3048) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen committed Nov 18, 2021
1 parent 9222a72 commit cfb4c69
Show file tree
Hide file tree
Showing 35 changed files with 632 additions and 251 deletions.
9 changes: 0 additions & 9 deletions packages/checkbox-group/src/vaadin-checkbox-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ class CheckboxGroup extends FieldMixin(FocusMixin(DisabledMixin(DirMixin(Themabl
});
}

/**
* @return {string}
* @override
* @protected
*/
get _ariaAttr() {
return 'aria-labelledby';
}

/**
* Override method inherited from `ValidateMixin`
* to validate the value array.
Expand Down
2 changes: 1 addition & 1 deletion packages/checkbox-group/test/checkbox-group.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ describe('vaadin-checkbox-group', () => {
let error, helper, label;

beforeEach(() => {
group = fixtureSync('<vaadin-checkbox-group helper-text="Choose one"></vaadin-checkbox-group>');
group = fixtureSync('<vaadin-checkbox-group helper-text="Choose one" label="Label"></vaadin-checkbox-group>');
error = group.querySelector('[slot=error-message]');
helper = group.querySelector('[slot=helper]');
label = group.querySelector('[slot=label]');
Expand Down
4 changes: 2 additions & 2 deletions packages/checkbox/src/vaadin-checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { ActiveMixin } from '@vaadin/component-base/src/active-mixin.js';
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { AriaLabelController } from '@vaadin/field-base/src/aria-label-controller.js';
import { CheckedMixin } from '@vaadin/field-base/src/checked-mixin.js';
import { DelegateFocusMixin } from '@vaadin/field-base/src/delegate-focus-mixin.js';
import { InputController } from '@vaadin/field-base/src/input-controller.js';
import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js';
import { SlotLabelMixin } from '@vaadin/field-base/src/slot-label-mixin.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';

Expand Down Expand Up @@ -176,7 +176,7 @@ class Checkbox extends SlotLabelMixin(
this.stateTarget = input;
})
);
this.addController(new AriaLabelController(this, this.inputElement, this._labelNode));
this.addController(new LabelledInputController(this.inputElement, this._labelNode));
}

/**
Expand Down
8 changes: 3 additions & 5 deletions packages/combo-box/src/vaadin-combo-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import '@vaadin/input-container/src/vaadin-input-container.js';
import './vaadin-combo-box-dropdown.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { AriaLabelController } from '@vaadin/field-base/src/aria-label-controller.js';
import { InputControlMixin } from '@vaadin/field-base/src/input-control-mixin.js';
import { InputController } from '@vaadin/field-base/src/input-controller.js';
import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js';
import { PatternMixin } from '@vaadin/field-base/src/pattern-mixin.js';
import { inputFieldShared } from '@vaadin/field-base/src/styles/input-field-shared-styles.js';
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
Expand Down Expand Up @@ -147,7 +146,6 @@ registerStyles('vaadin-combo-box', inputFieldShared, { moduleId: 'vaadin-combo-b
* @fires {CustomEvent} value-changed - Fired when the `value` property changes.
*
* @extends HTMLElement
* @mixes ControllerMixin
* @mixes ElementMixin
* @mixes ThemableMixin
* @mixes InputControlMixin
Expand All @@ -156,7 +154,7 @@ registerStyles('vaadin-combo-box', inputFieldShared, { moduleId: 'vaadin-combo-b
* @mixes ComboBoxMixin
*/
class ComboBox extends ComboBoxDataProviderMixin(
ComboBoxMixin(PatternMixin(InputControlMixin(ThemableMixin(ElementMixin(ControllerMixin(PolymerElement))))))
ComboBoxMixin(PatternMixin(InputControlMixin(ThemableMixin(ElementMixin(PolymerElement)))))
) {
static get is() {
return 'vaadin-combo-box';
Expand Down Expand Up @@ -244,7 +242,7 @@ class ComboBox extends ComboBoxDataProviderMixin(
this.ariaTarget = input;
})
);
this.addController(new AriaLabelController(this, this.inputElement, this._labelNode));
this.addController(new LabelledInputController(this.inputElement, this._labelNode));
this._positionTarget = this.shadowRoot.querySelector('[part="input-field"]');
this._toggleElement = this.$.toggleButton;
}
Expand Down
8 changes: 0 additions & 8 deletions packages/custom-field/src/vaadin-custom-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,6 @@ class CustomField extends FieldMixin(FocusMixin(ThemableMixin(ElementMixin(Polym
};
}

/**
* Attribute used by `FieldMixin` to set accessible name.
* @protected
*/
get _ariaAttr() {
return 'aria-labelledby';
}

/** @protected */
connectedCallback() {
super.connectedCallback();
Expand Down
5 changes: 1 addition & 4 deletions packages/date-picker/src/vaadin-date-picker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright (c) 2021 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { InputControlMixin } from '@vaadin/field-base/src/input-control-mixin.js';
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
Expand Down Expand Up @@ -126,9 +125,7 @@ export interface DatePickerEventMap extends HTMLElementEventMap, DatePickerCusto
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
* @fires {CustomEvent} value-changed - Fired when the `value` property changes.
*/
declare class DatePicker extends DatePickerMixin(
InputControlMixin(ThemableMixin(ElementMixin(ControllerMixin(HTMLElement))))
) {
declare class DatePicker extends DatePickerMixin(InputControlMixin(ThemableMixin(ElementMixin(HTMLElement)))) {
addEventListener<K extends keyof DatePickerEventMap>(
type: K,
listener: (this: DatePicker, ev: DatePickerEventMap[K]) => void,
Expand Down
8 changes: 3 additions & 5 deletions packages/date-picker/src/vaadin-date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import './vaadin-date-picker-overlay.js';
import './vaadin-date-picker-overlay-content.js';
import { GestureEventListeners } from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { AriaLabelController } from '@vaadin/field-base/src/aria-label-controller.js';
import { InputControlMixin } from '@vaadin/field-base/src/input-control-mixin.js';
import { InputController } from '@vaadin/field-base/src/input-controller.js';
import { LabelledInputController } from '@vaadin/field-base/src/labelled-input-controller.js';
import { inputFieldShared } from '@vaadin/field-base/src/styles/input-field-shared-styles.js';
import { registerStyles, ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
import { DatePickerMixin } from './vaadin-date-picker-mixin.js';
Expand Down Expand Up @@ -113,13 +112,12 @@ registerStyles('vaadin-date-picker', [inputFieldShared, datePickerStyles], { mod
* @fires {CustomEvent} value-changed - Fired when the `value` property changes.
*
* @extends HTMLElement
* @mixes ControllerMixin
* @mixes ElementMixin
* @mixes ThemableMixin
* @mixes InputControlMixin
*/
class DatePicker extends DatePickerMixin(
InputControlMixin(GestureEventListeners(ThemableMixin(ElementMixin(ControllerMixin(PolymerElement)))))
InputControlMixin(GestureEventListeners(ThemableMixin(ElementMixin(PolymerElement))))
) {
static get is() {
return 'vaadin-date-picker';
Expand Down Expand Up @@ -214,7 +212,7 @@ class DatePicker extends DatePickerMixin(
this.ariaTarget = input;
})
);
this.addController(new AriaLabelController(this, this.inputElement, this._labelNode));
this.addController(new LabelledInputController(this.inputElement, this._labelNode));
}

/** @private */
Expand Down
8 changes: 0 additions & 8 deletions packages/date-time-picker/src/vaadin-date-time-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,6 @@ class DateTimePicker extends FieldMixin(SlotMixin(DisabledMixin(ThemableMixin(El
this.ariaTarget = this;
}

/**
* Attribute used by `FieldMixin` to set accessible name.
* @protected
*/
get _ariaAttr() {
return 'aria-labelledby';
}

/** @private */
__filterElements(node) {
return node.nodeType === Node.ELEMENT_NODE;
Expand Down
3 changes: 2 additions & 1 deletion packages/field-base/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export { AriaLabelController } from './src/aria-label-controller.js';
export { CheckedMixin } from './src/checked-mixin.js';
export { DelegateFocusMixin } from './src/delegate-focus-mixin.js';
export { DelegateStateMixin } from './src/delegate-state-mixin.js';
export { FieldAriaController } from './src/field-aria-controller.js';
export { FieldMixin } from './src/field-mixin.js';
export { InputController } from './src/input-controller.js';
export { InputControlMixin } from './src/input-control-mixin.js';
export { InputFieldMixin } from './src/input-field-mixin.js';
export { InputMixin } from './src/input-mixin.js';
export { LabelledInputController } from './src/labelled-input-controller.js';
export { LabelMixin } from './src/label-mixin.js';
export { PatternMixin } from './src/pattern-mixin.js';
export { ShadowFocusMixin } from './src/shadow-focus-mixin.js';
Expand Down
3 changes: 2 additions & 1 deletion packages/field-base/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export { AriaLabelController } from './src/aria-label-controller.js';
export { CheckedMixin } from './src/checked-mixin.js';
export { DelegateFocusMixin } from './src/delegate-focus-mixin.js';
export { DelegateStateMixin } from './src/delegate-state-mixin.js';
export { FieldAriaController } from './src/field-aria-controller.js';
export { FieldMixin } from './src/field-mixin.js';
export { InputController } from './src/input-controller.js';
export { InputControlMixin } from './src/input-control-mixin.js';
export { InputFieldMixin } from './src/input-field-mixin.js';
export { InputMixin } from './src/input-mixin.js';
export { LabelledInputController } from './src/labelled-input-controller.js';
export { LabelMixin } from './src/label-mixin.js';
export { PatternMixin } from './src/pattern-mixin.js';
export { ShadowFocusMixin } from './src/shadow-focus-mixin.js';
Expand Down
56 changes: 56 additions & 0 deletions packages/field-base/src/field-aria-controller.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license
* Copyright (c) 2021 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/

/**
* A controller for managing ARIA attributes for a field element:
* either the component itself or slotted `<input>` element.
*/
export class FieldAriaController {
constructor(host: HTMLElement);

/**
* The controller host element.
*/
host: HTMLElement;

/**
* Sets a target element to which ARIA attributes are added.
*/
setTarget(target: HTMLElement): void;

/**
* Toggles the `aria-required` attribute on the target element
* if the target is the host component (e.g. a field group).
* Otherwise, it does nothing.
*/
setRequired(required: boolean): void;

/**
* Links the target element with a slotted label element
* via the target's attribute `aria-labelledby`.
*
* To unlink the previous slotted label element, pass `null` as `labelId`.
*/
setLabelId(labelId: string | null): void;

/**
* Links the target element with a slotted error element via the target's attribute:
* - `aria-labelledby` if the target is the host component (e.g a field group).
* - `aria-describedby` otherwise.
*
* To unlink the previous slotted error element, pass `null` as `errorId`.
*/
setErrorId(errorId: string | null): void;

/**
* Links the target element with a slotted helper element via the target's attribute:
* - `aria-labelledby` if the target is the host component (e.g a field group).
* - `aria-describedby` otherwise.
*
* To unlink the previous slotted helper element, pass `null` as `helperId`.
*/
setHelperId(helperId: string | null): void;
}

0 comments on commit cfb4c69

Please sign in to comment.