Skip to content

Commit

Permalink
feat(AutoComplete): add visual indicator if match is pending
Browse files Browse the repository at this point in the history
Fixes #221
  • Loading branch information
maxpatiiuk committed Jun 2, 2024
1 parent 3f16e42 commit eb5a01a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/src/components/PowerTools/AutoComplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import type { MatchRule, VirtualCalendar } from '../Widgets/VirtualCalendars';
import { matchRules } from '../Widgets/VirtualCalendars';
import { Synonym } from '../Widgets/Synonyms';

const indicator = document.createElement('div');
indicator.id = 'calendar-plus-autocomplete-indicator';

/**
* Provide autocomplete for calendar event names and automatically put events
* into correct calendars based on rules defined by the user
Expand Down Expand Up @@ -75,7 +78,17 @@ export function AutoComplete(): JSX.Element {
};
}

setActiveMatch(findMatch());
const match = findMatch();
setActiveMatch(match);

// Show autocomplete prediction indicator
const parent = element.parentElement;
if (match) {
const calendar = calendars.find(({ id }) => id === match.calendar);
indicator.style.background =
calendar?.backgroundColor ?? 'transparent';
parent?.append(indicator);
} else indicator.remove();

if (blurListeners.has(element)) return;
element.addEventListener('blur', handleBlur);
Expand Down Expand Up @@ -127,7 +140,7 @@ export function AutoComplete(): JSX.Element {
const expandCalendarsButton = findCalendarsButton(parent);
const alreadyOpened =
expandCalendarsButton === undefined ||
expandCalendarsButton.getBoundingClientRect().height === 0;
expandCalendarsButton.offsetWidth === 0;

const trigger = expandCalendarsButton ?? combobox;
// Don't change calendar if correct one is already selected
Expand Down Expand Up @@ -171,7 +184,6 @@ export function AutoComplete(): JSX.Element {
[virtualCalendars],
);

console.log(activeMatch);
const activeEventNames = React.useMemo(() => {
if (activeMatch === undefined || activeMatch.synonym.length === 0)
return eventNames;
Expand Down
17 changes: 17 additions & 0 deletions src/src/css/autocomplete-indicator.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* A small border-bottom indicator for the event name input. The indicator
* will be shown if pending event name matches some calendar name as per
* autocomplete rules, and the indicator will have the color of the matched
* calendar.
*/
@layer components {
#calendar-plus-autocomplete-indicator {
position: absolute;
pointer-events: none;
width: 100%;
height: 2px;
bottom: 0;
border-radius: 1rem;
z-index: 2;
}
}
1 change: 1 addition & 0 deletions src/src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@import './condense-interface.css';
@import './less-invasive-batch-edit.css';
@import './ghost-events.css';
@import './autocomplete-indicator.css';

/**
* This injects Tailwind's utility classes and any utility classes registered
Expand Down

0 comments on commit eb5a01a

Please sign in to comment.