-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathcomponent.ts
More file actions
1700 lines (1337 loc) · 46.4 KB
/
component.ts
File metadata and controls
1700 lines (1337 loc) · 46.4 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { View } from '@ember/-internals/glimmer';
import {
descriptorForProperty,
get,
nativeDescDecorator,
PROPERTY_DID_CHANGE,
} from '@ember/-internals/metal';
import type { PropertyDidChange } from '@ember/-internals/metal';
import { getOwner } from '@ember/-internals/owner';
import { TargetActionSupport } from '@ember/-internals/runtime';
import type { ViewStates } from '@ember/-internals/views';
import {
ActionSupport,
addChildView,
CoreView,
EventDispatcher,
getChildViews,
getViewElement,
} from '@ember/-internals/views';
import { guidFor } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import type { Environment, Template, TemplateFactory } from '@glimmer/interfaces';
import { setInternalComponentManager } from '@glimmer/manager';
import { isUpdatableRef, updateRef } from '@glimmer/reference';
import { normalizeProperty } from '@glimmer/runtime';
import type { DirtyableTag } from '@glimmer/validator';
import { createTag, dirtyTag } from '@glimmer/validator';
import type { SimpleElement } from '@simple-dom/interface';
import {
ARGS,
BOUNDS,
CURLY_COMPONENT_MANAGER,
DIRTY_TAG,
IS_DISPATCHING_ATTRS,
} from './component-managers/curly';
import { hasDOM } from '@ember/-internals/browser-environment';
// Keep track of which component classes have already been processed for lazy event setup.
let lazyEventsProcessed = new WeakMap<EventDispatcher, WeakSet<object>>();
const EMPTY_ARRAY = Object.freeze([]);
/**
Determines if the element matches the specified selector.
@private
@method matches
@param {DOMElement} el
@param {String} selector
*/
const elMatches: typeof Element.prototype.matches | undefined =
typeof Element !== 'undefined' ? Element.prototype.matches : undefined;
function matches(el: Element, selector: string): boolean {
assert('cannot call `matches` in fastboot mode', elMatches !== undefined);
return elMatches.call(el, selector);
}
/**
@module @ember/component
*/
interface ComponentMethods {
// Overrideable methods are defined here since you can't `declare` a method in a class
/**
Called when the attributes passed into the component have been updated.
Called both during the initial render of a container and during a rerender.
Can be used in place of an observer; code placed here will be executed
every time any attribute updates.
@method didReceiveAttrs
@public
@since 1.13.0
*/
didReceiveAttrs(): void;
/**
Called when the attributes passed into the component have been updated.
Called both during the initial render of a container and during a rerender.
Can be used in place of an observer; code placed here will be executed
every time any attribute updates.
@event didReceiveAttrs
@public
@since 1.13.0
*/
/**
Called after a component has been rendered, both on initial render and
in subsequent rerenders.
@method didRender
@public
@since 1.13.0
*/
didRender(): void;
/**
Called after a component has been rendered, both on initial render and
in subsequent rerenders.
@event didRender
@public
@since 1.13.0
*/
/**
Called before a component has been rendered, both on initial render and
in subsequent rerenders.
@method willRender
@public
@since 1.13.0
*/
willRender(): void;
/**
Called before a component has been rendered, both on initial render and
in subsequent rerenders.
@event willRender
@public
@since 1.13.0
*/
/**
Called when the attributes passed into the component have been changed.
Called only during a rerender, not during an initial render.
@method didUpdateAttrs
@public
@since 1.13.0
*/
didUpdateAttrs(): void;
/**
Called when the attributes passed into the component have been changed.
Called only during a rerender, not during an initial render.
@event didUpdateAttrs
@public
@since 1.13.0
*/
/**
Called when the component is about to update and rerender itself.
Called only during a rerender, not during an initial render.
@method willUpdate
@public
@since 1.13.0
*/
willUpdate(): void;
/**
Called when the component is about to update and rerender itself.
Called only during a rerender, not during an initial render.
@event willUpdate
@public
@since 1.13.0
*/
/**
Called when the component has updated and rerendered itself.
Called only during a rerender, not during an initial render.
@method didUpdate
@public
@since 1.13.0
*/
didUpdate(): void;
/**
Called when the component has updated and rerendered itself.
Called only during a rerender, not during an initial render.
@event didUpdate
@public
@since 1.13.0
*/
/**
The HTML `id` of the component's element in the DOM. You can provide this
value yourself but it must be unique (just as in HTML):
```handlebars
{{my-component elementId="a-really-cool-id"}}
```
```handlebars
<MyComponent @elementId="a-really-cool-id" />
```
If not manually set a default value will be provided by the framework.
Once rendered an element's `elementId` is considered immutable and you
should never change it. If you need to compute a dynamic value for the
`elementId`, you should do this when the component or element is being
instantiated:
```javascript
export default class extends Component {
init() {
super.init(...arguments);
var index = this.get('index');
this.set('elementId', `component-id${index}`);
}
}
```
@property elementId
@type String
@public
*/
layoutName?: string;
}
// A zero-runtime-overhead private symbol to use in branding the component to
// preserve its type parameter.
declare const SIGNATURE: unique symbol;
/**
A component is a reusable UI element that consists of a `.hbs` template and an
optional JavaScript class that defines its behavior. For example, someone
might make a `button` in the template and handle the click behavior in the
JavaScript file that shares the same name as the template.
Components are broken down into two categories:
- Components _without_ JavaScript, that are based only on a template. These
are called Template-only or TO components.
- Components _with_ JavaScript, which consist of a template and a backing
class.
Ember ships with two types of JavaScript classes for components:
1. Glimmer components, imported from `@glimmer/component`, which are the
default component's for Ember Octane (3.15) and more recent editions.
2. Classic components, imported from `@ember/component`, which were the
default for older editions of Ember (pre 3.15).
Below is the documentation for Classic components. If you are looking for the
API documentation for Template-only or Glimmer components, it is [available
here](/ember/release/modules/@glimmer%2Fcomponent).
## Defining a Classic Component
If you want to customize the component in order to handle events, transform
arguments or maintain internal state, you implement a subclass of `Component`.
One example is to add computed properties to your component:
```app/components/person-profile.js
import Component from '@ember/component';
export default class extends Component {
@computed('person.title', 'person.firstName', 'person.lastName')
get displayName() {
let { title, firstName, lastName } = this.person;
if (title) {
return `${title} ${lastName}`;
} else {
return `${firstName} ${lastName}`;
}
}
}
```
And then use it in the component's template:
```app/templates/components/person-profile.hbs
<h1>{{this.displayName}}</h1>
{{yield}}
```
## Customizing a Classic Component's HTML Element in JavaScript
### HTML Tag
The default HTML tag name used for a component's HTML representation is `div`.
This can be customized by setting the `tagName` property.
Consider the following component class:
```app/components/emphasized-paragraph.js
import Component from '@ember/component';
export default class extends Component {
tagName = 'em';
}
```
When invoked, this component would produce output that looks something like
this:
```html
<em id="ember1" class="ember-view"></em>
```
### HTML `class` Attribute
The HTML `class` attribute of a component's tag can be set by providing a
`classNames` property that is set to an array of strings:
```app/components/my-widget.js
import Component from '@ember/component';
export default class extends Component {
classNames = ['my-class', 'my-other-class'];
}
```
Invoking this component will produce output that looks like this:
```html
<div id="ember1" class="ember-view my-class my-other-class"></div>
```
`class` attribute values can also be set by providing a `classNameBindings`
property set to an array of properties names for the component. The return
value of these properties will be added as part of the value for the
components's `class` attribute. These properties can be computed properties:
```app/components/my-widget.js
import Component from '@ember/component';
import { computed } from '@ember/object';
export default class extends Component {
classNames = ['my-class', 'my-other-class'];
classNameBindings = ['propertyA', 'propertyB'];
propertyA = 'from-a';
get propertyB {
if (someLogic) { return 'from-b'; }
}
}
```
Invoking this component will produce HTML that looks like:
```html
<div id="ember1" class="ember-view my-class my-other-class from-a from-b"></div>
```
Note that `classNames` and `classNameBindings` is in addition to the `class`
attribute passed with the angle bracket invocation syntax. Therefore, if this
component was invoked like so:
```handlebars
<MyWidget class="from-invocation" />
```
The resulting HTML will look similar to this:
```html
<div id="ember1" class="from-invocation ember-view my-class my-other-class from-a from-b"></div>
```
If the value of a class name binding returns a boolean the property name
itself will be used as the class name if the property is true. The class name
will not be added if the value is `false` or `undefined`.
```app/components/my-widget.js
import Component from '@ember/component';
export default class extends Component {
classNameBindings = ['hovered'];
hovered = true;
}
```
Invoking this component will produce HTML that looks like:
```html
<div id="ember1" class="ember-view hovered"></div>
```
### Custom Class Names for Boolean Values
When using boolean class name bindings you can supply a string value other
than the property name for use as the `class` HTML attribute by appending the
preferred value after a ":" character when defining the binding:
```app/components/my-widget.js
import Component from '@ember/component';
export default class extends Component {
classNameBindings = ['awesome:so-very-cool'];
awesome = true;
}
```
Invoking this component will produce HTML that looks like:
```html
<div id="ember1" class="ember-view so-very-cool"></div>
```
Boolean value class name bindings whose property names are in a
camelCase-style format will be converted to a dasherized format:
```app/components/my-widget.js
import Component from '@ember/component';
export default class extends Component {
classNameBindings = ['isUrgent'];
isUrgent = true;
}
```
Invoking this component will produce HTML that looks like:
```html
<div id="ember1" class="ember-view is-urgent"></div>
```
Class name bindings can also refer to object values that are found by
traversing a path relative to the component itself:
```app/components/my-widget.js
import Component from '@ember/component';
import EmberObject from '@ember/object';
export default class extends Component {
classNameBindings = ['messages.empty'];
messages = EmberObject.create({
empty: true
});
}
```
Invoking this component will produce HTML that looks like:
```html
<div id="ember1" class="ember-view empty"></div>
```
If you want to add a class name for a property which evaluates to true and and
a different class name if it evaluates to false, you can pass a binding like
this:
```app/components/my-widget.js
import Component from '@ember/component';
export default class extends Component {
classNameBindings = ['isEnabled:enabled:disabled'];
isEnabled = true;
}
```
Invoking this component will produce HTML that looks like:
```html
<div id="ember1" class="ember-view enabled"></div>
```
When isEnabled is `false`, the resulting HTML representation looks like this:
```html
<div id="ember1" class="ember-view disabled"></div>
```
This syntax offers the convenience to add a class if a property is `false`:
```app/components/my-widget.js
import Component from '@ember/component';
// Applies no class when isEnabled is true and class 'disabled' when isEnabled is false
export default class extends Component {
classNameBindings = ['isEnabled::disabled'];
isEnabled = true;
}
```
Invoking this component when the `isEnabled` property is true will produce
HTML that looks like:
```html
<div id="ember1" class="ember-view"></div>
```
Invoking it when the `isEnabled` property on the component is `false` will
produce HTML that looks like:
```html
<div id="ember1" class="ember-view disabled"></div>
```
Updates to the value of a class name binding will result in automatic update
of the HTML `class` attribute in the component's rendered HTML
representation. If the value becomes `false` or `undefined` the class name
will be removed.
Both `classNames` and `classNameBindings` are concatenated properties. See
[EmberObject](/ember/release/classes/EmberObject) documentation for more
information about concatenated properties.
### Other HTML Attributes
The HTML attribute section of a component's tag can be set by providing an
`attributeBindings` property set to an array of property names on the
component. The return value of these properties will be used as the value of
the component's HTML associated attribute:
```app/components/my-anchor.js
import Component from '@ember/component';
export default class extends Component {
tagName = 'a';
attributeBindings = ['href'];
href = 'http://google.com';
};
```
Invoking this component will produce HTML that looks like:
```html
<a id="ember1" class="ember-view" href="http://google.com"></a>
```
One property can be mapped on to another by placing a ":" between the source
property and the destination property:
```app/components/my-anchor.js
import Component from '@ember/component';
export default class extends Component {
tagName = 'a';
attributeBindings = ['url:href'];
url = 'http://google.com';
};
```
Invoking this component will produce HTML that looks like:
```html
<a id="ember1" class="ember-view" href="http://google.com"></a>
```
HTML attributes passed with angle bracket invocations will take precedence
over those specified in `attributeBindings`. Therefore, if this component was
invoked like so:
```handlebars
<MyAnchor href="http://bing.com" @url="http://google.com" />
```
The resulting HTML will looks like this:
```html
<a id="ember1" class="ember-view" href="http://bing.com"></a>
```
Note that the `href` attribute is ultimately set to `http://bing.com`, despite
it having attribute binidng to the `url` property, which was set to
`http://google.com`.
Namespaced attributes (e.g. `xlink:href`) are supported, but have to be
mapped, since `:` is not a valid character for properties in Javascript:
```app/components/my-use.js
import Component from '@ember/component';
export default class extends Component {
tagName = 'use';
attributeBindings = ['xlinkHref:xlink:href'];
xlinkHref = '#triangle';
};
```
Invoking this component will produce HTML that looks like:
```html
<use xlink:href="#triangle"></use>
```
If the value of a property monitored by `attributeBindings` is a boolean, the
attribute will be present or absent depending on the value:
```app/components/my-text-input.js
import Component from '@ember/component';
export default class extends Component {
tagName = 'input';
attributeBindings = ['disabled'];
disabled = false;
};
```
Invoking this component will produce HTML that looks like:
```html
<input id="ember1" class="ember-view" />
```
`attributeBindings` can refer to computed properties:
```app/components/my-text-input.js
import Component from '@ember/component';
import { computed } from '@ember/object';
export default class extends Component {
tagName = 'input';
attributeBindings = ['disabled'];
get disabled() {
if (someLogic) {
return true;
} else {
return false;
}
}
};
```
To prevent setting an attribute altogether, use `null` or `undefined` as the
value of the property used in `attributeBindings`:
```app/components/my-text-input.js
import Component from '@ember/component';
export default class extends Component {
tagName = 'form';
attributeBindings = ['novalidate'];
novalidate = null;
};
```
Updates to the property of an attribute binding will result in automatic
update of the HTML attribute in the component's HTML output.
`attributeBindings` is a concatenated property. See
[EmberObject](/ember/release/classes/EmberObject) documentation for more
information about concatenated properties.
## Layouts
The `layout` property can be used to dynamically specify a template associated
with a component class, instead of relying on Ember to link together a
component class and a template based on file names.
In general, applications should not use this feature, but it's commonly used
in addons for historical reasons.
The `layout` property should be set to the default export of a template
module, which is the name of a template file without the `.hbs` extension.
```app/templates/components/person-profile.hbs
<h1>Person's Title</h1>
<div class='details'>{{yield}}</div>
```
```app/components/person-profile.js
import Component from '@ember/component';
import layout from '../templates/components/person-profile';
export default class extends Component {
layout = layout;
}
```
If you invoke the component:
```handlebars
<PersonProfile>
<h2>Chief Basket Weaver</h2>
<h3>Fisherman Industries</h3>
</PersonProfile>
```
or
```handlebars
{{#person-profile}}
<h2>Chief Basket Weaver</h2>
<h3>Fisherman Industries</h3>
{{/person-profile}}
```
It will result in the following HTML output:
```html
<h1>Person's Title</h1>
<div class="details">
<h2>Chief Basket Weaver</h2>
<h3>Fisherman Industries</h3>
</div>
```
## Handling Browser Events
There are two ways to handle user-initiated events:
### Using the `on` modifier to capture browser events
In a component's template, you can attach an event handler to any element with the `on` modifier:
```handlebars
<button {{on 'click' this.doSomething}} />
```
This will call the function on your component:
```js
import Component from '@ember/component';
export default class ExampleComponent extends Component {
doSomething = (event) => {
// `event` is the native click Event
console.log('clicked on the button');
};
}
```
See the [Guide on Component event
handlers](https://guides.emberjs.com/release/components/component-state-and-actions/#toc_html-modifiers-and-actions)
and the [API docs for `on`](../Ember.Templates.helpers/methods/on?anchor=on)
for more details.
### Event Handler Methods
Components can also respond to user-initiated events by implementing a method
that matches the event name. This approach is appropriate when the same event
should be handled by all instances of the same component.
An event object will be passed as the argument to the event handler method.
```app/components/my-widget.js
import Component from '@ember/component';
export default class extends Component {
click(event) {
// `event.target` is either the component's element or one of its children
let tag = event.target.tagName.toLowerCase();
console.log('clicked on a `<${tag}>` HTML element!');
}
}
```
In this example, whenever the user clicked anywhere inside the component, it
will log a message to the console.
It is possible to handle event types other than `click` by implementing the
following event handler methods. In addition, custom events can be registered
by using `Application.customEvents`.
Touch events:
* `touchStart`
* `touchMove`
* `touchEnd`
* `touchCancel`
Keyboard events:
* `keyDown`
* `keyUp`
* `keyPress`
Mouse events:
* `mouseDown`
* `mouseUp`
* `contextMenu`
* `click`
* `doubleClick`
* `focusIn`
* `focusOut`
Form events:
* `submit`
* `change`
* `focusIn`
* `focusOut`
* `input`
Drag and drop events:
* `dragStart`
* `drag`
* `dragEnter`
* `dragLeave`
* `dragOver`
* `dragEnd`
* `drop`
@class Component
@extends Ember.CoreView
@uses Ember.TargetActionSupport
@uses Ember.ActionSupport
@public
*/
// This type param is used in the class, so must appear here.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Component<S = unknown>
extends CoreView, TargetActionSupport, ActionSupport, ComponentMethods {}
class Component<S = unknown>
extends CoreView.extend(
TargetActionSupport,
ActionSupport,
{
// These need to be overridable via extend/create but should still
// have a default. Defining them here is the best way to achieve that.
didReceiveAttrs() {},
didRender() {},
didUpdate() {},
didUpdateAttrs() {},
willRender() {},
willUpdate() {},
} as ComponentMethods,
{
concatenatedProperties: ['attributeBindings', 'classNames', 'classNameBindings'],
classNames: EMPTY_ARRAY,
classNameBindings: EMPTY_ARRAY,
}
)
implements PropertyDidChange
{
isComponent = true;
// SAFETY: this has no runtime existence whatsoever; it is a "phantom type"
// here to preserve the type param.
declare private [SIGNATURE]: S;
// SAFTEY: This is set in `init`.
declare _superRerender: this['rerender'];
declare [IS_DISPATCHING_ATTRS]: boolean;
declare [DIRTY_TAG]: DirtyableTag;
/**
Standard CSS class names to apply to the view's outer element. This
property automatically inherits any class names defined by the view's
superclasses as well.
@property classNames
@type Array
@default ['ember-view']
@public
*/
declare classNames: string[];
/**
A list of properties of the view to apply as class names. If the property
is a string value, the value of that string will be applied as a class
name.
```javascript
// Applies the 'high' class to the view element
import Component from '@ember/component';
Component.extend({
classNameBindings: ['priority'],
priority: 'high'
});
```
If the value of the property is a Boolean, the name of that property is
added as a dasherized class name.
```javascript
// Applies the 'is-urgent' class to the view element
import Component from '@ember/component';
Component.extend({
classNameBindings: ['isUrgent'],
isUrgent: true
});
```
If you would prefer to use a custom value instead of the dasherized
property name, you can pass a binding like this:
```javascript
// Applies the 'urgent' class to the view element
import Component from '@ember/component';
Component.extend({
classNameBindings: ['isUrgent:urgent'],
isUrgent: true
});
```
If you would like to specify a class that should only be added when the
property is false, you can declare a binding like this:
```javascript
// Applies the 'disabled' class to the view element
import Component from '@ember/component';
Component.extend({
classNameBindings: ['isEnabled::disabled'],
isEnabled: false
});
```
This list of properties is inherited from the component's superclasses as well.
@property classNameBindings
@type Array
@default []
@public
*/
declare classNameBindings: string[];
init(properties?: object | undefined) {
super.init(properties);
// Handle methods from ViewMixin.
// The native class inheritance will not work for mixins. To work around this,
// we copy the existing rerender method provided by the mixin and swap in the
// new rerender method from our class.
this._superRerender = this.rerender;
this.rerender = this._rerender;
this[IS_DISPATCHING_ATTRS] = false;
this[DIRTY_TAG] = createTag();
this[BOUNDS] = null;
const eventDispatcher = this._dispatcher;
if (eventDispatcher) {
let lazyEventsProcessedForComponentClass = lazyEventsProcessed.get(eventDispatcher);
if (!lazyEventsProcessedForComponentClass) {
lazyEventsProcessedForComponentClass = new WeakSet<object>();
lazyEventsProcessed.set(eventDispatcher, lazyEventsProcessedForComponentClass);
}
let proto = Object.getPrototypeOf(this);
if (!lazyEventsProcessedForComponentClass.has(proto)) {
let lazyEvents = eventDispatcher.lazyEvents;
lazyEvents.forEach((mappedEventName, event) => {
if (mappedEventName !== null && typeof (this as any)[mappedEventName] === 'function') {
eventDispatcher.setupHandlerForBrowserEvent(event);
}
});
lazyEventsProcessedForComponentClass.add(proto);
}
}
if (
import.meta.env?.DEV &&
eventDispatcher &&
this.renderer._isInteractive &&
this.tagName === ''
) {
let eventNames = [];
let events = eventDispatcher.finalEventNameMapping;
for (let key in events) {
let methodName = events[key];
if (methodName && typeof (this as any)[methodName] === 'function') {
eventNames.push(methodName);
}
}
// If in a tagless component, assert that no event handlers are defined
assert(
`You can not define \`${eventNames}\` function(s) to handle DOM event in the \`${this}\` tagless component since it doesn't have any DOM element.`,
!eventNames.length
);
}
assert(
`Only arrays are allowed for 'classNameBindings'`,
descriptorForProperty(this, 'classNameBindings') === undefined &&
Array.isArray(this.classNameBindings)
);
assert(
`Only arrays of static class strings are allowed for 'classNames'. For dynamic classes, use 'classNameBindings'.`,
descriptorForProperty(this, 'classNames') === undefined && Array.isArray(this.classNames)
);
// ViewMixin
// Setup a view, but do not finish waking it up.
// * configure `childViews`
// * register the view with the global views hash, which is used for event
// dispatch
assert(
`You cannot use a computed property for the component's \`elementId\` (${this}).`,
descriptorForProperty(this, 'elementId') === undefined
);
assert(
`You cannot use a computed property for the component's \`tagName\` (${this}).`,
descriptorForProperty(this, 'tagName') === undefined
);
if (!this.elementId && this.tagName !== '') {
this.elementId = guidFor(this);
}
}
__dispatcher?: EventDispatcher | null;
get _dispatcher(): EventDispatcher | null {