feat: focus last tag when pressing backspace on input
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { WebApplication } from '@/ui_models/application';
|
import { WebApplication } from '@/ui_models/application';
|
||||||
import { SNTag } from '@standardnotes/snjs';
|
import { SNTag } from '@standardnotes/snjs';
|
||||||
import { FunctionalComponent } from 'preact';
|
import { FunctionalComponent, RefObject } from 'preact';
|
||||||
import { useRef, useState } from 'preact/hooks';
|
import { useRef, useState } from 'preact/hooks';
|
||||||
import { Icon } from './Icon';
|
import { Icon } from './Icon';
|
||||||
import { Disclosure, DisclosurePanel } from '@reach/disclosure';
|
import { Disclosure, DisclosurePanel } from '@reach/disclosure';
|
||||||
@@ -10,11 +10,13 @@ import { AppState } from '@/ui_models/app_state';
|
|||||||
type Props = {
|
type Props = {
|
||||||
application: WebApplication;
|
application: WebApplication;
|
||||||
appState: AppState;
|
appState: AppState;
|
||||||
|
lastTagRef: RefObject<HTMLButtonElement>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const AutocompleteTagInput: FunctionalComponent<Props> = ({
|
export const AutocompleteTagInput: FunctionalComponent<Props> = ({
|
||||||
application,
|
application,
|
||||||
appState,
|
appState,
|
||||||
|
lastTagRef,
|
||||||
}) => {
|
}) => {
|
||||||
const [searchQuery, setSearchQuery] = useState('');
|
const [searchQuery, setSearchQuery] = useState('');
|
||||||
const [dropdownVisible, setDropdownVisible] = useState(false);
|
const [dropdownVisible, setDropdownVisible] = useState(false);
|
||||||
@@ -92,6 +94,11 @@ export const AutocompleteTagInput: FunctionalComponent<Props> = ({
|
|||||||
type="text"
|
type="text"
|
||||||
onBlur={closeOnBlur}
|
onBlur={closeOnBlur}
|
||||||
onFocus={showDropdown}
|
onFocus={showDropdown}
|
||||||
|
onKeyUp={(event) => {
|
||||||
|
if (event.key === 'Backspace') {
|
||||||
|
lastTagRef.current?.focus();
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
{dropdownVisible && (
|
{dropdownVisible && (
|
||||||
<DisclosurePanel
|
<DisclosurePanel
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { toDirective } from './utils';
|
|||||||
import { Icon } from './Icon';
|
import { Icon } from './Icon';
|
||||||
import { AutocompleteTagInput } from './AutocompleteTagInput';
|
import { AutocompleteTagInput } from './AutocompleteTagInput';
|
||||||
import { WebApplication } from '@/ui_models/application';
|
import { WebApplication } from '@/ui_models/application';
|
||||||
|
import { useRef } from 'preact/hooks';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
application: WebApplication;
|
application: WebApplication;
|
||||||
@@ -11,18 +12,22 @@ type Props = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const NoteTags = observer(({ application, appState }: Props) => {
|
const NoteTags = observer(({ application, appState }: Props) => {
|
||||||
|
const { activeNoteTags } = appState.notes;
|
||||||
|
const lastTagRef = useRef<HTMLButtonElement>();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-wrap">
|
<div className="flex flex-wrap">
|
||||||
{appState.notes.activeNoteTags.map((tag) => (
|
{activeNoteTags.map((tag, index) => (
|
||||||
<button
|
<button
|
||||||
className={`bg-contrast border-0 rounded text-xs color-text p-1 flex items-center
|
className={`bg-contrast border-0 rounded text-xs color-text p-1 flex items-center
|
||||||
mt-2 mr-2 cursor-pointer hover:bg-secondary-contrast focus:bg-secondary-contrast`}
|
mt-2 mr-2 cursor-pointer hover:bg-secondary-contrast focus:bg-secondary-contrast`}
|
||||||
|
ref={index === activeNoteTags.length - 1 ? lastTagRef : undefined}
|
||||||
>
|
>
|
||||||
<Icon type="hashtag" className="sn-icon--small color-neutral mr-1" />
|
<Icon type="hashtag" className="sn-icon--small color-neutral mr-1" />
|
||||||
{tag.title}
|
{tag.title}
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
<AutocompleteTagInput application={application} appState={appState} />
|
<AutocompleteTagInput application={application} appState={appState} lastTagRef={lastTagRef} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user