-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinput-base.ts
More file actions
201 lines (172 loc) · 5.56 KB
/
input-base.ts
File metadata and controls
201 lines (172 loc) · 5.56 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { html, LitElement, nothing, type TemplateResult } from 'lit';
import { property, query, queryAssignedElements } from 'lit/decorators.js';
import { getThemeController, themes } from '../../theming/theming-decorator.js';
import { blazorDeepImport } from '../common/decorators/blazorDeepImport.js';
import { shadowOptions } from '../common/decorators/shadow-options.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { FormAssociatedRequiredMixin } from '../common/mixins/forms/associated-required.js';
import { partMap } from '../common/part-map.js';
import { createCounter } from '../common/util.js';
import type { RangeTextSelectMode, SelectionRangeDirection } from '../types.js';
import IgcValidationContainerComponent from '../validation-container/validation-container.js';
import { styles } from './themes/input.base.css.js';
import { styles as shared } from './themes/shared/input.common.css.js';
import { all } from './themes/themes.js';
export interface IgcInputComponentEventMap {
/* alternateName: inputOcurred */
igcInput: CustomEvent<string>;
/* blazorSuppress */
igcChange: CustomEvent<string>;
// For analyzer meta only:
/* skipWCPrefix */
focus: FocusEvent;
/* skipWCPrefix */
blur: FocusEvent;
}
@blazorDeepImport
@themes(all, { exposeController: true })
@shadowOptions({ delegatesFocus: true })
export abstract class IgcInputBaseComponent extends FormAssociatedRequiredMixin(
EventEmitterMixin<IgcInputComponentEventMap, Constructor<LitElement>>(
LitElement
)
) {
public static styles = [styles, shared];
private static readonly increment = createCounter();
protected inputId = `input-${IgcInputBaseComponent.increment()}`;
/* blazorSuppress */
/** The value attribute of the control. */
public abstract value: string | Date | null;
@query('input')
protected input!: HTMLInputElement;
@queryAssignedElements({ slot: 'helper-text' })
protected _helperText!: Array<HTMLElement>;
@queryAssignedElements({
slot: 'prefix',
selector: '[slot="prefix"]:not([hidden])',
})
protected prefixes!: Array<HTMLElement>;
@queryAssignedElements({
slot: 'suffix',
selector: '[slot="suffix"]:not([hidden])',
})
protected suffixes!: Array<HTMLElement>;
protected get _isMaterial() {
return getThemeController(this)?.theme === 'material';
}
/**
* Whether the control will have outlined appearance.
* @attr
*/
@property({ reflect: true, type: Boolean })
public outlined = false;
/**
* Makes the control a readonly field.
* @attr readonly
*/
@property({ type: Boolean, reflect: true, attribute: 'readonly' })
public readOnly = false;
/**
* The placeholder attribute of the control.
* @attr
*/
@property()
public placeholder!: string;
/**
* The label for the control.
* @attr
*/
@property()
public label!: string;
protected override createRenderRoot() {
const root = super.createRenderRoot();
root.addEventListener('slotchange', () => this.requestUpdate());
return root;
}
/* alternateName: focusComponent */
/** Sets focus on the control. */
public override focus(options?: FocusOptions) {
this.input.focus(options);
}
/* alternateName: blurComponent */
/** Removes focus from the control. */
public override blur() {
this.input.blur();
}
protected abstract renderInput(): TemplateResult;
protected renderValidatorContainer(): TemplateResult {
return IgcValidationContainerComponent.create(this);
}
protected resolvePartNames(base: string) {
return {
[base]: true,
prefixed: this.prefixes.length > 0,
suffixed: this.suffixes.length > 0,
filled: !!this.value,
};
}
protected renderFileParts(): TemplateResult | typeof nothing {
return nothing;
}
/** Sets the text selection range of the control */
public setSelectionRange(
start: number,
end: number,
direction: SelectionRangeDirection = 'none'
) {
this.input.setSelectionRange(start, end, direction);
}
/** Replaces the selected text in the input. */
public setRangeText(
replacement: string,
start: number,
end: number,
selectMode: RangeTextSelectMode = 'preserve'
) {
this.input.setRangeText(replacement, start, end, selectMode);
}
private renderPrefix() {
return html`<div part="prefix">
<slot name="prefix"></slot>
</div>`;
}
private renderSuffix() {
return html`<div part="suffix">
<slot name="suffix"></slot>
</div>`;
}
private renderLabel() {
return this.label
? html`<label part="label" for=${this.inputId}> ${this.label} </label>`
: nothing;
}
private renderMaterial() {
return html`
<div
part=${partMap({
...this.resolvePartNames('container'),
labelled: !!this.label,
})}
>
<div part="start">${this.renderPrefix()}</div>
${this.renderInput()} ${this.renderFileParts()}
<div part="notch">${this.renderLabel()}</div>
<div part="filler"></div>
<div part="end">${this.renderSuffix()}</div>
</div>
${this.renderValidatorContainer()}
`;
}
private renderStandard() {
return html`${this.renderLabel()}
<div part=${partMap(this.resolvePartNames('container'))}>
${this.renderPrefix()} ${this.renderFileParts()} ${this.renderInput()}
${this.renderSuffix()}
</div>
${this.renderValidatorContainer()}`;
}
protected override render() {
return this._isMaterial ? this.renderMaterial() : this.renderStandard();
}
}