mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-12 16:14:25 +03:00
ed85901607
refs https://github.com/TryGhost/Team/issues/1009 - setting `tabIndex="-1"` on the trigger prevents the trigger container receiving focus rather than the container input element (fixes general tabbing in and out) - added extra blur handling to the input element so that it is not left in an open dropdown state when tabbing out
107 lines
2.8 KiB
JavaScript
107 lines
2.8 KiB
JavaScript
/* global key */
|
|
import Component from '@glimmer/component';
|
|
import {action} from '@ember/object';
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
export default class GhSearchInputTrigger extends Component {
|
|
inputElem = null;
|
|
|
|
@action
|
|
registerInput(elem) {
|
|
this.inputElem = elem;
|
|
|
|
if (this.args.extra?.autofocus) {
|
|
this.inputElem.focus();
|
|
}
|
|
}
|
|
|
|
@action
|
|
captureMousedown(e) {
|
|
e.stopPropagation();
|
|
}
|
|
|
|
@action
|
|
handleInput(event) {
|
|
let term = event.target.value;
|
|
|
|
// open dropdown if not open and term is present
|
|
// close dropdown if open and term is blank
|
|
if (isBlank(term) === this.args.select.isOpen) {
|
|
isBlank(term) ? this.close() : this.open();
|
|
}
|
|
|
|
this.args.onInput?.(event);
|
|
}
|
|
|
|
// hacky workaround to let Escape clear the input if there's text,
|
|
// but still allow it to bubble if there's no text (used for closing modals, etc)
|
|
@action
|
|
handleKeydown(e) {
|
|
if ((e.key === 'Escape' && e.target.value) || e.key === 'Enter') {
|
|
this._previousKeyScope = key.getScope();
|
|
key.setScope('ignore');
|
|
}
|
|
}
|
|
|
|
@action
|
|
handleKeyup() {
|
|
if (key.getScope() === 'ignore') {
|
|
key.setScope(this._previousKeyScope);
|
|
}
|
|
}
|
|
|
|
@action
|
|
handleFocus() {
|
|
if (this.args.extra?.openOnFocus && this.args.select.results.length > 0) {
|
|
this.open();
|
|
}
|
|
}
|
|
|
|
@action
|
|
handleBlur(event) {
|
|
if (event?.relatedTarget) {
|
|
const thisInputTrigger = this.inputElem.closest('.ember-basic-dropdown-trigger');
|
|
const relatedInputTrigger = event.relatedTarget.closest('.ember-basic-dropdown-trigger');
|
|
|
|
if (relatedInputTrigger !== thisInputTrigger) {
|
|
this.args.select.actions.search('');
|
|
this.close();
|
|
}
|
|
}
|
|
|
|
if (this.args.extra.value && this.args.select.searchText === this.args.extra.value) {
|
|
this.args.select.actions.search('');
|
|
this.close();
|
|
}
|
|
}
|
|
|
|
@action
|
|
closeWhenEmpty() {
|
|
if (document.activeElement === this.inputElem) {
|
|
if (this.args.extra?.closeWhenEmpty) {
|
|
if (this.args.select.results.length > 0) {
|
|
this.open();
|
|
}
|
|
|
|
if (this.args.select.results.length === 0) {
|
|
this.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
open() {
|
|
if (!this.args.select.isOpen) {
|
|
// second argument skips ember-basic-dropdown focus
|
|
this.args.select.actions.open(null, true);
|
|
}
|
|
}
|
|
|
|
close() {
|
|
if (this.args.select.isOpen) {
|
|
// second argument skips ember-basic-dropdown focus
|
|
this.args.select.actions.close(null, true);
|
|
}
|
|
}
|
|
}
|