-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
inputbase.ts
223 lines (191 loc) · 4.94 KB
/
inputbase.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module ui/input/inputbase
*/
import View from '../view.js';
import {
FocusTracker,
type Locale,
type ObservableChangeEvent
} from '@ckeditor/ckeditor5-utils';
/**
* The base input view class.
*/
export default abstract class InputBase<TElement extends HTMLInputElement | HTMLTextAreaElement = HTMLInputElement> extends View<TElement> {
/**
* Stores information about the editor UI focus and propagates it so various plugins and components
* are unified as a focus group.
*/
public readonly focusTracker: FocusTracker;
/**
* The value of the input.
*
* @observable
*/
declare public value: string | undefined;
/**
* The `id` attribute of the input (i.e. to pair with a `<label>` element).
*
* @observable
*/
declare public id: string | undefined;
/**
* The `placeholder` attribute of the input.
*
* @observable
*/
declare public placeholder: string | undefined;
/**
* The `tabindex` attribute of the input.
*
* @observable
*/
declare public tabIndex: number | undefined;
/**
* The `aria-label` attribute of the input.
*
* @observable
*/
declare public ariaLabel: string | undefined;
/**
* Controls whether the input view is in read-only mode.
*
* @observable
*/
declare public isReadOnly: boolean;
/**
* Set to `true` when the field has some error. Usually controlled via
* {@link module:ui/labeledinput/labeledinputview~LabeledInputView#errorText}.
*
* @observable
*/
declare public hasError: boolean;
/**
* The `id` of the element describing this field, e.g. when it has
* some error; it helps screen readers read the error text.
*
* @observable
*/
declare public ariaDescribedById: string | undefined;
/**
* An observable flag set to `true` when the input is currently focused by the user.
* Set to `false` otherwise.
*
* @readonly
* @observable
* @default false
*/
declare public isFocused: boolean;
/**
* An observable flag set to `true` when the input contains no text, i.e.
* when {@link #value} is `''`, `null`, or `false`.
*
* @readonly
* @observable
* @default true
*/
declare public isEmpty: boolean;
/**
* @inheritDoc
*/
constructor( locale?: Locale ) {
super( locale );
this.set( 'value', undefined );
this.set( 'id', undefined );
this.set( 'placeholder', undefined );
this.set( 'tabIndex', undefined );
this.set( 'isReadOnly', false );
this.set( 'hasError', false );
this.set( 'ariaDescribedById', undefined );
this.set( 'ariaLabel', undefined );
this.focusTracker = new FocusTracker();
this.bind( 'isFocused' ).to( this.focusTracker );
this.set( 'isEmpty', true );
const bind = this.bindTemplate;
this.setTemplate( {
tag: 'input',
attributes: {
class: [
'ck',
'ck-input',
bind.if( 'isFocused', 'ck-input_focused' ),
bind.if( 'isEmpty', 'ck-input-text_empty' ),
bind.if( 'hasError', 'ck-error' )
],
id: bind.to( 'id' ),
placeholder: bind.to( 'placeholder' ),
tabindex: bind.to( 'tabIndex' ),
readonly: bind.to( 'isReadOnly' ),
'aria-invalid': bind.if( 'hasError', true ),
'aria-describedby': bind.to( 'ariaDescribedById' ),
'aria-label': bind.to( 'ariaLabel' )
},
on: {
input: bind.to( ( ...args ) => {
this.fire( 'input', ...args );
this._updateIsEmpty();
} ),
change: bind.to( this._updateIsEmpty.bind( this ) )
}
} );
}
/**
* @inheritDoc
*/
public override render(): void {
super.render();
this.focusTracker.add( this.element! );
this._setDomElementValue( this.value );
this._updateIsEmpty();
// Bind `this.value` to the DOM element's value.
// We cannot use `value` DOM attribute because removing it on Edge does not clear the DOM element's value property.
this.on<ObservableChangeEvent>( 'change:value', ( evt, name, value ) => {
this._setDomElementValue( value );
this._updateIsEmpty();
} );
}
/**
* @inheritDoc
*/
public override destroy(): void {
super.destroy();
this.focusTracker.destroy();
}
/**
* Moves the focus to the input and selects the value.
*/
public select(): void {
this.element!.select();
}
/**
* Focuses the input.
*/
public focus(): void {
this.element!.focus();
}
/**
* Resets the value of the input
*/
public reset(): void {
this.value = this.element!.value = '';
this._updateIsEmpty();
}
/**
* Updates the {@link #isEmpty} property value on demand.
*/
protected _updateIsEmpty(): void {
this.isEmpty = isInputElementEmpty( this.element! );
}
/**
* Sets the `value` property of the {@link #element DOM element} on demand.
*/
private _setDomElementValue( value: any ) {
this.element!.value = ( !value && value !== 0 ) ? '' : value;
}
}
function isInputElementEmpty( domElement: HTMLInputElement | HTMLTextAreaElement ) {
return !domElement.value;
}