Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

input keyup.enter event error, if have datalist. #1694

Closed
joo opened this issue Dec 24, 2018 · 2 comments
Closed

input keyup.enter event error, if have datalist. #1694

joo opened this issue Dec 24, 2018 · 2 comments

Comments

@joo
Copy link

joo commented Dec 24, 2018

Debian 10
Chrome 71

environment:
sdk: ">=2.0.0 <3.0.0"

dependencies:
angular: ^5.1.0
angular_components: ^0.10.0
angular_router: ^2.0.0-alpha
intl: any
uuid: any
base58check: any
sass_builder: any

dev_dependencies:
build_runner: any
build_web_compilers: '>=0.3.6 <0.5.0'

template:

    <input
        (keyup.enter)="onEnter()"
        list="datalist"
    />
    <datalist id="datalist">
        <option *ngFor="let suggestion of ['01','02','03']" value="{{ suggestion }}">
            {{ suggestion }}
        </option>
    </datalist>

Component code:

  void onEnter() {
    // nothing
  }

Have a error when select a suggestion:

dart_sdk.js:4858 Uncaught _js_helper.CastErrorImpl.new {message: "Type 'Event$' is not a subtype of expected type 'KeyboardEvent$'.", Symbol(_error): Error
    at Object.dart.throw (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:485…}message: "Type 'Event$' is not a subtype of expected type 'KeyboardEvent$'."Symbol(_error): Error
    at Object.dart.throw (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4857:29)
    at Object.dart.castError (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4474:15)
    at Object.dart.as (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4773:17)
    at Function.as_C [as as] (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:3996:19)
    at dart.fn.event (http://localhost:9090/packages/angular/src/bootstrap/modules.ddc.js:2765:104)
    at Object.dart._checkAndCall (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4668:16)
    at Object.dart.dcall (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4673:17)
    at HTMLInputElement.(anonymous function).html$._wrapZone.dart.fn.e (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:96928:96)stack: "Error↵    at Object.dart.throw (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4857:29)↵    at Object.dart.castError (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4474:15)↵    at Object.dart.as (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4773:17)↵    at Function.as_C [as as] (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:3996:19)↵    at dart.fn.event (http://localhost:9090/packages/angular/src/bootstrap/modules.ddc.js:2765:104)↵    at Object.dart._checkAndCall (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4668:16)↵    at Object.dart.dcall (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:4673:17)↵    at HTMLInputElement.(anonymous function).html$._wrapZone.dart.fn.e (http://localhost:9090/packages/$sdk/dev_compiler/amd/dart_sdk.js:96928:96)"Symbol(dartx.hashCode): (...)Symbol(dartx.runtimeType): (...)__proto__: ObjecthashCode: (...)runtimeType: (...)stackTrace: (...)Symbol(dartx.hashCode): (...)Symbol(dartx.runtimeType): (...)Symbol(dartx.stackTrace): (...)__proto__: Error
dart.throw @ dart_sdk.js:4858
dart.castError @ dart_sdk.js:4474
dart.as @ dart_sdk.js:4773
as_C @ dart_sdk.js:3996
dart.fn.event @ key_events.dart:138
dart._checkAndCall @ dart_sdk.js:4668
dart.dcall @ dart_sdk.js:4673
(anonymous function).html$._wrapZone.dart.fn.e @ dart_sdk.js:96928
@leonsenft
Copy link
Contributor

It looks like the KeyEventsPlugin assumes that all keyup events are of type KeyboardEvent so that we can read the keyCode property. When you select a suggestion via the mouse, the browser dispatches a synthetic keyup event with no keyCode property.

I think the best we can do is change the KeyEventsPlugin to drop any events that don't have the keyCode property. This means your onEnter() handler won't fire when you make a selection via mouse.

So if you're specifically only care about keyboard events, keyup.enter will continue to work. But if you want to know whenever a selection is made change would be a better event to listen for:

<input (change)="onChange" list="datalist">

@leonsenft leonsenft self-assigned this Jan 14, 2019
@leonsenft
Copy link
Contributor

Note I tried listening directly to Element.onKeyUp since it's typed as a stream of KeyboardEvent, and discovered that this causes a crash in the SDK as well: dart-lang/sdk#35656.

For now we'll fix this by ignoring any events that aren't of type KeyboardEvent.

alorenzen pushed a commit that referenced this issue Jan 17, 2019
…oid crash

The `KeyEventsPlugin` responsible for handling composite key event bindings,
such as `(keyup.enter)`, will now ignore any events that aren't a real
`KeyBoardEvent`. Previously the plugin assumed all events were so that it could
access the `keyCode` property. This would lead to a crash in the event that a
synthetic or custom event with the same `type` was dispatched on the element.

Fixes #1694.

PiperOrigin-RevId: 229279612
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants