fix dynamic setting of suggestions modal (#272)

This commit is contained in:
Red J Adaya 2024-08-27 07:33:05 +08:00 committed by GitHub
parent cefc491bf2
commit 7d98aeb2c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 20 deletions

View File

@ -34,6 +34,7 @@
border: 0.5px solid var(--modal-border-color);
background: var(--modal-bg-color);
box-shadow: 0px 8px 32px 0px rgba(0, 0, 0, 0.25);
height: auto;
.modal-close-btn {
position: absolute;

View File

@ -2,7 +2,7 @@ import { Input } from "@/app/element/input";
import { InputDecoration } from "@/app/element/inputdecoration";
import { useDimensions } from "@/app/hook/useDimensions";
import clsx from "clsx";
import React, { useEffect, useRef, useState } from "react";
import React, { forwardRef, useEffect, useRef, useState } from "react";
import ReactDOM from "react-dom";
import "./typeaheadmodal.less";
@ -81,15 +81,9 @@ interface SuggestionsProps {
onSelect?: (_: string) => void;
}
function Suggestions({ suggestions, onSelect }: SuggestionsProps) {
const suggestionsWrapperRef = useRef<HTMLDivElement>(null);
const Suggestions = forwardRef<HTMLDivElement, SuggestionsProps>(({ suggestions, onSelect }: SuggestionsProps, ref) => {
return (
<div
ref={suggestionsWrapperRef}
className="suggestions-wrapper"
style={{ marginTop: suggestions?.length > 0 ? "8px" : "0" }}
>
<div ref={ref} className="suggestions-wrapper" style={{ marginTop: suggestions?.length > 0 ? "8px" : "0" }}>
{suggestions?.map((suggestion, index) => (
<div className="suggestion" key={index} onClick={() => onSelect(suggestion.value)}>
{suggestion.label}
@ -97,7 +91,7 @@ function Suggestions({ suggestions, onSelect }: SuggestionsProps) {
))}
</div>
);
}
});
interface TypeAheadModalProps {
anchor: React.MutableRefObject<HTMLDivElement>;
@ -125,20 +119,31 @@ const TypeAheadModal = ({
const { width, height } = useDimensions(anchor);
const modalRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLDivElement>(null);
const suggestionsWrapperRef = useRef<HTMLDivElement>(null);
const [suggestionsHeight, setSuggestionsHeight] = useState<number | undefined>(undefined);
const [modalHeight, setModalHeight] = useState<string | undefined>(undefined);
useEffect(() => {
if (modalRef.current && inputRef.current) {
const modalHeight = modalRef.current.getBoundingClientRect().height;
if (modalRef.current && inputRef.current && suggestionsWrapperRef.current) {
const modalPadding = 32;
const inputHeight = inputRef.current.getBoundingClientRect().height;
let suggestionsTotalHeight = 0;
// Get the padding value (assuming padding is uniform on all sides)
const padding = 16 * 2; // 16px top + 16px bottom
// Subtract the input height and padding from the modal height
setSuggestionsHeight(modalHeight - inputHeight - padding);
const suggestionItems = suggestionsWrapperRef.current.children;
for (let i = 0; i < suggestionItems.length; i++) {
suggestionsTotalHeight += suggestionItems[i].getBoundingClientRect().height;
}
}, [width, height]);
const totalHeight = modalPadding + inputHeight + suggestionsTotalHeight;
const maxHeight = height * 0.8;
const computedHeight = totalHeight > maxHeight ? maxHeight : totalHeight;
setModalHeight(`${computedHeight}px`);
const padding = 16 * 2;
setSuggestionsHeight(computedHeight - inputHeight - padding);
}
}, [height, suggestions.length]);
const renderBackdrop = (onClick) => <div className="type-ahead-modal-backdrop" onClick={onClick}></div>;
@ -162,7 +167,7 @@ const TypeAheadModal = ({
className={clsx("type-ahead-modal", className)}
style={{
width: width * 0.6,
maxHeight: height * 0.8,
maxHeight: modalHeight,
}}
>
<div className="content-wrapper">
@ -187,7 +192,7 @@ const TypeAheadModal = ({
overflowY: "auto",
}}
>
<Suggestions suggestions={suggestions} onSelect={handleSelect} />
<Suggestions ref={suggestionsWrapperRef} suggestions={suggestions} onSelect={handleSelect} />
</div>
</div>
</div>

View File

@ -687,7 +687,6 @@ function PreviewView({ blockId, model }: { blockId: string; model: PreviewModel
anchor={contentRef}
onKeyDown={(e) => keyutil.keydownWrapper(handleKeyDown)(e)}
onSelect={handleFileSuggestionSelect}
onSubmit={(value) => console.log(value)}
/>
)}
<div