-
Notifications
You must be signed in to change notification settings - Fork 197
/
FieldLabel.ts
152 lines (133 loc) · 4.82 KB
/
FieldLabel.ts
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
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import {
CSSResultArray,
html,
PropertyValues,
SizedMixin,
SpectrumElement,
TemplateResult,
} from '@spectrum-web-components/base';
import {
property,
query,
} from '@spectrum-web-components/base/src/decorators.js';
import type { Focusable } from '@spectrum-web-components/shared';
import '@spectrum-web-components/icons-ui/icons/sp-icon-asterisk100.js';
import asteriskIconStyles from '@spectrum-web-components/icon/src/spectrum-icon-asterisk.css.js';
import styles from './field-label.css.js';
type AcceptsFocusVisisble = HTMLElement & { forceFocusVisible?(): void };
/**
* @element sp-field-label
*
* @slot - text content of the label
*/
export class FieldLabel extends SizedMixin(SpectrumElement) {
public static get styles(): CSSResultArray {
return [styles, asteriskIconStyles];
}
/**
* @private
*/
static instanceCount = 0;
@property({ type: Boolean, reflect: true })
public disabled = false;
@property({ type: String })
public id = '';
@property({ type: String })
public for = '';
@property({ type: Boolean, reflect: true })
public required = false;
@query('slot')
public slotEl!: HTMLSlotElement;
@property({ type: String, reflect: true, attribute: 'side-aligned' })
public sideAligned?: 'start' | 'end';
private target?: HTMLElement;
private handleClick(event: Event): void {
if (!this.target || this.disabled || event.defaultPrevented) return;
this.target.focus();
const parent = this.getRootNode() as ShadowRoot;
const target = this.target as AcceptsFocusVisisble;
const targetParent = target.getRootNode() as ShadowRoot;
const targetHost = targetParent.host as AcceptsFocusVisisble;
if (targetParent === parent && target.forceFocusVisible) {
target.forceFocusVisible();
} else if (targetHost && targetHost.forceFocusVisible) {
targetHost.forceFocusVisible();
}
}
private async manageFor(): Promise<void> {
if (!this.for) {
return;
}
const parent = this.getRootNode() as HTMLElement;
const target = parent.querySelector(`#${this.for}`) as Focusable;
if (!target) {
return;
}
if (target.localName.search('-') > 0) {
await customElements.whenDefined(target.localName);
}
if (typeof target.updateComplete !== 'undefined') {
await target.updateComplete;
}
this.target = target.focusElement || target;
if (this.target) {
const targetParent = this.target.getRootNode() as HTMLElement;
if (targetParent === parent) {
this.target.setAttribute('aria-labelledby', this.id);
} else {
this.target.setAttribute('aria-label', this.labelText);
}
}
return Promise.resolve();
}
private get labelText(): string {
const assignedNodes = this.slotEl.assignedNodes({ flatten: true });
if (!assignedNodes.length) {
return '';
}
const labelText = assignedNodes.map((node) =>
(node.textContent || /* c8 ignore next */ '').trim()
);
return labelText.join(' ');
}
protected render(): TemplateResult {
return html`
<label>
<slot @slotchange=${this.manageFor}></slot>
${this.required
? html`
<sp-icon-asterisk100
class="required-icon spectrum-UIIcon-Asterisk100"
></sp-icon-asterisk100>
`
: html``}
</label>
`;
}
protected firstUpdated(changes: PropertyValues): void {
super.firstUpdated(changes);
if (!this.hasAttribute('id')) {
this.setAttribute(
'id',
`${this.tagName.toLowerCase()}-${FieldLabel.instanceCount++}`
);
}
this.addEventListener('click', this.handleClick);
}
protected updated(changes: PropertyValues): void {
super.updated(changes);
if (changes.has('for') || changes.has('id')) {
this.manageFor();
}
}
}