Skip to content

Commit

Permalink
feat: create and add tag on click enter
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonella Sgarlatta committed May 25, 2021
1 parent 4c6bfb8 commit e3ea0aa
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions app/assets/javascripts/components/AutocompleteTagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ export const AutocompleteTagInput: FunctionalComponent<Props> = ({

const inputRef = useRef<HTMLInputElement>();
const dropdownRef = useRef<HTMLDivElement>();
const [closeOnBlur, setLockCloseOnBlur] = useCloseOnBlur(dropdownRef, (visible: boolean) => {
setDropdownVisible(visible);

const clearResults = () => {
setSearchQuery('');
setTagResults(getActiveNoteTagResults(''));
};

const [closeOnBlur, setLockCloseOnBlur] = useCloseOnBlur(dropdownRef, (visible: boolean) => {
setDropdownVisible(visible);
clearResults();
});

const showDropdown = () => {
Expand All @@ -59,14 +64,23 @@ export const AutocompleteTagInput: FunctionalComponent<Props> = ({
setLockCloseOnBlur(false);
};

const onTagHintClick = async () => {
const createAndAddNewTag = async () => {
const newTag = await application.findOrCreateTag(searchQuery);
await appState.notes.addTagToActiveNote(newTag);
setSearchQuery('');
clearResults();
};

const onTagHintClick = async () => {
await createAndAddNewTag();
};

const onFormSubmit = async (event: Event) => {
event.preventDefault();
await createAndAddNewTag();
};

return (
<form onSubmit={(event) => event.preventDefault()} className="mt-2">
<form onSubmit={onFormSubmit} className="mt-2">
<Disclosure open={dropdownVisible} onChange={showDropdown}>
<input
ref={inputRef}
Expand Down

0 comments on commit e3ea0aa

Please sign in to comment.