-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
172 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 75 additions & 48 deletions
123
client/src/cqlInput/popover/components/TextSuggestionContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,88 @@ | ||
|
||
import { h } from "preact"; | ||
import { useEffect, useRef } from "preact/hooks"; | ||
import { useEffect, useRef, useState } from "preact/hooks"; | ||
import { TextSuggestion } from "../../../lang/types"; | ||
import { ActionSubscriber } from "./PopoverContainer"; | ||
import { wrapSelection } from "./utils"; | ||
|
||
const numberFormat = new Intl.NumberFormat(); | ||
|
||
export const TextSuggestionContent = ({ | ||
suggestion, | ||
onSelect, | ||
currentOptionIndex, | ||
}: { | ||
suggestion: TextSuggestion; | ||
onSelect: (value: string) => void; | ||
currentOptionIndex: number | undefined; | ||
}) => { | ||
const showCount = suggestion.position === "chipValue"; | ||
const showValue = suggestion.position === "chipValue"; | ||
const showDescription = suggestion.position === "chipKey"; | ||
const currentItemRef = useRef<HTMLDivElement | null>(null); | ||
suggestion, | ||
onSelect, | ||
subscribeToAction, | ||
}: { | ||
suggestion: TextSuggestion; | ||
onSelect: (value: string) => void; | ||
subscribeToAction: ActionSubscriber; | ||
}) => { | ||
const showCount = suggestion.position === "chipValue"; | ||
const showValue = suggestion.position === "chipValue"; | ||
const showDescription = suggestion.position === "chipKey"; | ||
const currentItemRef = useRef<HTMLDivElement | null>(null); | ||
const [currentOptionIndex, setCurrentOptionIndex] = useState(0); | ||
|
||
useEffect(() => { | ||
if (currentOptionIndex !== undefined) { | ||
currentItemRef.current?.scrollIntoView({ block: "nearest" }); | ||
useEffect(() => { | ||
subscribeToAction((action) => { | ||
switch (action) { | ||
case "up": | ||
setCurrentOptionIndex( | ||
wrapSelection(currentOptionIndex, -1, suggestion.suggestions.length) | ||
); | ||
return true; | ||
case "down": { | ||
setCurrentOptionIndex( | ||
wrapSelection(currentOptionIndex, 1, suggestion.suggestions.length) | ||
); | ||
return true; | ||
} | ||
case "enter": { | ||
onSelect(suggestion.suggestions[currentOptionIndex].value); | ||
return true; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
}, [currentOptionIndex]); | ||
}); | ||
}, [subscribeToAction, currentOptionIndex, suggestion]); | ||
|
||
if (!suggestion.suggestions.length) { | ||
return ( | ||
<div class="Cql__Option"> | ||
<div class="Cql__OptionLabel">No results</div> | ||
</div> | ||
); | ||
useEffect(() => { | ||
if (currentOptionIndex !== undefined) { | ||
currentItemRef.current?.scrollIntoView({ block: "nearest" }); | ||
} | ||
}, [currentOptionIndex]); | ||
|
||
if (!suggestion.suggestions.length) { | ||
return ( | ||
<div class="Cql__Option"> | ||
<div class="Cql__OptionLabel">No results</div> | ||
</div> | ||
); | ||
} | ||
|
||
return suggestion.suggestions.map( | ||
({ label, description, value, count }, index) => { | ||
const isSelected = index === currentOptionIndex; | ||
const selectedClass = isSelected ? "Cql__Option--is-selected" : ""; | ||
return ( | ||
<div | ||
class={`Cql__Option ${selectedClass}`} | ||
data-index={index} | ||
ref={isSelected ? currentItemRef : null} | ||
onClick={() => onSelect(value)} | ||
> | ||
<div class="Cql__OptionLabel"> | ||
{label} | ||
{showCount && count !== undefined && ( | ||
<div class="Cql__OptionCount">{numberFormat.format(count)}</div> | ||
)} | ||
</div> | ||
{showValue && <div class="Cql__OptionValue">{value}</div>} | ||
{showDescription && description && ( | ||
<div class="Cql__OptionDescription">{description}</div> | ||
return suggestion.suggestions.map( | ||
({ label, description, value, count }, index) => { | ||
const isSelected = index === currentOptionIndex; | ||
const selectedClass = isSelected ? "Cql__Option--is-selected" : ""; | ||
return ( | ||
<div | ||
class={`Cql__Option ${selectedClass}`} | ||
data-index={index} | ||
ref={isSelected ? currentItemRef : null} | ||
onClick={() => onSelect(value)} | ||
> | ||
<div class="Cql__OptionLabel"> | ||
{label} | ||
{showCount && count !== undefined && ( | ||
<div class="Cql__OptionCount">{numberFormat.format(count)}</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
); | ||
}; | ||
{showValue && <div class="Cql__OptionValue">{value}</div>} | ||
{showDescription && description && ( | ||
<div class="Cql__OptionDescription">{description}</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const wrapSelection = (current: number, by: number, length: number) => | ||
(current + by + (by < 0 ? length : 0)) % length; |