Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inputs in shadow DOM in disableOnInputFields mode #578

Merged
merged 1 commit into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions addon-test-support/key-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import validMouseButtons from 'ember-keyboard/fixtures/mouse-buttons-array';
import getCmdKey from 'ember-keyboard/utils/get-cmd-key';
import { triggerEvent } from '@ember/test-helpers';

export function keyEvent(keyCombo, type, element = document) {
export function keyEvent(
keyCombo,
type,
element = document,
eventOptions = {}
) {
let keyComboParts = (keyCombo || '').split('+');

let eventProps = keyComboParts.reduce((eventProps, keyComboPart) => {
Expand All @@ -30,5 +35,5 @@ export function keyEvent(keyCombo, type, element = document) {
return eventProps;
}, {});

return triggerEvent(element, type, eventProps);
return triggerEvent(element, type, { ...eventOptions, ...eventProps });
}
4 changes: 2 additions & 2 deletions addon-test-support/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export function keyDown(keyCombo) {
return keyEvent(keyCombo, 'keydown');
}

export function keyDownWithElement(keyCombo, element) {
return keyEvent(keyCombo, 'keydown', element);
export function keyDownWithElement(keyCombo, element, eventOptions) {
return keyEvent(keyCombo, 'keydown', element, eventOptions);
}

export function keyUp(keyCombo) {
Expand Down
6 changes: 3 additions & 3 deletions addon/services/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export default class KeyboardService extends Service {
@action
_respond(event) {
if (this._disableOnInput && event.target) {
const tag = event.target.tagName;
const target = event.composedPath()[0] ?? event.target;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that composedPath is supported in all browsers except IE. But I believe this addon does not support IE anyway, right? Otherwise we could add a better check and fall back to event.target if not supported...

const tag = target.tagName;
const isContentEditable =
event.target.getAttribute &&
event.target.getAttribute('contenteditable') != null;
target.getAttribute && target.getAttribute('contenteditable') != null;
if (isContentEditable || tag === 'TEXTAREA' || tag === 'INPUT') {
return;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/acceptance/disable-on-input-fields-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ module('Acceptance | disableOnInputFields config', function (hooks) {
assert.deepEqual(getValues(), [0, 0, 0], 'responders do not respond');
});

test('test event does not propagate on input field in open shadow DOM', async function (assert) {
assert.expect(1);

await visit('/test-scenario');

const input = this.element
.querySelector('[data-test-shadow-dom]')
.shadowRoot.querySelector('input');

// trigger a "composed" event to make sure the event triggered within the shadow dom is able to traverse the shadow root boundary.
// this is only needed for the synthetic event we create here in tests, "real" events do this by default.
await keyDownWithElement('ArrowRight', input, { composed: true });
assert.deepEqual(getValues(), [0, 0, 0], 'responders do not respond');
});

test('test standard functionality', async function (assert) {
assert.expect(8);

Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Application from '@ember/application';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from 'dummy/config/environment';
import './custom-elements/input-in-open-shadow';

export default class App extends Application {
modulePrefix = config.modulePrefix;
Expand Down
11 changes: 11 additions & 0 deletions tests/dummy/app/custom-elements/input-in-open-shadow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (typeof FastBoot === 'undefined') {
class InputInOpenShadow extends HTMLElement {
async connectedCallback() {
const shadowRoot = this.attachShadow({ mode: 'open' });
const input = document.createElement('input');
shadowRoot.appendChild(input);
}
}

customElements.define('input-in-open-shadow', InputInOpenShadow);
}
2 changes: 2 additions & 0 deletions tests/dummy/app/templates/test-scenario/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@

<label for="data-test-input-field">input field</label>
<input id="data-test-input-field" data-test-input-field/>

<input-in-open-shadow data-test-shadow-dom/>